Freshers Python Coding interview Question
1. How many modules are used in your projects
2. Difference between List and Tuple at least 3 differences
3. Difference between Set and Tuple at least 3 differences
4. Difference between Set and List at least 3 differences
5. Difference between append() and extend() at least 2 differences
6. Define Dictionary in Python and its behaviours.
7. What is the lambda function and its uses
8. Difference between Generator and Iterator
9. What is map and filter functions in python
10.What is recursion explain with Example(Feb series)
11. How to achieve Overloading in python and one example
12. What is List Comprehension
13.Difference between list Comprehension and lambda function
14.What is slicing in python
15.What is __init__(self) in python
Freshers Python Coding interview Question & Answers
1. How to find a particular word in a string and calculate word length.
Source code:
import re
y=”particular word in python”
x=re.findall(“word”,y)
print(x)
i=re.findall(“p+”,y)
print(len(i))
2. Write a program as follows
# Input list [4,5,6,3,2,1]
# Output list [5,6,-1,-1,-1,-1]
Source code:
def fun(l,n):
ls=[]
for i in range(0,n):
n1=l[i]
try:
n2=l[i+1]
except:
n2 = -1
if n1 < n2:
ls.append(n2)
else:
ls.append(-1)
return ls
l=[4,5,6,3,2,1]
n=6
print(fun(l,n))
3. Write a program to print duplicate elements in key and values pair (dictionary)
# input: l=[1,1,2,2,3,2,3,1,4,4,5,2,3,5]
#output: {1: 3, 2: 4, 3: 3, 4: 2, 5: 2}
Source code:
l=[1,1,2,2,3,2,3,1,4,4,5,2,3,5]
d={}
for i in l:
if i in d:
d[i]+=1
else:
d[i]=1
print(dict(sorted(d.items())))
(or)
import collections
l=[2,3,2,3,4,5,5,5,6,6]
l1=collections.Counter(l)
print(l1)
4. Write a program and find the duplicate characters and find length and sort alphabet format.
# input: “aabbbccde”
# output: b 3
a 2
c 2
Source code:
d = “aabbbccde”
l = {}
for i in d:
if i in l:
l[i]+=1
else:
l[i] = 1
try:
k = sorted(l,key = l.get,reverse = True)
for j in k:
if l[j] ==1:
l.popitem()
else:
print(j,l[j])
except KeyError:
Pass
5. Print below pattern
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Source code:
n=5
for i in range(n,0,-1):
number=i
for j in range(0,i):
print(number, end=” “)
print(“”)
for k in range(2,n+1,1):
number1=k
for l in range(0,k):
print(number1,end=” “)
print(“”)
6. Write a program to find divisible by 9 elements in list (use lambda function only)
Source code:
n=[3,4,21,9,35]
res=list(filter(lambda x: (x%9 ==0),n))
print(res)
7. Solve below snippet
Input: l=[4,3,2,1]
Output : 4a3a2a1
Source code:
l=[4,3,2,1]
print(‘a’.join(str(item) for item in l))
8. Write a program to find Fibonacci number
Source code:
def fun1(n):
if n<0:
print(“invalid number”)
elif n==0:
return 0
elif n==1 or n==2:#0,1,1,
return 1
else:
return fun1(n-1)+fun1(n-2)# 3-1 + 3-2= 3,
print(fun1(13))
9. Write a program to find divisible by 9 numbers
Source code:
n=[3,4,21,9,35]
res=list(filter(lambda x: (x%9 ==0),n))
print(res)
10. Write a program to find Mean
Source code:
# mean
l=[2,3,5,6,8,5,5,8,7]
mean = sum(l)/len(l)
print(mean)
11. Write a program to find Median
Source code:
# Median
l=[2,3,5,1,7,5,5,8,7]
data = len(l)//2
l.sort()
if len(l) % 2 == 1:
median = l[data]
else:
median = (l[data – 1]+l[data])/2
print(median)
12. Write a program to find Mode
Source code:
# Mode
l=[2,3,5,1,7,5,5,8,7]
mode = max(set(l),key=l.count)
print(mode)
13. Write a program Sort a list According to the length of the elements in python
Source code:
a = [‘like’, ‘love’, ‘sad’, ‘loveyou’]
a.sort(key=len)
print(a)
14. Write a program to Count the words in a string
Source code:
s = “hi am vijay from mallapuram”
count = len(s.split())
print(count)
15. Write a program to get numbers in string
Source code:
# get numbers in string
s = “0i2 l4ove i7ndia”
number= []
for i in s:
if i.isdigit():
number.append(int(i))
print(number)