Tuesday, November 19, 2019

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

Saturday, November 16, 2019

a function which is outside the method is called as method

a function  which is outside the method is called as method.
def f1():
    print("this is the function")
class student:
    def info(self):
        print("this is the method")
f1()
s=student()
s.info()

output:
this is the function
this is the method

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

python class topic video