Top Interview Questions For Python – Check your Ability

1. Tricky Python Interview Questions

In our series of Python Interview Questions, we discussed part 3 of Interview Questions for Python. Today, we will move towards part 4. This part of Interview Questions for Python contains some basic and important Python questions. If you want to crack your Python developer interview, these interview questions for Python will prove beneficial for you. Follow all the links for getting more in-depth knowledge of Python.

So, let’s explore most asked Interview Questions for Python

Top Interview Questions For Python - Check your Ability

Top Interview Questions For Python – Check your Ability

2. Best Interview Questions for Python

Following are the most tricky interview questions for Python. Both Python freshers and experienced can refer these Python Interview Questions. So, let’s see these interview questions for Python in detail –

Q.1. Take a look at this piece of code:

>>> A0= dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
>>> A1= range(10)
>>> A2= sorted([i for i in A1 if i in A0])
>>> A3= sorted([A0[s] for s in A0])
>>> A4= [i for i in A1 if i in A3]
>>> A5= {i:i*i for i in A1}
>>> A6= [[i,i*i] for i in A1]
>>> A0,A1,A2,A3,A4,A5,A6

What are the values of variables A0 to A6? Explain.

Ans. Here you go:

A0= {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
A1= range(0, 10)
A2= []
A3= [1, 2, 3, 4, 5]
A4= [1, 2, 3, 4, 5]
A5= {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
A6= [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]

Now to find out what happened. A0 zips ‘a’ with 1, ‘b’ with 2, and so on. This results in tuples, which the call to dict() then turns into a dictionary by using these as keys and values.

Have a look at Python Itertools

A1 gives us a range object with start=0 and stop=10.

A2 checks each item in A1- does it exist in A0 as well? If it does, it adds it to a list. Finally, it sorts this list. Since no items exist in both A0 and A1, this gives us an empty list.

A3 takes each key in A0 and returns its value. This gives us the list [1,2,3,4,5].

A4 checks each item in A1- does it exist in A3 too? If it does, it adds it to a list and returns this list.

A5 takes each item in A1, squares it, and returns a dictionary with the items in A1 as keys and their squares as the corresponding values.

A6 takes each item in A1, then returns sublists containing those items and their squares- one at a time.

Q.2. Differentiate between split(), sub(), and subn() methods of the re module.

Ans. The re module is what we have for processing regular expressions with Python. Let’s talk about the three methods we mentioned-

  • split()- This makes use of a regex pattern to split a string into a list
  • sub()- This looks for all substrings where the regex pattern matches, and replaces them with a different string
  • subn()- Like sub(), this returns the new string and the number of replacements made

Q.3. Differentiate between Django, Pyramid, and Flask.

Ans. These are three major frameworks in Python. Here are the differences:

  • We can also use Django for larger applications. It includes an ORM.
  • Flask is a microframework for a small platform with simpler requirements. It is ready to use and you must use external libraries.
  • The pyramid is for larger applications. It is flexible and you can choose the database, the URL structure, the templating style, and much more. It is also heavily configurable.

Q.4. Explain the Inheritance Styles in Django.

Ans. Talking on inheritance styles, we have three possible-

  • Abstract Base Classes- For the parent class to hold information so we don’t have to type it out for each child model
  • Multi-table Inheritance- For subclassing an existing model and letting each model have its own database
  • Proxy Models- For modifying the model’s Python-level behavior without having to change its fields

Q.5. Explain Python List Comprehension.

The list comprehension is a way to declare a list in one line of code. Let’s take a look at one such example.

>>> [i for i in range(1,11,2)]

[1, 3, 5, 7, 9]

>>> [i*2 for i in range(1,11,2)]

[2, 6, 10, 14, 18]

Q.6. How will you locally save an image using its URL address?

Ans. For this, we use the urllib module.

>>> import urllib.request
>>> urllib.request.urlretrieve('https://yt3.ggpht.com/a-/ACSszfE2YYTfvXCIVk4NjJdDfFSkSVrLBlalZwYsoA=s900-mo-c-c0xffffffff-rj-k-no','dataflair.jpg')

(‘dataflair.jpg’, <http.client.HTTPMessage object at 0x02E90770>)

You can then get to your Python’s location and confirm this.

Q.7. What does the map() function do?

Ans. map() executes the function we pass to it as the first argument; it does so on all elements of the iterable in the second argument. Let’s take an example, shall we?

>>> for i in map(lambda i:i**3, (2,3,7)):
print(i)

8
27
343

This gives us the cubes of the values 2, 3, and 7.

Have a look at Python Geographic maps and Graph Data

Q.8. How will you share global variables across modules?

Ans. To do this for modules within a single program, we create a special module, then import the config module in all modules of our application. This lets the module be global to all modules.

Q.9. What is the process to calculate percentiles with NumPy?

Ans. Refer to the code below.

>>> import numpy as np
>>> arr=np.array([1,2,3,4,5])
>>> p=np.percentile(arr,50)
>>> p

3.0

Q.10. What is Flask- WTF? Explain its features.

Ans. Flask-WTF supports simple integration with WTForms. It has the following features-

  • Integration with wtforms
  • Global csrf protection
  • Recaptcha supporting
  • Internationalization integration
  • Secure form with csrf token
  • File upload compatible with Flask uploads

Interview Questions for Python Freshers – Q. 2,3,4,5,7,8,10

Interview Questions for Python Experienced – Q. 1,6,9

Q.11. How is NumPy different from SciPy?

Ans. We have so far seen them used together. But they have subtle differences:

  • SciPy encompasses most new features
  • NumPy does hold some linear algebra functions
  • SciPy holds more fully-featured versions of the linear algebra modules and other numerical algorithms
  • NumPy has compatibility as one of its essential goals; it attempts to retain all features supported by any of its predecessors
  • NumPy holds the array data type and some basic operations: indexing, sorting, reshaping, and more

Q.12. What is the Dogpile effect?

Ans. In case the cache expires, what happens when a client hits a website with multiple requests is what we call the dogpile effect. To avoid this, we can use a semaphore lock. When the value expires, the first process acquires the lock and then starts to generate the new value.

Q.13. How do you insert an object at a given index in Python?

Ans. Let’s build a list first.

>>> a=[1,2,4]

Now, we use the method insert. The first argument is the index at which to insert, the second is the value to insert.

>>> a.insert(2,3)
>>> a

[1, 2, 3, 4]

Q.14. And how do you reverse a list?

Ans. Using the reverse() method.

>>> a.reverse()
>>> a

[4, 3, 2, 1]

Q.15. What is a Python module?

Ans. A module is a script in Python that defines import statements, functions, classes, and variables. It also holds runnable Python code. ZIP files and DLL files can be modules too. The module holds its name as a string that is in a global variable.

Q.16. What is the with statement in Python?

Ans. Using the with statement, we open files, process data in files, and even close them without having to make a call to the close() method. This makes exception-handling simpler with the cleanup activities. Here’s a demonstration-

>>> with open('data.txt') as data:
#processing statements

You must read errors and exceptions in Python

Q.17. What are the different file-processing modes with Python?

Ans. We have the following modes-

  • read-only – ‘r’
  • write-only – ‘w’
  • read-write – ‘rw’
  • append – ‘a’

We can open a text file with the option ‘t’. So to open a text file to read it, we can use the mode ‘rt’. Similarly, for binary files, we use ‘b’.

Q.18. What are the file-related modules we have in Python?

Ans. We have the following libraries and modules that let us manipulate text and binary files on our file systems-

os

os.path

shutil

Q.19. What makes Python object-oriented?

Ans. Python is object-oriented because it follows the Object-Oriented programming paradigm. This is a paradigm that revolves around classes and their instances (objects). With this kind of programming, we have the following features:

Encapsulation

Abstraction

Inheritance

Polymorphism

Data hiding

Q.20. Does Python support interfaces like Java does?

Ans. No. However, Abstract Base Classes (ABCs) are a feature from the abc module that let us declare what methods subclasses should implement. Python supports this module since version 2.7.

Interview Questions for Python Freshers – Q. 11,12,15,16,19,20

Interview Questions for Python Experienced – Q. 13,14,18

Q.21. Explain the output of the following piece of code-

x=[‘ab’,’cd’]

print(len(map(list,x)))

Ans. This actually gives us an error- a TypeError. This is because map() has no len() attribute in their dir().

Q.22. So what is the output of the following piece of code?

x=[‘ab’,’cd’]

print(len(list(map(list,x))))

Ans. This outputs 2 because the length of each string is 2.

Q.23. How can you keep track of different versions of code?

Ans. To make this happen, we implement version control. For this, one tool you can use is Git.

Q.24. Explain garbage collection with Python.

Ans. The following points are worth nothing for the garbage collector with CPython-

  • Python maintains a count of how many references there are to each object in memory
  • When a reference count drops to zero, it means the object is dead and Python can free the memory it allocated to that object
  • The garbage collector looks for reference cycles and cleans them up
  • Python uses heuristics to speed up garbage collection
  • Recently created objects might as well be dead
  • The garbage collector assigns generations to each object as it is created
  • It deals with the younger generations first.

Q.25. How is Python different from Java?

Ans. Following is the comparison of Python vs Java – 

a. Java is faster than Python

b. Python mandates indentation. Java needs braces.

c. Python is dynamically-typed; Java is statically typed.

d. Python is simple and concise; Java is verbose

e. Python is interpreted

f. Java is platform-independent

g. Java has stronger database-access with JDBC

Q.26. How do we execute Python?

Ans. Python files first compile to bytecode. Then, the host executes them.

<provide link to Python Compiler Tutorial>

Q.27. What is the Python interpreter prompt?

Ans. This is the following sign for Python Interpreter:

>>>

If you have worked with the IDLE, you will see this prompt.

Q.28. Explain try, raise, and finally.

Ans. These are the keywords we use with exception-handling. We put risky code under a try block, use the raise statement to explicitly raise an error, and use the finally block to put code that we want to execute anyway.

<put link to exception handling>

Have a look at Python Exception Handling

Q.29. What happens if we do not handle an error in the except block?

Ans. If we don’t do this, the program terminates. Then, it sends an execution trace to sys.stderr.

Q.30. How does a function return values?

Ans. A function uses the ‘return’ keyword to return a value. Take a look:

>>> def add(a,b):
       return a+b

Interview Questions for Python Freshers – Q. 21, 23, 26, 29

Interview Questions for Python Experienced – Q. – 22, 24, 25,27,28,30

So, this was all in Interview Questions for Python. Hope this helps you.

3. Conclusion – Interview Questions for Python

So, you have completed the Interview Questions for Python. We hope you learned something new with this set of Python Interview Questions we curated right for you. Want more? We’ll be here with more questions. Still, if you have any query related interview Questions for Python, ask in the comment tab.

See also – 

Python Interview Questions part 6

4 Responses

  1. lavanya says:

    there is no method called “reverse” to reverse a list.
    instead use reversed() method.
    ex:
    a = [1,2,3]
    b = list(reversed(a))
    output: [3,2,1]

    • DataFlair Team says:

      Hello Lavanya,
      Lists do have a reverse() method; try it out in your Python shell. Your approach isn’t incorrect either- but you have made use of the reversed class in Python. Try checking with help(reversed).
      Hope, it helps!

  2. Laczkó Péter says:

    Hello,

    Q.28. Explain try, raise, and finally.
    Maybe I would change title to “Explain try, except and finally”

    Regards
    Peter

    • DataFlair Team says:

      Hi Laczkó

      The question is indeed about the try, raise, and finally keywords in Python. We haven’t mistakenly written raise instead of except; we can understand your concerns, however.
      Hope, it helps!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.