1. Chapter 2
Revision of the Basics of Python - II
Class – XII
Subject: Computer Science
2. Tuples
Its Sequence of values of any data type enclosed
in round brackets.
Tuples are Immutable .
Examples:-
() is a tuple with no element
(6, ) is a tuple with one element
(‘a’, 2, 5.77) is a tuple of mixed data types
3. Creating Tuples
By enclosing values in parentheses
A = (1,2,3)
B = (“Ram”, “Sham”, “Anil”)
Using function tuple(<any sequence>)
A = tuple(“WELCOME”)
B=tuple([1,2,3,4])
C = tuple(input(“enter any sequence”))
Using eval()
Most common method used to create tuple
A = eval(“(1,2,3,4)”)
4. Similarities with Lists
len() function
Returns number of elements in tuple and list both
Indexing and slicing
Indexing and Slicing is same in list and tuple
Membership operators
‘in’ and ‘not in’ operators work similarly for tuple and lists
+ and * operators
Concatenation and Replication
Traversing
Traversing list and tuple is same
for <Item> in <tuple/list> :
Process <item>
5. Similarities with Lists
Unpacking
Unpacking is same for lists and tuples
E.g. A = (1,2,3,4)
X,Y,Z,W = A
Count() method
Count() method works similarly for lists and tuples
6. Differences with Lists
As tuple is immutable, Changing tuple elements
is not allowed
e.g. A= (1,2,3)
A[2] = 4 # this is not allowed
Adding or deleting elements in tuple is not
allowed
max() and min() functions for tuples
max(<tuple>) gives maximum value in tuple
min(<tuple>) gives minimum value in a tuple
max() and min() works only if all the elements of
tuple are of same data type
7. Copying A list
How to create a copy of a List?
Lets say we have a list
L = [1,2,3,4]
How to create a copy L_Copy of this
list L???
L_Copy = list(L)
8. List Reverse and Sort
Function List.reverse() is used to reverse a list
Function List.sort() is used to sort a list
By default sorting is done in which order?
Increasing order
How to do sorting in decreasing order ?
List.sort(reverse = True)
9. Dictionary
Dictionary in python is an unordered pair of keys:
values
Dictionary is not a sequence, its not indexed by
using numbers
Dictionary is created using curly braces ( { } )
Dictionary is indexed by using Keys
Dictionary keys are immutable and all the keys
should be unique
Dictionary is a mutable data object, Its values can
be changed (not keys)
10. How to create Dictionary
Simply by enclosing Key:Value pairs in curly
braces
D1 = { 1 : “David” , 2 : “Rick , 3: “Kiya” }
D2 = {} #this is an empty dictionary
By adding key:value pairs in an empty dictionary
D2 = {}
D2[1] = ‘A’
It will create dictionary D2 = {1:’A’}
11. Creating dictionary : dict() function
Dictionary can be created by passing key and value
pairs to dict() function. There are multiple ways to do
so—
1. By passing arguments and values
D1 = dict(rollno = 1, name = ‘Ajay’, marks = 90)
2. By passing keys and values using zip function
D1 = dict(zip(‘rollno’,’name’,’marks’ ), (1,’Ajay’,90))
3. By passing key:value pair in the form of sequence
D1 = dict([[‘rollno’ ,1],[‘name’,’Ajay’],[‘Marks’,90]]
12. Traversing Dictionary
for <Item> in <Dictionary>
Process <Item>
#Here <Item> will be assigned the keys of dictionary
D1 = {1 : ‘A’, 2: ‘B’, 3 : ‘C’ }
for i in D1:
print( i , D1[i}
13. Adding and Updating elements of
Dictionary ? How?
D1 = {‘RollNo’ : 1, ‘Name’ : ‘Ajay’}
How to add new key value pair as “Marks” : 90??
D1[“Marks”] = 90
How to update Marks to 95??
D1[“Marks”] = 95
14. Deleting elements of dictionary
1. using del command
del <dictionary>[<key>]
If <key> does not exist in <dictionary> python will throw error
del D1[‘Marks’]
del D1 #It will delete entire D1 dictionary object
2. Using pop() method
<dictionary>.pop(<key>)
D1.pop(‘Marks’)
If <key> does not exist , python will throw error
But you can give your own error message
D1.pop(‘Marks’, “Not Found”)
15. Membership Operators for Dictionary
How in and not in operator works for dictionaries?
in and not in operator checks for existence of keys
in dictionary
in operator will return True if given KEY exist in
dictionary
not in operator will return True if given KEY does
not exist in dictionary
16. Methods for Dictionaries
1. len(<dictionary>) : It gives length of
dictionary(number of pairs )
2. <dictionary>.clear(): It gives empty dictionary
3. <dictionary>.get(<key>) : to get value for a Key
4. <dictionary>.items(): it gives sequence of
tuples(key, value)
5. <dictionary>.keys() : It returns all keys as a list
6. <dictionary>.values(): It returns all values a a list
7. <dictionary>.update() : this method merges two
dictionaries