In python there are some data structures used for storing the particular type of data.Most importants in that are Lists,Tuple and Ditionary.
A List,they are the just like the arrays in other language,list contains strings,integers,as well as objects.In other words,List stores a sequence of objects in a defined order,they allow indexing and iterating through the list.Lists are mutable which you can modify after creation. A List is created by [].
A Tuple,is same as list. The only difference is that tuple is immutable that is the elements in tuple cannot be modify once it added.Another difference is “Tuples are sequence of heterogeneous data where list is homogeneous sequence of data.”Tuples is usually faster than list. A Tuple is created by ().
A Dictionary,is an sequence of key-value pairs.where it is similar to hash or maps in other language.The value can be accessed by unique key in the dictionary.Each key and value is separated by a colon(:).Search operations happen to be faster in dictionary as they use keys for searching. A dictionary is created by {}.
Index Positions for List & Tuple are started from 0 (zero).
Methods Followed by List , Tuple and Dictionary in Python.
Methods in List
Creating a List
ex. list1 is a list of names
list1=[“Swaraj“,”Suraj“,”Manas“,”Pankaj“]
print(list1)
Output:
[‘Swaraj’, ‘Suraj’, ‘Manas’, ‘Pankaj’]
Accessing values in a list
list1=[“Swaraj“,”Suraj“,”Manas“,”Pankaj“]
list1[3]=”Shreyas“
print(list1[2]) # print the image at index position 2
print(list1) # The whole list is printed
Output:
Manas
[‘Swaraj’, ‘Suraj’, ‘Manas’, ‘Shreyas’]
Deletion of List and element in list
list1=[“Swaraj“,”Suraj“,”Manas“,”Pankaj“]
print(list1)
del list1[2] #delete the element at index position 2
print(list1)
list1=[“Swaraj“,”Suraj“,”Manas“,”Pankaj“]
del list1[1:3] # The list removes from index oreder 1 to 3 where 3 is excluded and 1 is included.
print(list1)
del list1
Output:
[‘Swaraj’, ‘Suraj’, ‘Manas’, ‘Pankaj’]
[‘Swaraj’, ‘Suraj’, ‘Pankaj’]
[‘Swaraj’, ‘Pankaj’]
Insertion of list in another list
list1=[“Swaraj“,”Suraj“,”Manas“,”Pankaj“]
list2=[“shreyas“,”Pranesh“]
list1[2]=list2 #list2 is added in the list1 at index position 2
print(list1)
Output:
[‘Swaraj’, ‘Suraj’, [‘shreyas’, ‘Pranesh’], ‘Pankaj’]
Methods in Tuple
Creating a Tuple
ex. tup1 is a tuple of sequence of even number in the range 1 to 11
tup1=(2,4,6,8,10)
print(tup1)
Output:
(2, 4, 6, 8, 10)
Accessing values in Tuple
tup1=(2,4,6,8,10)
print(tup1)
print(tup1[2:4])
Output:
(2, 4, 6, 8, 10)
(6, 8)
Updating Tuple
tup1=(2,4,6,8,10)
tup2=(1,3,5,7,9)
tup3=tup1+tup2
print(tup3)
Output:
(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)
Deletion of tuple and element in tuple
tup1=(2,4,6,8,10)
del tup1[2] # show error message since tuple has immutable type hence element in tuple is not deleted
print(tup1)
del tup1
print(tup1) # show error message since the whole tuple is deleted
Output:
Traceback (most recent call last):
File “C:\Users\sai\Desktop\ex.py”, line 2, in
del tup1[2] # show error message since tuple has immutable type hence element in tuple is not deleted
TypeError: ‘tuple’ object doesn’t support item deletion
Methods in Dictionary
Creating a Dictionary
ex. dict1 is a dictionary of student information.
dict1={‘Roll_no‘:’16‘,’Name‘:’Suraj‘,’class‘:’BE‘}
print(dict1)
Output:
{‘Roll_no’: ’16’, ‘Name’: ‘Suraj’, ‘class’: ‘BE’}
Accessing values in Dictionary
In dictinary value is accessed by key which is assigned to value.
syntax:dict1={key:value}
dict1={‘Roll_no‘:’16‘,’Name‘:’Suraj‘,’class‘:’BE‘}
print(“Dict[‘Roll_no’]:“,dict1[‘Roll_no‘]) #show the value assigned to the key “Roll_no” .
Output:
Dict[‘Roll_no’]:16
Adding and Modifying an item in Dictionary
To add new entry or a key-value pair in a dictionary,just spcify the key-value pair as you had done for existing pairs.
syntax:dictionary_name[key]=value
dict1={‘Roll_no‘:’16‘,’Name‘:’Suraj‘,’class‘:’BE‘}
dict1[‘division‘]=”D” # This key-value pair is added at the last position in dict1 dictionary.
print(dict1)
Output:
{‘Roll_no’: ’16’, ‘Name’: ‘Suraj’, ‘class’: ‘BE’, ‘division’: ‘D’}
Deletion of dictionary or element in dictionary
By del function:
syntax:del dictionary_variable[key]
dict1={‘Roll_no‘:’16‘,’Name‘:’Suraj‘,’class‘:’BE‘}
print(dict1)
del dict1[‘Roll_no‘] # deleting a key of “Roll_no”
print(dict1)
del dict1 # the whole dictionary is deleted
print(dict1)
Output:
{‘Roll_no’: ’16’, ‘Name’: ‘Suraj’, ‘class’: ‘BE’}
{‘Name’: ‘Suraj’, ‘class’: ‘BE’}
Traceback (most recent call last):
File “C:\Users\sai\Desktop\ex.py”, line 6, in
print(dict1)
NameError: name ‘dict1’ is not defined
By pop function:
syntax:dictionary_name.pop(key)
dict1={‘Roll_no‘:’16‘,’Name‘:’Suraj‘,’class‘:’BE‘}
print(dict1)
dict1.pop(“Name“)
print(dict1)
Output:
{‘Roll_no’: ’16’, ‘Name’: ‘Suraj’, ‘class’: ‘BE’}
{‘Roll_no’: ’16’, ‘class’: ‘BE’}
- Keys must have unique values,Not even a single key can be duplicated in a dictionary. If you try to add a duplicate key, then the last assignment is continued.
ex.
dict1={‘Roll_no‘:’16‘,’Name‘:’Suraj‘,’class‘:’BE‘,’Name‘:’Kriti‘}
print(“Dict[‘Name’]:“,dict1[‘Name‘])
Output:
Dict[‘Name’]: Kriti
Comment me if you have some doubt.
Stay Safe || Stay Happy
