0% found this document useful (0 votes)
10 views8 pages

28 May Class With Chapter 2 List and Tuple

Uploaded by

lokeshprasaddr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

28 May Class With Chapter 2 List and Tuple

Uploaded by

lokeshprasaddr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

list and tuples => chapter 2= > 7 to 9 marks

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

2. write apython program to access the list using


for loop

1) using range() function

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]
"""

3) write a python program to get n no of list from user


and find no of odd no and no of even from the list.

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

"""

4)write a python program to get list from user


and check the given no is even no to increment by 5
and check the given no is odd no to increment by 7
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)
for i in range(0,len(l1)):
if l1[i]%2==0:
l1[i]=l1[i]+5
else:
l1[i]=l1[i]+7
print(l1)
"""
enter n value5
enter the no2
enter the no4
enter the no3
enter the no5
enter the no6
[2, 4, 3, 5, 6]
[7, 9, 10, 12, 11]
"""

sequence common function => list,tuple,dictinary,string

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]
"""

5) example for pop(),remove(),clear()

pop() => to remove the last element in a list


pop(index) => to remove element in a particular index
note
remove() => to remove particular element
clear() => to clear all elements from the list
del => to delete the particular index element or
to delete the entire list.
example
l1=[10,20,30,40,50,60]
print(l1)
l1.pop()
print(l1)
l1.pop(2)
print(l1)
del l1[0]
print(l1)
l1.clear()
print(l1)
del l1
print(l1)
"""
[10,20,30,40,50,60]
[10,20,30,40,50]
[10,20,40,50]
[20,40,50]
[]
error l1 is not define
"""

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
"""

1)predict the output

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]

2. predict the output


1)predict the output

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]
"""

l2=l1 ## l1, l2 which contains same address


l2=l1.copy() ## l1,l2 has separate address

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)

tmrw => test 2 pm


topic => sql
list,tuple

You might also like