Tuesday, November 19, 2019

sort method in python

a1=[30,5,14,8,0]
a1.sort()
print(a1)

output:
[0, 5, 8, 14, 30]

example2:

a1=[30,5,14,8,0]
a1.sort()
print(a1)
a2=["bat","ant","dog","cat"]
a2.sort()
print(a2)

output:
[0, 5, 8, 14, 30]
['ant', 'bat', 'cat', 'dog']

example3:
if upper case strings and lowercase strings are present then it will shows the uppercase string first then it will shows the lower case strings

a1=[30,5,14,8,0]
a1.sort()
print(a1)
a2=["Bat","ant","Dog","Cat"]
a2.sort()
print(a2)

output:
[0, 5, 8, 14, 30]
['Bat', 'Cat', 'Dog', 'ant']

example 4:
strings and integers with in list then they will not sorted .compiler will shows the error

a1=[10,'B',20,'C',4,'A']
a1.sort()
print(a1)

output:
TypeError: '<' not supported between instances of 'str' and 'int'

reverse method in python



l1=[10, 30, 40, 50, 60, 70]
l1.reverse()
print(l1)

output:
[70, 60, 50, 40, 30, 10]

pop method in python

pop method removes the last element of the python and also return the which element is deleted

l1=[10, 30, 40, 50, 60, 70]
print(l1.pop())
print(l1)
print(l1.pop())
print(l1)

output:
70
[10, 30, 40, 50, 60]
60
[10, 30, 40, 50]

remove method in python

l1=[10,20,30,40]
print(l1)
l1.remove(20)
print(l1)

output:
[10, 20, 30, 40]
[10, 30, 40]

example 2:

l1=[10,20,30,40,50,60,70]
x=int(input("enter elements to be removed"))
if x in l1:
    l1.remove(x)
    print("removed successfully")
    print(l1)
else:
    print("specified element is not available")

output:
enter elements to be removed10
removed successfully
[20, 30, 40, 50, 60, 70]

enter elements to be removed20
removed successfully
[10, 30, 40, 50, 60, 70]

enter elements to be removed82
specified element is not available


append and extend methods in python


l1=[10,20,30]
l2=[40,50,60]
l1.extend(l2)
print(l1)
l1.extend('anil')
print(l1)
l1.append('anil')
print(l1)

output:
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 'a', 'n', 'i', 'l']
[10, 20, 30, 40, 50, 60, 'a', 'n', 'i', 'l', 'anil']

extend method in python

l1=["chicken","mutton","fish"]
l2=["biryani","fry","roast"]
l1.extend(l2)
print(l1)
print(l2)

output:
['chicken', 'mutton', 'fish', 'biryani', 'fry', 'roast']
['biryani', 'fry', 'roast']


example 2:
l1=["chicken","mutton","fish"]
l2=["biryani","fry","roast"]
l1.extend(l2)
print(l1)
print(l2)
l2.extend(l1)
print(l2)

output:

['chicken', 'mutton', 'fish', 'biryani', 'fry', 'roast']
['biryani', 'fry', 'roast']
['biryani', 'fry', 'roast', 'chicken', 'mutton', 'fish', 'biryani', 'fry', 'roast']

example3:
If we are adding a string using extend method then extire string added to given list but individual character.

l1=[10,20,30]
l2=[40,50,60]
l1.extend(l2)
print(l1)
l1.extend('anil')
print(l1)

output:
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 'a', 'n', 'i', 'l']

insert method in python

l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
l.insert(1,50)
print(l)

output:
[10, 20, 30, 40]
[10, 50, 20, 30, 40]

example 2:

l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
l.insert(1,50)
print(l)
l.insert(50,888)
print(l)
l.insert(-20,333)
print(l)

output:
[10, 20, 30, 40]
[10, 50, 20, 30, 40]
[10, 50, 20, 30, 40, 888]
[333, 10, 50, 20, 30, 40, 888]

l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
l.insert(1,50)
print(l)
l.insert(50,888)
print(l)
l.insert(-20,333)
print(l)
print(l.index(888))
print(l.index(333))

output:
[10, 20, 30, 40]
[10, 50, 20, 30, 40]
[10, 50, 20, 30, 40, 888]
[333, 10, 50, 20, 30, 40, 888]
6
0



write a program in python to append all even number upto 100 which are divisible by 10

write a program in python to append all even number upto 100 which are divisible by 10

l=[]
for x in range(101) :
    if x%10==0:
        l.append(x)
    else:
        x=x+1
print(l)
 
output:
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

or
l=[]
for x in range(0,101,10) :
        l.append(x)
print(l)


output:
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

write a program in python to append all even number upto 100


l=[]
for x in range(101) :
    if x%2==0:
        l.append(x)
    else:
        x=x+1
print(l)
 
output:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]


write a program in python to append all odd numbers upto 100

l=[]
for x in range(101) :
    if x%2==1:
        l.append(x)
    else:
        x=x+1
print(l)

output:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]

append method in python

single element adding by using append method
l=[]
l.append(10)
print(l)

output:
[10]


multiple methods adding by using append method
l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)

output:
[10, 20, 30, 40]

try and except method in python

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

output
enter the value to search20
20 available and its first occurence is at: 1


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


enter the value to search50
50 not available

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

python class topic video