Top 10 Most Asked Python Interview Questions With Answers Part 11

Top 10 Most Asked Python Interview Questions With Answers Part 11 By Muhammad Umair

Question no 1: How can you declare multiple assignments in one statement?

There are two ways to do this:

#This assigns 3, 4, and 5 to a, b, and c resp.
a = b = c =3
#This assigns 3 to a, b, and c

Q.uestion no 2: What is tuple unpacking?

First, let’s discuss tuple packing. It is a way to pack a set of values into a tuple.


(3, 4, 5)

This packs 3, 4, and 5 into mytuple.

Now, we will unpack the values from the tuple into variables x, y, and z.


Question no 3: What data types does Python support?

Python provides us with five kinds of data types: a=7.0     
title="Ramayan's Book"
<class 'list'>
Traceback (most recent call last):
File "<pyshell#129>, line 1, in <module>
TypeError: 'tuple' object does not support item assignment squares={1:1,2:4,3:9,4:16,5:25}
<class 'dict'>
<class 'dict'>
squares={x:x**2 for x in range(1,6)}
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Question no 4: What is a docstring?

A docstring is a documentation string that we use to explain what a construct does. We place it as the first thing under a function, class, or a method, to describe what it does. We declare a docstring using three sets of single or double quotes.

def sayhi():    
""" The function prints Hi """


To get a function’s docstring, we use its __doc__ attribute.


‘ This function prints Hi ‘

A docstring, unlike a comment, is retained at runtime.

Question no 5: What is the PYTHONPATH variable?

PYTHONPATH is the variable that tells the interpreter where to locate the module files imported into a program. Hence, it must include the Python source library directory and the directories containing Python source code. You can manually set PYTHONPATH, but usually, the Python installer will preset it.

Question no 6: What is slicing?

These are the types of basic Python interview questions for freshers.

Slicing is a technique that allows us to retrieve only a part of a list, tuple, or string. For this, we use the slicing operator [].

(3, 4)
[8, 5, 9]

Question no 7: What is a namedtuple?

A named tuple will let us access a tuple’s elements using a name/label. We use the function namedtuple() for this, and import it from collections.

from collections import namedtuple result=namedtuple('result','Physics Chemistry Maths') #format Ramayan=result(Physics=86,Chemistry=95,Maths=86) #declaring the tuple Ramayan.Chemistry


As you can see, it let us access the marks in Chemistry using the Chemistry attribute of object Ramayan.

Question no 8: How would you declare a comment in Python?

Unlike languages like C++, Python does not have multiline comments. All it has is octothorpe (#). Anything following a hash is considered a comment, and the interpreter ignores it.

#line 1 of comment 
#line 2 of comment

In fact, you can place a comment anywhere in your code. You can use it to explain your code.

Question no 9: How would you convert a string into an int in Python?

If a string contains only numerical characters, you can convert it into an integer using the int() function.



Let’s check the types:


<class ‘str’>


<class ‘int’>

Question no 10: How do you take input in Python?

For taking input from user, we have the function input(). In Python 2, we had another function raw_input().

The input() function takes, as an argument, the text to be displayed for the task:

a=input('Enter a number')

Enter a number7

But if you have paid attention, you know that it takes input in the form of a string.


<class ‘str’>

Multiplying this by 2 gives us this:

a*=2 a


So, what if we need to work on an integer instead?

We use the int() function for this.

a=int(input('Enter a number'))

Enter a number7

Now when we multiply it by 2, we get this:

a*=2 a




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Muhammad Umair

Muhammad Umair


MERN Stack Developer | Software Engineer| Frontend & Backend Developer | Javascript, React JS, Express JS, Node JS, MongoDB, SQL, and Python