Friday, November 15, 2019

list in python

l=[10,20,30,40]
print(l[0])
print(l[-4])


output:
10
10

example 2:
l=[10,20,30,40]
print(l[0])
print(l[-1])

output:
10
10

example3:
l=[10,20,[30,40]]
print(l[2])
print(l[-1])

output:
[30, 40]
[30, 40]

example4:
l=[10,20,[30,40]]
print(l[2][0])
print(l[2][1])

output:
30
40



list elements printing using while loop

list=[1,2,3,4,5,6,325,7,8]
i=0
while i<len(list):
    print(list[i])
    i=i+1

output:
1
2
3
4
5
6
325
7
8

list elements printing using for loop

list=[1,2,3,4,5,6,7,8,9]
for x in list:
    print(x)

output:
1
2
3
4
5
6
7
8
9

printing the even places of the list elements
list=[1,2,3,4,5,6,7,8,9]
for x in list:
    if x%2==0:
        print(x)

output:
2
4
6
8

No comments:

Post a Comment

python class topic video