Python Scripting - Week 4 Flashcards
(44 cards)
What are containers in Python ?
To save multiple values in one object, containers are used
How many type of containers are in Python
There are four types of containers in Python
1. Lists
2. Tuples
3. Dictionaries
4. Sets
List in Python surrounded by
Square brackets []
How items are separated in list
Items in list are separated by comma
What type of values we put in a list
We put any type of value in list
What is indices in list
List have index positions, starts with 0 and and with total length of List
Can we slice the list
Yes we can slice the list,
What is the format of list slicing
[start,stop,step]
How to assign values to list
list1=[1,2,3,4]
list1[1]=”Hello”
list1=[i for i in range(10)]
How to insert a new value to a list ?
To insert and value to an list insert() method is used.
Index position is required to insert a value to a list
list1=[1,2,3]
list1.insert(1,20)
How to remove an item from list
We have to use remove method
list1.remove(4)
It removes first occurrence of value
list1.pop(2)
removes value at given index
What are the basic operations on list
- Assignment
- Insert
- Delete
- Append
- search
How to delete an item from last
We have to use pop method
In delete the last item permanently and also returns it
list1.pop()
How to concatenate two lists
a = [1,2,3,4]
B = [2,3,4,5]
c= a+b
Can you put list in a list
Yourwe can put list in a list
[a = [1,2,3,4] list1+=[4,4,4,[5,5]]]
How to create a tuple
Tuples are created using brackets
mytuple = (1,2,3)
What types of values can be put in a tuple
We can put any type of values in a tuple
What kind of operations we can perform on Tuple
Tuples are immutable objects we cannot add or Delete anything inside the tuple
What are the methods of tuple ?
Index() and and count()
What is the use of index method in tuples
Index method is used to get index position of given value,
IT returns the index value of first occurrence of given value
What is the use of count method in truple
Count method is used to get, how many times a value appears in a tuple
Can we concatenate tuple
Yes we can
a = (1,2)
b = (”bob”, “mary”)
a + b
What are mutable objects
Mutable objects can change after their creation
Define mutability of list
a=[1,2,3,4]
a
b=a
a[2]=8
print(b)
print(a)
Both A & B prints same value , becoz a & b points to same location , they are immutable