Python Scripting - Week 4 Flashcards
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