Class Notes Class 11 Computer Science (Dictionary and Tuple)
Class Notes Class 11 Computer Science (Dictionary and Tuple)
Python’s dictionaries are kind of hash table type. They work like associative arrays or hashes found in
Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually
numbers or strings. Values, on the other hand, can be any arbitrary Python object.
It is an unordered collection of items where each item consists of a key and a value. It is mutable
(can modify its contents) but Key must be unique and immutable.
Creating Dictionary
It is enclosed in curly braces { } and each item is separated from other item by a comma (,).Within
each item, key and value are separated by a colon(:).
Example :
print day
Output:
print day
print day[1]
print day[3]
Output:
Monday
Wednesday
>>>
print(day.get(1))
Output
Monday
>>>
Following example will show how dictionary items can be accessed through loop.
for i in day:
print(day[i])
Output:
Monday
Tuesday
Wednesday
>>>
day[1]='Sunday'
print(day)
OUTPUT
>>>
day[4]='Thursday'
day[5]='Friday'
day[6]='Saturday'
day[7]='Sunday'
print day
Output:
>>>
del, pop() and clear() statement are used to remove elements from the dictionary.
day {1:'Monday',2:'Tuesday',3:'Wednesday',4:'Thursday',5:'Friday'}
del day[1]
print day
Output:
{2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday'}
>>>
♦ len(dict) Gives the total length of the dictionary. It is equal to the number of items in the
dictionary.
♦ setdefault(key,default=None) Similar to get (), but will set dict [key] = default if key is not already
in dict
7. A dictionary key can be almost any Python type, but are usually numbers or strings.
11. There are two methods to delete elements from a dictionary: (i) using del statement (ii) using
pop( ) method
12. To check the existence of a key in dictionary, two operators are used (i) in (ii) not in
Python Tuples
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of
values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.
The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their
elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be
updated.
Tuple: It is a sequence of immutable objects. It is just like list. Difference between the tuples and the
lists is that the tuples cannot be changed unlike lists. Lists uses square bracket whereas tuples use
parentheses.
Creating A Tuple:
A tuple is enclosed in parentheses () for creation and each item is separated by a comma.
Updating Tuples
Tuples are immutable,that’s why we can’t change the content of tuple.It’s alternate way is to take
contents of existing tuple and create another tuple with these contents as well as new content.
tup1 = (1, 2)
tup2 = (‘a’, ‘b’)
tup3 = tup1 + tup2
print (tup3)
Output
(1, 2, ‘a’, ‘b’)
Direct deletion of tuple element is not possible but shifting of required content after discard of
unwanted content to another tuple.
del tup1
Tuple Functions