Python Interview Questions - 2022
What is Python? What are the benefits of using Python?
Python is a open-source, high-level, interpreted, general-purpose programming language. Python has several advantages:





What are the key differences between lists and tuples?
The main difference between the tuples and lists is that the tuples are immutable objects and lists are mutable. So, tuples cannot be modified while the lists can be modified. Tuples are more memory efficient than the lists and hence are typically faster
How to remove duplicates from a list?
Starting with Python 3.7, dictionary is insertion-ordered. So, dict.fromkeys(mylist) will remove the duplicates from a list preserving the original order of the list.
items = [1, 2, 0, 1, 3, 2] list(dict.fromkeys(items))
[1, 2, 0, 3]
items = [1, 2, 0, 1, 3, 2] list(set(items))
[0, 1, 2, 3]
What is the difference between a module, a package, and a library?
A module is a Python file that’s intended to be imported into scripts or other modules. It can contains functions, classes, and global variables.
A package is a collection of modules that are grouped together inside a folder to provide consistent functionality. Packages can be imported just like modules. They usually have a __init__.pyfile in them that tells the Python interpreter to process them as such.
A library is a collection of packages.
What are the main differences between shallow copy and deep copy?
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances)Shallow copying duplicates as little as possible. A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
Deep copying replicates the original at a different memory location, so the copy is totally independent with no connection to the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. So, any changes made to the copy do not reflect in the original and any changes made to the original do not reflect in the copy.
How to remove all whitespace from a string?
All whitespace characters from a string can be removed by using replace method replacing whitespace with null string
How to sort elements of a tuple?
Python tuples do not have the sort method. So, to sort the elements of a tuple, the sorted builtin function can be used by passing the tuple as the argument. The function returns a list with the items of tuple sorted in ascending order. This list can be converted into a tuple by using tuple( )
The default sorting order is ascending order. The reverse=True parameter sorts the items in descending order.
How is string interpolation performed in Python?
String interpolation is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
There are three common ways to interpolate strings in Python:



What are the two methods available for a tuple
Python tuples have two built-in methods:


How to find the number of occurrence of an element in a tuple?
my_tuple.count(element) will give the number of occurrence of an element in my_tuple
How to create a tuple?
An empty tuple can be created by using tuple constructor, tuple( ) or by using a pair of parentheses with no values in them.A tuple with just one element (singleton tuple) can be created by using a trailing comma after the object or by using a pair of parentheses with the object in them.
A tuple with more than one element can be created by elements separated by commas or by using a pair of parentheses with the elements separated by commas in them.
How to delete a tuple?
del.my_tuple would delete my_tuple
How to add an element to a tuple?
Since tuples are immutable, elements cannot be added/modified/deleted from a tuple.
How to find the index of specific element in a tuple?
my_tuple.index(element,start,end) would retun the position of the first occurrence of the element in the tuple and If the element is not found, ValueError is raised.
How to compare two tuples to see if they have the same elements?
Python's python inbuilt cmp method can be used to compare two tuples to see if they have the same elements
Can a tuple be a member in another tuple?
Yes, tuples can be nested to any depth needed. tup1 = (1, 2, 3)
tup2 = ('gk', 'nxt')
nested_tuple = (tup1, tup2)
How to convert a tuple into a list?
A tuple can be converted into a list by using the list constructor with the tuple as the argumentlist(my_tuple)
How to convert a list into a tuple?
A list can be converted into a tuple by using the tuple constructor with the list as the argumenttuple(my_list)
How to remove duplicate characters from a string?
Starting with Python 3.7, dictionary is insertion-ordered. So, dict.fromkeys(mylist) will remove the duplicate characters from the string preserving the original order of the characters in the string. Creating a list with the dict keys and joining the individual characters into a string using the join function will give the string with duplicates removed.
magic = 'abracadabra' ''.join(list(dict.fromkeys(magic)))
'abrcd'
magic = 'abracadabra' ''.join(list(set(magic)))
'dracb'
What is a list?
A list is a mutable, ordered collection of objects. The objects can be of any type and need not be unique. Lists can be constructed by enclosing comma separated objects in a pair of square brackets or by using list constructor, list(iterator). An empty list can be constructed by using a pair of square brackets ([]).
What is a tuple?
A tuple is an immutable, ordered collection of objects. Python’s creator intended tuples for heterogenous data. Tuples are Python's way of packaging heterogeneous pieces of information in a composite object.
For example, socket = ('www.python.org', 80) brings together a string and a number so that the host/port pair can be passed around as a socket, a composite object
Which Python statement can be used in places where a statement is required syntactically but the program requires no action?
In Python, pass is both a keyword and statement. It's just a place-holder and will be ignored by Python as pass requires no action
Can a tuple be a member in itself?
Yes, but it will create a circular reference
Does python support switch or case statement?
No, python does not support switch or case statement. A set of if-elif-else statements can be used to get that functionality
What is the difference between a list and a tuple? Answer |
What is the difference between deep copy and shallow copy? Answer |
What is the difference between append and extend? Answer |
What is the difference between a function and a method? Answer |
What is the difference between sort and sorted? Answer |
What is the difference between an array and a list? Answer |
What is the difference between is and ==? Answer |
What is the difference arange and linspace? Answer |
What is the difference arange and range? Answer |
What is the difference args and kwargs? Answer |
What is the difference argument and parameter? Answer |
What is the difference between / and //? Answer |
What is the difference between break and continue? Answer |
What is the difference between break and pass? Answer |
What is the difference between list and set? Answer |
What is the difference between list and dictionary? Answer |
What is the difference between set and frozenset? Answer |
What is the difference between for loop and while loop? Answer |
What is the difference between datetime.now() and datetime.today()? Answer |
What is the difference between array and matrix? Answer |
What is the difference between mutable and immutable? Answer |
What is the difference between pop and remove? Answer |
What is the difference between print and return? Answer |
What is the difference between map and filter? Answer |
What is the difference between append and insert? Answer |
What is the difference between bitwise and logical operators? Answer |
What is the difference between break and return? Answer |
What is the difference between del and pop? Answer |
What is the difference between error and exception? Answer |
What is the difference between sinle quotes, double quotes and triple quotes? Answer |
What is the difference between else and elif? Answer |
What is the difference between expression and statement? Answer |
What is the difference between find and index? Answer |
What is the difference between global and local variable? Answer |
What is the difference between global and nonlocal variable? Answer |
What is the difference between eval and exec? Answer |
What is the difference between keyword and identifier? Answer |
What is the difference between keyword and variable? Answer |
What is the difference between reverse and reversed? Answer |
What is the difference between quit and exit? Answer |
What is the difference between yield and return? Answer |
What is the difference between ravel( ) and flatten( ) and reshape( )? Answer |
What is the difference between queue and dequeue? Answer |
What is the difference between generator and iterator? Answer |
What is the difference between zip and map? Answer |
What would be the output of executing the following code:
d, e, f = {}, dict(), dict(())
d['one'] = 1.0; d['two'] = 2.0; d['three'] = 3.0
e = dict([('two', 2/1), ('three', 3/1), ('one', 1/1)])
f = dict(zip(['three', 'one', 'two'], [3, 1, 2]))
print(d == e and e == f and d == f)
Answer
|
What would be the output of executing the following code:
b = [['1'] * 2] * 3
b[1][1] = 'O'
print(b)
Answer
|
Which line of code should replace '# Replace this line' in the following code snippet to get the result: 7
def f1(a=1,b=2,c=3): return a+b+c
v = {"a":6, "c":-1}
# Replace this line
Answer
|
What would be the output of executing the following code:
c = 50 + 50
print(c in [50, 100, 50.0, 100.0] == True)
Answer
|
What would be the output of executing the following code:
a = {1,2,6,8}
b = {2,3,5,8}
print((a) - (b))
Answer
|
What would be the output of executing the following code:
print(0.0 or 0)
Answer
|
What would be the output of executing the following code:
lang = ['Julia', 'R', 'Python']
src = 'Learned Julia, Python, and R'.split(' ', 3)
print(any(i in lang for i in src))
Answer
|
What would be the output of executing the following code:
d = map(lambda c: chr(ord(c) + 1), "Oxsgnm")
print(''.join(d))
Answer
|
What would be the output of executing the following code:
s = list(range(10,20)[:4])
s[:]= 11
print(s)
Answer
|
What would be the output of executing the following code:
k = [(10,'ten'),(11,'eleven'),(12,'twelve')]
k.sort(key=lambda k: k[1])
print(k[0][1])
Answer
|
What would be the output of executing the following code:
print(sorted([8,5,9,5,7], key=lambda x:0 if x==9 else x))
Answer
|
What would be the output of executing the following code:
for i in zip([1, 2, 3], ['x', 'y']): print(i, end='')
Answer
|
What would be the output of executing the following code:
s = 'Ape deer elk cow yak fox ape'
w = s.lower().split()
print(max(dict(list(zip(w,[w.count(p) for p in w])))))
Answer
|
What is the difference between library and module and package? Answer |
Which of the following code snippets, when executed, would NOT give the output [0] Answer |
What would be the output of executing the following code:
t = [10, 13, 16]
print(max(t, key=lambda x: x%5))
Answer
|
What would be the output of executing the following code:
s = "-4" "\\-2"
print(len(s))
Answer
|
What would be the output of executing the following code:
s = [1, 2, 3, 4]
s[-1 : -5] = ['a']
print(s)
Answer
|
What would be the output of executing the following code:
t = [1,2,3,4]
x = (t.pop() > t.pop()) > (t.pop() < t.pop())
t.append(x)
print(t)
Answer
|
What would be the output of executing the following code:
w = [1, 2, 3, 4]
w[2::-2] = [7, 8]
print(w[0])
Answer
|
What would be the output of executing the following code:
sum = 0
for i in range(5, 0, -2): sum += i > sum
print(sum)
Answer
|
What would be the output of executing the following code:
a = ['xyz']
b = ['p', 'q', 'r']
print(list(zip(b, a)))
Answer
|
What would be the output of executing the following code:
s = 'apple'
print(s.find('p') + s.rfind('f') + s.find(''))
Answer
|
What would be the output of executing the following code:
s = 'the smart owl'
print(s[-9:-100:-2])
Answer
|
What would be the output of executing the following code:
w = 'Python Rocks'
print(w.find('Python') == False)
Answer
|
Which line of code should replace '# Replace this line' in the following code snippet to get the result: P-y-t-h-o-n!
w = 'Python'
d = {'sep':'-', 'end':'!'}
# Replace this line
Answer
|
Which of the following code snippets, when executed would NOT give the following output:
[1, 2, 3, []]
Answer
|
What would be the output of executing the following code:
p = [0, 1, 2, 3, 2, 4]
q = [4, 3, 2, 1, 1, 3]
print(set(p) > set(q))
Answer
|
What would be the output of executing the following code:
t = 'Hello Python'
for i in range(5): t = tuple(t)
print(len(t))
Answer
|
What would be the output of executing the following code:
x,y = (False, False)
if (y,) and [y,]: x+=1
print(x)
Answer
|
What would be the output of executing the following code:
y = zip(*[iter(range(7))]*3)
print(list(y))
Answer
|
What would be the output of executing the following code:
print(divmod(7, 3))
Answer
|
What would be the output of executing the following code:
def f1(*num): print (num + num)
f1(4/2)
Answer
|
What would be the output of executing the following code:
k = ('Python' 'Rocks')
print(k)
Answer
|
What would be the output of executing the following code:
s1 = 'ppy!'
f1 = 'a'.join(list(s1))
print(f1)
Answer
|
What would be the output of executing the following code:
list1 = ['Hi', 'python']
len(list1.extend(list(range(3))))
Answer
|
What would be the output of executing the following code:
import numpy as np
s=j=k=0
for e in np.eye(4)*5:s+=e[j];e+=1;j+=1
print(s)
Answer
|
In python 3.8 or later, what would be the output of executing the following code:
run = 20/2
[run := run + x for x in range(10)]
print(run)
Answer
|
Which of the following code snippets, when executed, would print True Answer |
What would be the output of executing the following code:
a = [1, 2, 3, 4, 5, 6, 7, 8]
print(a[-4:--1:-1])
Answer
|
What would be the output of executing the following code:
print(2**3**2**0*2)
Answer
|
What is the difference between datetime.now( ) and datetime.today( ) methods?
from datetime import datetime
print(datetime.today())
print(datetime.now())
Answer
|
What would be the output of executing the following code:
a = [1.0, 2, 1, 2, 1.0, 2.5]
print(a.index(1.0, 1))
Answer
|
In python 3.8 two dictionaries, d1 and d2, were created - d1 = {'p': 1, 'q': 2}; d2 = {'q': 3, 'r': 4}. Which of the following code snippet would NOT merge d1 and d2 into a new dictionary, d3, that will have {'q': 2, 'r': 4, 'p': 1} as its data.
Answer
|
k = 2
s1 = 'a' * k
s2 = "a" * k
s3 = 'a' * 2
s4 = "a" * 2
After executing the code above, which of the following would print True
Answer
|
What would be the output of executing the following code:
import re
s = '@Raju will meets him @11 am or reply to hr@datai.co'
print(re.findall('\S+@\S+[.]\w{2,3}', s))
Answer
|
Given two lists, a = [1, 2, 3]; b = [2, 3, 4], which of the following code snippets will NOT find the difference between the two lists (elements that are unique ie. elements that are present in one list, but not in the other)?
Answer
|
What would be the output of executing the following code:
curr_rate, curr_state = 0, True
print (not(curr_rate) + curr_state)
Answer
|
What would be the output of executing the following code: Answer |
What would be the output of executing the following code:
print([n for n in range(1895, 1905) if (not(bool(n%4)) \
and (bool(n%100) or not bool(n%400)))])
Answer
|
What would be the output of executing the following code:
a, *b, c = (1, 2, 3, 4, 5)
print(b)
Answer
|
Which of the following code snippets, when executed, prints the calendar of January 2021? Answer |
What would be the output of executing the following code:
call_codes = {"France":33, "Brazil": 55, "UK": 44}
sorted(call_codes)
Answer
|
Which of the following python statements, when executed, will NOT give the output 0.3 Answer |
What would be the output of executing the following code:
s1 = ['W', 'lo','Python']
s2 = ['!', 've', 'e']
print(" ".join(["".join([i, j]) for i, j in zip(s1, s2[::-1])]))
Answer
|
Which of the following code snippets, when executed, will give the time elapsed in seconds since the epoch as a float? Answer |
What would be the output of executing the following code:
list1 = [True, 1, 0, True, 2, 1, False, 5, 0]
print(sorted(list1))
Answer
|
Which of the following code snippets, when executed, will NOT give the output
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Answer
|
Which of the following code snippets, when executed, will NOT give the output
['Hello', 'World']
Answer
|
What would be the output of executing the following code: Answer |
What would be the output of executing the following code:
str1 = 'a quick red fox'
str1[-2::str1.count('k')]
Answer
|
Which of the following statements, when executed, raises the TypeError? Answer |
What would be the output of executing the following code:
"USA's $100b-plan q&a on WhatsApp".title()
Answer
|
What would be the output of executing the following code:
x = [2, 1, 5, 6, 8, 9, 4]
[sum(x[i::2]) for i in range(len(x)//(len(x)//2))]
Answer
|
Which of the following statements, when executed, raises the TypeError? Answer |
What would be the output of executing the following code:
x, y, z = 2, 2, 2.0
print((x is y) == (y is z), (x == z) is (y == z))
Answer
|
What would be the output of executing the following code:
vowel_set = {'a', 'e', 'i','o', 'u'}
all_vowels_set = vowel_set.add('A')
print(all_vowels_set)
Answer
|
What would be the output of executing the following code:
str1 = "python was born in the usa"
print (str1.capitalize())
Answer
|
Which of the following will NOT create an empty dictionary? Answer |
Which of the following statements give the output True? Answer |
Which of the following statements about a frozenset is NOT true? Answer |
What would be the output of executing the following code:
my_list = [1, 2, 3, 2, 4, 2, 1, 2]
print(max(set(my_list),key=my_list.count))
Answer
|
Which of the following is NOT a way to shallow copy a list? Answer |
What would be the output of executing the following code:
my_scores = (4, 6, 7, 9, 5, 4)
my_scores.sort()
Answer
|
What would be the output of executing the following code:
original_list = [1, 2, 3, 4]
new_list = original_list
new_list[1] = 8
print(original_list)
Answer
|
What would be the output of executing the following code:
list(range(5, 0, -1))
Answer
|
What would be the output of executing the following code:
my_obj = 1, "hello", 4.5, [6,8], (8, 9.2)
type(my_obj)
Answer
|
Which of the following is correct about the differences between a list and a tuple? Answer |
Which of the following about dictionary keys in NOT correct? Answer |
What would be the output of executing the following code:
a, b, c = (1, 2, 3, 4, 5, 6, 7, 8)[1:6:2]
print(b)
Answer
|
What would be the output of executing the following code:
import numpy as np
print(np.array((3, 4, 10.0, 4)))
Answer
|
What would be the output of executing the following code:
print(sorted('Data Science')[2])
Answer
|
Which of the following is a keyword in python? Answer |
What would be the output of executing the following code?
my_list = [3, 4, 7, 9.6, 8, 4]
Answer
my_set = set(my_list) print(my_set) |
What would be the output of executing the following code?
olympic_venues = ['Beijing', 'London', 'Rio', 'Tokyo', 'Paris', 'LA']
Answer
print(olympic_venues[1:-1:2]) |
Which the following functions can be used to convert an ndarray to a one-dimension array? Answer |
Which the following modules provides regular-expression functionality? Answer |
What is the output of executing the following code snippet:
my_list = [2,3,4,5,6,4]
Answer
my_list.remove(4) print(my_list) |
What is the output of executing the following code snippet:
poll_data = 7,
Answer
type(poll_data) |
What is the output of executing the following code snippet:
salary = 36,567.90
Answer
type(salary) |
What is the output of executing the statement:
[ ] and 5
Answer
|
What is the output of executing the statement:
len(list("Hi Python!"))
Answer
|
What is the return value of print()? Answer |
Which of the following can be used as a dictionary key? Answer |
What would be output of executing this code:
print(True*2)
Answer
|
Which code snippet will give this output:
['H', 'e', 'l', 'l', 'o']
Answer
|
Which of the following python statements will not be ignored by python interpreter, but the interpreter does nothing at run time as the intended use of the statement is to act as a stand-in or placeholder inside a class or function definition? Answer |
Which of the following built-in function of python will return the physical memory address of the object being referenced? Answer |
Which of the following is not supported by python? Answer |
Which of the following python objects is immutable? Answer |
Which code snippet will give this output:
1.0 // 2
Answer
|
Which open source framework for parallel and distributed Python can make numeric computations many times faster? Answer |
What would be the output of executing the following code:
print(round(7.5) - round(6.5))
Answer
|