Chapter 1: Python Revision Tour 4
Tuple and Dictionary in Python Statement
Tuple in Python:
What is Tuple:
Tuple is a sequence of immutable Python object. Tuples are sequences, just like lists.
Tuples use parentheses and lists use square brackets.
Tuple can hold elements of different data type (Heterogeneous).
Tuples are immutable it means we cannot perform insert, update and delete operation on
them. So, iterating through a tuple is faster as compared to a list.
Example: tuple= (‘mango’,’apple’,’grapes’)
Example: List= [‘mango’,’apple’,’grapes’]
Tuple creation:
Empty tuple: t=()
Tuple with one element: t=(10,)
Tuple with one element: t=10,
Tuple with multiple value: t=(10, “Hello”,20,”Kanpur”)
Tuple with multiple value: t= 10, “Hello”,20,”Kanpur”
Creating tuple using function tuple()
Nested Tuple:
Example: fruits= (10 , 20 , (‘Ram’ , ’Mohan’) , 40 , 50 , [‘Red’ , ’blue’] , 60)
Accessing a Tuple and Nested Tuple:
Traversing a Tuple:
Traversing a tuple means accessing each element of a tuple. This can be done by using
either for or while looping statement.
Common Tuple operations:
Tuple Slicing: Slicing is used to retrieve a subset of values.
Syntax: tuple_name[start:stop:step]
Tuple Addition/Concatenation/joining:
Tuple Multiplication/Repetition
Membership Operator like ‘in’ and ‘not in’
Comparing Tuples
Tuple Functions:
Built-in-functions and Methods of Tuple
len() count() any()
min() max() sorted()
index() del statement
Some features of a tuple:
You can't add elements to a tuple because of their immutable property. There's no
append() or extend() method for tuples.
You can't remove elements from a tuple, also because of their immutability. Tuples
have no remove() or pop() method.
You can find elements in a tuple since this doesn't change the tuple.
You can also use the in operator to check if an element exists in the tuple.
To change a tuple you can unpack it first, then change the value and pack it again.
You can use list() and tuple() methods to change the values of a tuple.
Tuples use less memory whereas lists use more memory.
Dictionary in Python:
What is Dictionary:
A python dictionary is a mapping of unique keys to values. It is a collection of key-value
pairs. Dictionaries are mutable which means they can be changed.
Important features of dictionaries are:
Each key map to values. It’s called key value pair.
Each key is separated from its values by a colon(:), the items are separated by
commas, and the entire dictionary is enclosed in curly braces {}.
Keys are unique within a dictionary while values may not be.
The value of a dictionary can be of any type, but the keys must be of an immutable
data types.
Creating a Dictionary:
Empty dictionary:
d={}
d=dict()
Adding an item in Dictionary:
D={}
D[‘input’]=’Keyboard’
D[‘Output’]=’Printer’
D[‘Language’]=’Python’
D={‘input’: ’Keyboard’, ‘Output’:’Printer’, ‘Language’=’Python’}
Accessing a Dictionary:
To access dictionary elements, you can use the square brackets along with the key to obtain
its value.
print (D[‘Output’])
It will give out: ‘Printer’
Traversing a Dictionary:
Traversing a dictionary means accessing each element of a Dictionary. This can be done by
Using either for or while looping statement.
Common Dictionary operations:
Appending values to Dictionary, means add new elements to exiting dictionary.
Updating Elements in a Dictionary
Membership Operators ‘in’ and ‘not in’
Removing an item from Dictionary
Built-in-functions and Methods of Dictionary
len() clear() get()
items() keys() values()
fromkeys() copy() popitem()
Max() min() sorted()