Top 10 Most Asked Python Interview Questions With Answers Part 11

By Muhammad Umair

Muhammad Umair
3 min readSep 27, 2022
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:

a,b,c=3,4,5     
#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.

mytuple=3,4,5mytuple

(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.

x,y,z=mytuple
x+y+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"
colors=['red','green','blue']
type(colors)
<class 'list'>
name=('Ramayan','Sharma')
name[0]='Avery'
Traceback (most recent call last):
File "<pyshell#129>, line 1, in <module>
name[0]='Avery'
TypeError: 'tuple' object does not support item assignment squares={1:1,2:4,3:9,4:16,5:25}
type(squares)
<class 'dict'>
type({})
<class 'dict'>
squares={x:x**2 for x in range(1,6)}
squares
{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 """
print("Hi")sayhi()

Hi

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

sayhi.__doc__

‘ 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 [].

(1,2,3,4,5)[2:4]        
(3, 4)
[7,6,8,5,9][2:]
[8, 5, 9]
'Hello'[:-1]
'Hell'

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

95

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.

int('227')

227

Let’s check the types:

type('227')

<class ‘str’>

type(int('227'))

<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.

type(a)

<class ‘str’>

Multiplying this by 2 gives us this:

a*=2 a

‘77’

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

14

--

--

Muhammad Umair

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