chapt3
chapt3
Unit Outcomes :
1) Write python program to use and manipulate lists
for the given problem.
2) Write python program to use and manipulate Tuples
for the given problem.
3) Write python program to use and manipulate sets
for the given problem.
4) Write python program to use and manipulate
Dictionaries for the given problem.
Lists
• It is a sequence of values written in asquare
bracket and separated by commas.
• For example
• L1=[‘AAA’,’BBB’,’CCC’]
• L2=[10,20,30,40]
• Here L1 and L2 are lists data structures.
Accessing values in Lists
for i in enumerate(a):
print(i)
(0, 'u')
(1, 'x')
(2, 'y')
List concatenation
• [12]*4
output :- [12, 12, 12, 12]
• [1,2,3]*5
output :- [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Finding length of a list
• The len() function is used to find the no of
elements present in the list.
• a=[1,2,3,4,5,6,7]
• a
• [1, 2, 3, 4, 5, 6, 7]
• print(len(a))
• 7
List Methods
1) append
This method adds elements at the end of the
list.
a=[10,20,30,40]
a.append(50)
a
[10, 20, 30, 40, 50]
List Methods
2) extend
This method takes the list as an argument and
appends this list at the end of old list.
a=[1,2,3]
b=[4,5,6]
a.extend(b)
a
[1, 2, 3, 4, 5, 6]
List Methods
3) Sort
This method arranges the elements in increasing order.
a=[‘x’,’y’,’u’,’v’,’w’,’z’]
a.sort()
a
[‘u’,’v’,’w’,’x’,’y’,’z’]
2) Concatenation()
The + operator is used to concatenate two tuples.
T1=(1,2,3)
T2=(4,5)
print(T1+T2)
(1,2,3,4,5)
Basic Tuple Operations
3) Repetition
The * operator is used for repetition of the elements.
T1=(1,2,3)
print(T1*3)
(1,2,3,1,2,3,1,2,3)
4)Membership
It means checking if the element is present in the tuple or not.
The membership operation is performed using in operator.
T1=(1,2,3,4,5)
print (3 in T1)
True
print(6 in T1)
false
Built in tuple Function
1)cmp(t1,t2)
This function compares tuple t1 and t2
It will return -1 if t1<t2
0 if t1=t2
1 if t1>t2
2) max(t1)
It returns maximum value from the given tuple t1.
3) Min(t1)
It returns minimum value from the given tuple t1.
Built in tuple Function
4) tuple(sequence)
It converts the list into tuple.
l=[1,2,3]
t=tuple(l)
print (t)
(1, 2, 3)
t1=tuple("python")
>>> print(t1)
('p', 'y', 't', 'h', 'o', 'n')
Sets
-> Sets are data structures that, follows
following rules.
1) Items in a set are unique.
2) Items in a set are not ordered.
color=set(['red','green','blue'])
print(color)
{'red', 'green', 'blue'}
Accessing values in set
We can not access the values in set using index as the set is
unordered and has no index.
It is possible to iterate through each item of a set using for loop.
s={'a','b',3,4,"amit"}
for i in s:
print(i)
3
4
b
a
amit
Deleting values in set
• For removing item from the set either remove or
discard method is used.
• del keyword is used to delete the set completely.
s={10,20,30,40,50}
s.remove(40)
print(s)
{10, 50, 20, 30}
s.discard(20)
print (s)
{10, 50, 30}
Deleting set
del s
print(s)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module> print(s)
NameError: name 's' is not defined
Updating values in set
• Once the set is created, we can not change the values
in the set. But we can add elements to the set by using
add() method.
• By using update() method more than one element can
be added to the set.
s={10,20,30,40,50}
s.add(25)
print(s)
{40, 10, 50, 20, 25, 30}
s.update([35,45])
print(s)
{35, 40, 10, 45, 50, 20, 25, 30}
Basic Set Operations
1) Union
The union between two sets, results in a third set with all the
elements from both sets.
It is performed using the operator |.
Example :
>>> s={1,2,3}
>>> s1={2,3,4}
>>> s2=s|s1
>>> print(s2)
{1, 2, 3, 4}
>>> s3=s.union(s1)
>>> print(s3)
{1, 2, 3, 4}
>>>
Basic Set Operations
2) Intersection
-> The intersection of two sets is a third set in which
only common elements from both the sets are enlisted.
-> It is performed using & operator.
>>> set1={1,2,3,4,5,6}
>>> set2={4,5,6,7,8,9}
>>> set3=set1&set2
>>> set3
{4, 5, 6}
Basic Set Operations
3) Difference
-> Difference of A and B i.e.(A-B) is a set of
elements that are only in A but not in B.
-> Similarly B-A is a set of elements in B but not
in A.
->The operator – is used for difference.
-> The method difference is also used for
specifying the difference.
Basic Set Operations
3)Difference
>>> A={1,2,3,4,5,6}
>>> B={4,5,6,7,8,9}
>>> C=A-B
>>> C
{1, 2, 3}
>>> B-A
{8, 9, 7}
>>> A.difference(B)
{1, 2, 3}
Basic Set Operations
4) Symmetric difference
-> It is a set of elements in both A and B except those that
are common in both.
-> It is performed using ^ operator.
-> The symmetric_difference() method can also be used.
>>> A={1,2,3,4,5,6}
>>> B={4,5,6,7,8,9}
>>> C=A^B
>>> C
{1, 2, 3, 7, 8, 9}
>>> A.symmetric_difference(B)
{1, 2, 3, 7, 8, 9}
Built in Set function
1)all()
It returns true if all elements of the set are true.
It also returns true if the set is empty.
2) any()
It returns true if any element of the set is true. If set is empty it returns false.
3) len()
It returns the length of the set.
4)max()
It returns maximum value present in the set.
5) min()
It returns minimum value present in the set.
6) sorted()
It returns a new sorted list from the elements.
Sorted(s1,reverse=True)
7) sum()
It returns the sum of all elements in the set.
Built in Set function
>>> print(all(set1))
True
>>> print(any(set1))
True
>>> set2={}
>>> print(all(set2))
True
>>> print(any(set2))
False
Dictionaries
• It is unordered collection of items.
• These items are in the form key-value pairs.
• Dictionary always represent the mappings of
keys with values.
>>> marksdictionary={'m1':85,'m2':90,'m3':94}
>>> print(marksdictionary.values())
dict_values([85, 90, 94])
>>>
Built in Dictionary Functions
6) pop()
It removes and returns an element from a
dictionary having the given key.
>>> student
{'rollno': 10, 'name': 'Aakash', 'marks': 90}
>>> val=student.pop('rollno')
>>> val
10
>>>
Programs
Output :
RESTART: C:/Users/Lab 13-25/AppData/Local/Programs/Python/Python37-
32/clist1.py
['red', 'blue', 'yellow', 'orange']
sorted list based on keys
blue
orange
red
yellow
>>>