13 Essential Python Interview Questions

Here are the 13 essential python questions useful for interviews.

Python Interview Questions

1. What is mutable and Immutable?

Everything in Python is Object. Mutable objects can be changed after it is created while the immutable object cannot change.

Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable.

Objects of built-in types like (list, set, dict) are mutable. Custom classes are generally mutable.

2. Write sample syntax for Dictionary?

A={“srini”:’rao’, “sal” : 1000}

3. List Vs Tuple?

List – List is mutable [1,2,3,4,6]

Square brackets for List.

Best example: We can replace/append any value. You can mask middle 6 letters in credit card number, which is a value in List

Tuple – Immutable (1,2,3,4,5)

Parenthesis for Tuple.

Best example: Name in the Tuple, you cannot replace.

4. Local variable Vs Global Variable?

The variable, which is defined inside the function is called local variable and it is local to function.

The variable, which is defined outside the function is called Global variable.

order=”maggi” ==> Global

def myfunction():

              order=”roti” ==> Local

myfunction()

5. What is Global Keyword in Python?

If we use “Global” keyword for a variable inside the function. Then it will access Global variable.

Suppose in the function, if we don’t mention the Global keyword, but if we try to use that, it gives error.

var = 321

# function to modify the variable

def modify():

   global var  ==> Using Global keyword

   var = var * 2

   print(var)

# calling the function

modify()

6. What happens if we try to replace a value in Tuple?

Tuple is immutable in Python, that means we cannot replace/modify it. If we try to do that, we will get an error.

TypeError: ‘tuple’ object does not support item assignment

7. What is List Comprehension?

It is concise way of creating list. It is popularly called List Comprehension.

Best Examples:

a=[n*2 for n in range(10)]

print(a)

myList = [ num**2 for num in range(1,10) if num %2==0]

print(myList)

Output:

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

[4, 16, 36, 64]

Similarly, you can check for Dictionary comprehension.

8. What is Zip() function?

The zip() function takes iterables (List, Tuple) as arguments and create iterator.

mylist=(100,200,300)
mytuple=(500,600,800)
myoutput=zip(mylist, mytuple)
myiterator=set(myoutput)
print(myiterator)
Output:
{(200, 600), (100, 500), (300, 800)}

9. What is iterator?

In general, iterators are repetition, and you can read iterables by using iter() and next() function

mylist=[1,2,3.4,5]

newlist=iter(mylist)

iter1=next(newlist)

print(iter1)

Output:

1

10. What is interpreter?

Python is interpreted language. It converts source code to machine understandable code (by using dedicated in-built virtual machine). Best examples are Python, Ruby, java.

Here is reference link for best explanation on interpreted language.

11. What are ‘Yield’ and “Generator’ in Python?

Basically, it is a function. If we give yield in any function, that function is called ‘Generator’

Script name abc.py

def course_generate():
  yield(“C”)
  yield(“C++”)
  yield(“Java”)
  yield(“Python”)
  yield(“Php”)
  yield(“Vb.Net”)
  yield(“Asp.Net”)
  yield(“Android”)

Script mygen.py

from course_generate import course_generate
course = course_generate() # function is called here
print(next(course)) # only first element will be printed i.e. "C"

12. What is Decorator?

Calling another function in a function is called Decorator.

13. What is Meta class?

With Meta class you can create Classes.

More Questions

14. What is split() function?

The split() function splits the strings into a comma separated list.

str=”a b c d”

out=str.split()

print(out)

[‘a’, ‘b’, ‘c’, ‘d’]

15. What are the types of arguments you can pass to a function?

def myfunc()

return “test”

Those are:

  • Positional Arguments
  • Default Arguments
  • Keyword arguments

Here is reference link for types of arguments.

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.

Start Discussion

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.