Python Interview Programs2
Python Interview Programs2
# a = 20
# b = 10
# temp = a
# a = b
# b = temp
# print(a)
# print(b)
# a = a + b #a = 10 + 20, #a = 30
# b = a - b #b = 30 - 20, #b = 10
# a = a - b #a = 30 - 10, #a = 20
# print(a)
# print(b)
# a, b = b, a
# print(a)
# print(b)
# a="Nil"
# b="Priya"
# print("a:",a)
# print("b:",b)
a=a+b
b=a[0:len(a)-len(b)] this gives us range of sub string and not only position of
lttr
a=a[len(b):]
print("a:",a)
print("b:",b
2. How to check number is prime or not.
num=int(input('enter a number'))
if num>1:
for i in range(2,num):
if (num %i)==0:
break
else:
else:
print("")
--------------------------------------------------------------
or
if (num % i) == 0:
if is_prime:
else:
counter = 6
result = 1
nums = [5, 7, 2, 4, 9, 6]
max = nums[i]
min = nums[i]
maximum
nums = [5, 7, 2, 4, 9, 6]
max = nums[0]
max = nums[i]
print('The maximum element in the list is', max)
minimum
nums = [5, 7, 2, 4, 9, 6]
min = nums[0]
min = nums[i]
numlist = [2,4,2,5,7,9,23,4,5]
numsum=0
for i in numlist:
numsum+=i
Diff between list and array : List cannot manage arithmetic operations. Array can manage arithmetic
operations.
sum = 0;
#method-1
print(len(thislist))
#method-2
lenght = 0
for x in thislist:
lenght += 1
print(lenght)
list=[0,12,2,45,5]
list[0],list[-1]=list[-1],list[0]
#method1
list=[0,12,2,45,5]
temp=list[0]
list[0]=list[len(list)-1]
list[len(list)-1]=temp
print(list)
##method2
a=[]
for x in range(0,n):
a.append(element)
temp=a[0]
a[0]=a[n-1]
a[n-1]=temp
print(a)
return list
# Driver function
pos1, pos2 = 1, 3
#method 1
list=['greek','for','greek']
word='greek'
n=2
counter=0
if (list[i]==word):
counter=counter+1
if (counter==n):
del(list[i])
Using function
#method 2
def removeelement(list,word,n):
counter=0
if (list[i]==word):
counter=counter+1
if (counter==n):
del(list[i])
return True
else:
return False
list=['greek','for','greek']
word='greek'
n=2
# counter=0
flag=removeelement(list,word,n) ##imp
if flag==True:
print((list))
18. How to find the smallest and largest numbers on the list?
19. How to find the second largest number in a list?
Or
Prrogram to count each element,to print element which occurs nth time
Q.
dict1 = {'a':1,'b':2,'c':3}
dict2 = {'a':1,'c':3}
you have two dictionaries write a python program to find key,value pair which are present in dict1 but
not in dict2
>>
Q.you have febonachi series where element of series is missing as shown below lst.find that missing
number
L1 =[‘name’,’age’]
L2=[‘yog’,29]
Dict = {‘name’:’yog’,’age’:29}
Q .write a python program if element occurce In list nth time add I to new list