Featured
- Get link
- X
- Other Apps
Tuples and dictionary
Tuple:
The next sequence type we are going to look is very similar to list. Its tuple. The only difference between tuple and list is tuple are immutable and list are mutable. We cannot edit or manipulate tuple.
Tuple is defined by using parenthesis rather than square bracket. In tuple the iteration run faster than list because tuple values are fixed.
Basically all reading and accessing function like len,min and max stay the same and can be used with tuple. But it is not possible to use any modification
Example:
tup2=(10,20,30,40,50)
tup3=('a',)
print(len(tup1)
print(tup1+tup2)
print(tup3*3)
print(max(tup1))
print(min(tup1))
print(sorted(tup1))
print(sum(tup1,9)
Output:
(12, 23, 34, 45, 67, 10, 20, 30, 40, 50)
('a','a','a')
67
12
[12, 23, 34, 45, 67]
190
Hope you understand the program and output if not copy the code and paste it in the console page and see the output
Dictionary:
Python dictionary is a container of unordered set of elements like list. The elements are surrounded by curly braces{}. Every entry in this sequence has a key and a respective value where keys are usually numbers or strings and values can be any arbitrary python datatypes. In other programming language this structure is called hash map.
print (dict)
print(len(dict))
print(dict.item())
print("print name:", dict ['name'])
print("print subject:", dict['subject'])
dict1={'dob':2001}
dict.update(dict1)
print("update:", dict)
dict.pop('dob')
print("dob removed:", dict)
Output:
To get more clear view on the program copy the code and run it in the console page in menu and also make your own datatypes and values to get new experience and fun!!!!!!
- Get link
- X
- Other Apps

Comments
Post a Comment