Notes_Class_10_Recap_Python_List_Tuples_Dictionary_set
Notes_Class_10_Recap_Python_List_Tuples_Dictionary_set
If you add new items to a list, the new items will be placed at the end of the list.
Note: There are some list methods that will change the order, but in general: the
order of the items will not change.
Since lists are indexed, lists can have items with the same value. Lists allow
Lists
To assign a list to a variable.
list1=[1,2,3,4,5]
list2=[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
list3=[‘DPS’, 12, ‘Bopal’]
List4=[] #empty list
list5=list() #creating an empty list using list
function
list6=[3,4,[5,6],7,8] #nested list
print(type(list1)) #will display <class list>
print(list1[1]) #will print 2
print(list2[3]) #will print ‘o’
print(list2[-2]) #will print ‘o’
Lists
list7=list(‘DPS’)
print(list7) #will print [‘D’, ‘P’, ‘S’]
t1=(‘w’, ‘o’, ‘r’, ‘k’) # creates tuple t1
print(list(t1)) #will print [‘w’, ‘o’, ‘r’, ‘k’]
del: Python del function is used to delete an item from a list at a user-specified
index. The index position of the List del function starts from 0 and ends at n-1
del list6[0] #will print[4,[5,6],7,8]
del list6 # will delete the entire list
print(list 6) # will show error ‘list6 not defined’
Lists
list6=[3,4,[5,6],7,8] (consider this list as reference for the
following)
The implication of
The implication of iterations is
2 iterations is Time-
comparatively Faster
consuming
The update() is used to add any literals (list, tuple, set) in a set
set1={10, ‘Vinay’, 19}
set2={30,40}
set1.update(set2)
print(set1) #will {19, 40, 10, 30, “Vinay”}
Set
The remove(), discard() and pop() functions are used to in set to
delete from the set.
set1={10, ‘Vinay’, 19}
set1.remove(19)
print(set1) #will {10, ‘Vinay’}
clear() function is used to delete all elements in a set & del key
word is used to destroy or delete a set permanently.
set1={10, ‘Vinay’, 19}
del set1
print(set1) #will show an error
Dictionary
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered, changeable and do
not allow duplicates.
As of Python version 3.7, dictionaries are ordered. In Python 3.6
and earlier, dictionaries are unordered.
Dictionaries are written with curly brackets, and have keys and
values.