Tuesday, November 19, 2019

count method in python

l=[10,20,30,40,10,20,10,10]
print(l.count(20))

output:
2


index method
it tells first occurrence of the given number

l=[10,20,30,40,10,20,10,10]
print(l.count(20))
print(l.index(10))

output:
2
0


if the value is  not there in the given string then it will show the value error

l=[10,20,30,40,10,20,10,10]

print(l.index(50))


output:

ValueError: 50 is not in list



l=[10,20,30,40,10,20,10,10]
target=int(input("enter the value to search"))
if target in l:
    print(target,"available and its first occurence is at:",l.index(target))
else:
    print(target,"not available")

output:
enter the value to search10
10 available and its first occurence is at: 0



enter the value to search30
30 available and its first occurence is at: 2


enter the value to search50
50 not available

No comments:

Post a Comment

python class topic video