Chap-4 Python Tuples and Dictionary
Chap-4 Python Tuples and Dictionary
PYTHON PROGRAMMING
PROGRAMMING -- II
Chap - 4 By-
Prof. A. P. Chaudhari
len(1,2,3) 3 Length
Syntax: len(tuple)
Parameters: tuple − This is a tuple for which number of elements to be counted.
Returns: This method returns the number of elements in the tuple.
Syntax: min(tuple)
Parameters: tuple − This is a tuple from which min valued element to be
returned.
Returns: This method returns the elements from the tuple with minimum value.
Syntax: tuple(seq)
Parameters: seq − This is a list to be converted into tuple.
Returns: This method returns the tuple.
Syntax: any(tuple)
Syntax: sorted(tuple)
Syntax: sum(tuple)
Syntax: tuple.count(element)
Syntax: tuple.index(element)
If we attempt to access a data item with a key, which is not part of the
dictionary, we get an error as follows −
dict1 = {„Name': „Parth', „Age':11, „Class': „Fifth'}
print "dict1['Address']: ", dict1[„Address']
O/P: dict1['Address]:
Traceback (most recent call last): File "test.py", line 4, in <module>
print "dict1['Address']: ", dict1['Address']; KeyError: 'Address'
Updating Dictionary:
You can update a dictionary by adding a new entry or a key-value pair,
modifying an existing entry, or deleting an existing entry as shown below in the
simple example −
dict1 = {„Name': „Parth', „Age':11, „Class': „Fifth'}
dict1['Age'] = 12; # update existing entry
dict1['School'] = “Rotary School"; # Add new entry
print "dict1['Age']: ", dict1['Age']
print "dict1['School']: ", dict1['School']
When the above code is executed, it produces the following result −
dict1['Age']: 12
dict1['School']: Rotary School
Delete Dictionary Elements:
You can either remove individual dictionary elements or clear the entire
contents of a dictionary. You can also delete entire dictionary in a single
operation.
To explicitly remove an entire dictionary, just use the del statement.
e.g.: dict1 = {„Name': „Parth', „Age':11, „Class': „Fifth'}
del dict1['Name']; # remove entry with key 'Name'
dict1.clear(); # remove all entries in dict
del dict1 ; # delete entire dictionary
print "dict1['Age']: ", dict1['Age']
print "dict1['School']: ", dict1['School']
This produces an exception is raised because after del dict dictionary does not
exist any more.
Properties of Dictionary Keys:
Dictionary values have no restrictions. They can be any arbitrary Python
object, either standard objects or user-defined objects. However, same is not true
for the keys.
There are two important points to remember about dictionary keys −
a) More than one entry per key not allowed. Which means no duplicate key is
allowed.
e.g.: dict1 = {„Name': „Parth', „Age':11, „Name': „Rohit'}
print "dict1['Name']: ", dict1['Name']
O/P: dict1['Name']: Rohit
b) Keys must be immutable. Which means you can use strings, numbers or
tuples as dictionary keys but something like ['key'] is not allowed.
e.g.: dict1 = {['Name']: „Parth', 'Age': 11} O/P: TypeError:
print "dict1['Name']: ", dict1['Name'] list objects are unhashable
Built-in Dictionary Functions:
1) cmp(): Python dictionary method cmp() compares elements of two
dictionaries.
Syntax: cmp(a, b)
Parameters: a and b are two dictionaries in which the comparison is being done.
Returns: -1 if a<b, 0 if a=b 1 if a>b
Syntax: len(dictionary)
Parameters: dictionary − This is a dictionary for which number of elements to be
counted.
Returns: This method returns the number of elements in the dictionary.
e.g.: dict1 = {"Name":"Kajal","Age":10}
dict2 = {"Name":"Harsha","Age":15 ,"City":"Mumbai"}
print “Number of elements in dict1: ”, len(dict1)
print “Number of elements in dict2: ”, len(dict2)
O/P: Number of elements in dict1: 2
Number of elements in dict2: 3
Built-in Dictionary Functions:
3) str(): Python dictionary method str() produces a printable string representation
of a dictionary.
Syntax: str(dictionary)
Returns: This method returns string representation.
Syntax: type(dictionary)
Returns: This method returns the type of the passed variable.
e.g.: d1 = {"Name":"Kajal","Age":10}
l1 = ["Komal",15]
s1 = "Pooja"
print type(d1) O/P: <type 'dict'>
print type(l1) O/P: <type 'list'>
print type(s1) O/P: <type 'str'>
Built-in Dictionary Methods:
1) clear(): Python dictionary method clear() removes all items from the
dictionary.
Syntax: dict.clear()
Returns: This method does not return any value.
Syntax: dict.copy()
Returns: This method returns a copy of the dictionary.
Syntax: dict.has_key(key)
Parameters: key − This is the Key to be searched in the dictionary.
Returns: This method return true if a given key is available in the dictionary,
otherwise it returns a false.
Syntax: dict.items()
Returns: This method returns a list of tuple pairs.
Syntax: dict.keys()
Returns: This method returns a list of all the available keys in the dictionary.
Syntax: dict.values()
Returns: This method returns a list of all the values available in a given dictionary.
Syntax: dict1.update(dict2)
Parameters: dict2 − This is the dictionary to be added into dict1.
Returns: This method does not return any value.