With growing competition in the market especially if you are a Fresher, it is very important that you stand out in your interview. You can make quite an impression on your interviewer if your basics are clear. This post is going to help you out to practice all the important Python interview questions .
1. What is the difference between list and tuples in Python?
Below table includes basic difference between list and tuples-
List | Tuple |
It is changeable | It is not changeable |
It is surrounded by square brackets – [] | It is surrounded by parenthesis () |
It has more built in functions | It has less built in functions |
Less memory efficient | More memory efficient |
List vs Tuples
2. What is a lambda function? Give an example.
A lambda function is a normal python function, except that it does not have any name when defining it. It is defined using lambda
keyword. Below is the syntax for a lambda function.
lambda arguments: expression
A lambda function can have multiple arguments but a single expression. It evaluates the expression for the given arguments and return. Lambda function is very much used along with built-in functions like map()
and filter()
.
The map()
function in Python takes a function and an iterator. The function is then called with all the items in the iterator and a new list is returned by that function. A simple example to clarify this-
# Program to square each item in a list using map()
list1 = [2, 55, 7, 6, 8, 1, 8, 19]
list2 = list(map(lambda x: x * x , list1))
print(f 'new list after squaring is {list2}')
3. What is list comprehension in Python?
List comprehension is a fast and efficient way of creating a list in Python. Below is the syntax for creating a list using list comprehension-
new_list = [ expression for item in list if condition ]
A simple example to create a list by squaring each item in given range would be-
a1 = [item *item for item in range(5)]
print (a1)
Output-
[0, 1, 4, 9, 16, 25]
4. What are negative indexes in Python? Why are they used?
Negative indexing starts from the end of an array, which means negative index -1 will give you the last element in the array, -2 index will give you second last and so on.
Example-
a1 = [11, 22, 33, 44, 55]
print(a1[-1])
print(a1[-2])
Output-
55
44
5. What is break, pass and continue statement in Python?
Break A break statement allows a loop to terminate immediately. It will pass the control to the code written just outside the loop.
In the below example, the break
statement gets executed when item ==
3 is satisfied. It terminates the loop immediately. The control is passed to the second print statement after break
is executed.
for item in range(6):
if item == 3:
break
print(item)
print(f' item is {item}')
# 0
# 1
# 2
# item is 3
Continue The continue statement skips the current iteration and jumps to next one. It just ignores rest of the code and moves to next iteration.
Below example will clarify above-
for item in range(4):
if item == 3:
continue
print(item)
# 0
# 1
# 2
# 4
Pass A Pass statement basically does nothing. It is used to fill a place in code where at least 1 statement is required.
The pass statement can be used to fill in code in initial development that could be used in the future.
6. What is the use of *args, **kwargs?
*args *args is used as an argument in a function when we are not sure about the number of arguments to pass in the function. In below example- new_func function receives few numbers as argument and returns their sum.
def new_func(*nums):
sum = 0
for item in nums:
sum = sum + item
print(f' hey sum is {sum}')
new_func(2,2)
new_func(5,2,3)
Output would be –
hey sum is 4
hey sum is 10
**kwargs This allows us to pass any number of keyword arguments. Keyword arguments mean they are like a key-value pair.
Below example will make it clear-
def new_func1(**allwords):
for key,value in allwords.items():
print(f'{key} is {value}')
new_func1(name="Ram", age=22)
Output –
name is Ram
age is 22
7. What are decorators in Python?
Decorators in Python basically allow you to modify the behavior of a callable, without modifying it permanently.
Below is an example of a simple decorator to convert the result of function to uppercase –
def upperstr(func):
def wrapper():
old_str = func()
new_str = old_str.upper()
return new_str
return wrapper
@upperstr
def random_func():
return 'Hey I am new'
>>> random_func()
'HEY I AM NEW'
8. What is slicing in Python?
Slicing is the process of extracting a part of string, tuple or list. Elements can be extracted by mentioning the range of the indices.
Below is the syntax for slicing-
Object [start:stop:step]
A simple example for slicing –
my_name = "glitchblog"
print(my_name[1:4])
>> lit
9. What is sort() and sorted() in Python? Explain the difference.
sort() and sorted() both are used to sort a given list. Main difference between both is that sort() will change the list and does not return anything ,while sorted() will create a new list and return the same after sorting but original list will not be changed.
10. What is __init__ in Python?
__init__
method has special significance in Python class. It is called automatically when a new object of a class is created and is used to initialize the object’s attributes.
class Player:
def __init__(self, name):
self.name = name
def greet(self):
print 'Hello, player is', self.name
p = Player('John')
p.greet()
Output-
Hello, player is John
These are few of the most asked Python interview questions for Freshers, will keep adding to the list. Also, to really crack the interviews, one should really understand the basics and few good projects to showcase the knowledge.
It was very helpful ,thanks
LikeLike