28 May Class With Chapter 2 List and Tuple
28 May Class With Chapter 2 List and Tuple
list
=> collection of elements covered in []
=> list is mutable=> can change the element in a place.
l1=[10,20,30,40,50,60]
0 1 2 3 4 5 forward index
-6 -5 -4 -3 -2 -1 backward index
print(l1[0]) => 10
print(l1[1]) => 20
print(l1[2]) => 30
print(l1[3]) => 40
print(l1[4]) => 50
print(l1[5]) => 60
l1=[10,20,30,40,50,60]
for i in range(0,len(l1)):
print(l1[i])
## with out using range
for i in l1:
print(i)
"""
len() => to return the no of elements in a list
range(0,len(l1)) => range(0,6) => 0,1,2,3,4,5
i=0 => l1[0]=> 10
i=1 => 20
i=2 => 30
i=3 => 40
i=4 => 50
i=5 => 60
output
10
20
30
40
50
60
10
20
30
40
50
60
"""
3) write a python program to increment the given
list into 5
l1=[10,20,30,40,50]
the output shoule be
[15,25,35,45,55] => for loop with range
solution:
l1=[10,20,30,40,50,60]
for i in range(0,len(l1)):
l1[i]=l1[i]+5
print(l1)
"""
[15, 25, 35, 45, 55, 65]
"""
solution:
n=int(input("enter n value"))
l1=[]
for i in range(1,n+1):
no=int(input("enter the no"))
l1.append(no)
print(l1)
noe=0
noo=0
for i in l1:
if i%2==0:
noe+=1
else:
noo+=1
print("no of odd no=",noo)
print("no of even no=",noe)
"""
l1=[15,24,3,6,8,7]
noe => 3
noo => 3
"""
len()
max()
min()
sum()
list - functions
1. append()
2. insert()
3. extend()
4. pop()
5. pop(index)
6. remove()
7. del
8. clear()
9. sort()
10. index()
11.count()
12. reverse()
13.
1. example append(),insert(),extend()
append() => to insert element or list into last postion
insert() => to insert element in a particular position
extend() => to add list elements into last position
solution:
l1=[10,20,30,40,50]
print(l1)
l1.append(90)
l1.append([1,2])
print(l1)
l1.extend([5,6])
print(l1)
l1.insert(3,65)
print(l1)
"""
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50, 90, [1, 2]]
[10, 20, 30, 40, 50, 90, [1, 2],5,6]
[10, 20, 30,65, 40, 50, 90, [1, 2],5,6]
"""
l1=[10,20,30,40,50]
print(l1)
l1.remove(30)
print(l1)
"""
[10, 20, 30, 40, 50]
[10, 20, 40, 50]
"""
5) other functions
sort()
l1=[5,7,29,12,8,13,7,8,4]
print(l1)
l1.sort()
print(l1)
l1.sort(reverse=True)
print(l1)
"""
[5, 7, 29, 12, 8, 13, 7, 8, 4]
[4, 5, 7, 7, 8, 8, 12, 13, 29]
[29, 13, 12, 8, 8, 7, 7, 5, 4]
"""
6)index(),count()
output
l1=[5,7,29,12,8,13,7,8,4,12]
print(l1.index(12))
print(l1.index(12,4))
print(l1.count(12))
print(l1.index(90))
"""
3
9
2
Traceback (most recent call last):
File "C:/Users/New/AppData/Local/Programs/Python/Python37-32/ll.py", line 8, in
<module>
print(l1.index(90))
ValueError: 90 is not in list
"""
l1=[10,5,6,2,4,7]
l2=l1
for i in range(0,len(l1)):
l1[i]=l1[i]+5
print(l1)
print(l2)
calc
l2=l1 ## any change in l1 it reflected into l2
l1=[10,5,6,2,4,7]
l2=[10,5,6,2,4,7]
l1=[15,10,11,7,9,12]
l2=[15,10,11,7,9,12]
output
[15,10,11,7,9,12]
[15,10,11,7,9,12]
l1=[10,5,6,2,4,7]
l2=l1.copy()
for i in range(0,len(l1)):
l1[i]=l1[i]+5
print(l1)
print(l2)
"""
[15, 10, 11, 7, 9, 12]
[10, 5, 6, 2, 4, 7]
"""
tuple
=> tuple covered with ()
=> tuple is immutable
=> can't change the value in a place.
=> we can't change and we can't delete the tuple elements
tuple is immutable
t1=(10,20,30,40,50)
t1[0]=90 ## error index error
tuple functions
index()
count()
1. tuple adding
t1=(10,20,30,40,50)
t2=(1,2)
t1=t1+t2
print(t1)
output
(10,20,30,40,50,1,2)