DEV Community

Cover image for 10 Python Interview Questions and Answers for Freshers
Digvijay Singh
Digvijay Singh

Posted on • Updated on

10 Python Interview Questions and Answers for Freshers

Python is growing to be the most popular language because of simplicity. It is considered as the best programming language to start if you don't have a computer science background. There are lots of Jobs for python developers all around the world with an average salary of $79k (according to payscale). Here is the list of best Python interview questions and answers for freshers that can be helpful to crack Python job interview.

Python Interview Questions

Most of the questions in the list are based on my personal experience of Python programming job interviews during the college recruitment process, and also some other well-known questions that are asked in almost all programming interviews.

What is the difference between compiler and Interpreter?

Both Compiler and interpreter do the same job of converting high-level language into low-level language.
The compiler converts high-level language to machine understandable code and then gives the output.
Interpreter on the other hand directly gives the output of the statement.
One more difference which is general is that compiler execute the whole program at once and Interpreter executes program line by line.
This is the reason why the program executes successfully until any error occurs in Python.
Here is a detailed comparison between the two.

Difference between Python and Java

This is the question of general discussion and often asked in advanced Python programming interview. The interviewer expects you to know about other programming languages.
Python is Interpreted Language while Java is compiled language.
Python is Very Easy to learn as compared to java.
Multiple inheritance is supported by Python, but in Java, multiple inheritance is done through the interface.
Python is mostly Used Data Analytics, Machine learning, software and web development and Java in software development and web development

What is list comprehension in Python?

List comprehension in Python is a way to write logic inside any list. It is very helpful in writing short and concise code.
Here is an example to print the square of numbers from 1 to 10 using list comprehension.

print([i**2 for i in range(1, 11)])
#Output = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Not only limited to this, but it can also handle nested if-else conditions. Here is an example to check even and odd numbers from 1 to 10.

print(["{} is even".format(i) if(i%2==0) else "{} It is odd".format(i) for i in range(1, 11)])

List comprehension is the way to write more pythonic code and you must go for it, just remember don't create very complex nested list comprehension because other developers will find it hard to read.
Here is a detailed guide on List comprehension in Python which can be very helpful in some cases.

Difference between Python list and array of C language.

Python list is more flexible and useful as compared to the array data structure.
The list has dynamic size while array has fixed size. It means the list size grows and shrinks internally we do not need to do anything.
The array is more efficient if we know how many elements to store while List can hold useless memory.
Python list is an advanced data structure in which we can write nested logic like list comprehension. There is nothing like this in an array.

Difference between list and tuple in Python

List and tuple are very helpful data structures in Python but are often confusing for beginners.
The major difference between list and tuple is that list is mutable (items can be changed) and tuple is immutable (cannot be changed after it is created).

What is the use of Map function in Python?

The map function takes two arguments, the first one is a function and the other is iterable.

map(function, iterable)

It takes each element of iterable and passes it to the function, the returned values are stored and returned in the form of a map object.
Here is an example that convert string elements to integer type.

int_values = map(int, ["1", "2", "3"])
print(list(int_values)) #Convert map object to list type
#Output = [1, 2, 3]

What are Mutable and Immutable objects in Python?

Mutable objects are those whose values can be changed like a list. Immutable objects are those objects which cannot be changed once they are created, they are read-only, example tuple.
Tuples can also contain mutable objects like list then how tuples are immutable.
Thanks to Nik Gil for this suggestion.

Write a Python program to sum all dictionary values.

Unfortunately, I was unable to answer this question because I haven't faced a situation to sum values of a dictionary.
Here is the solution of the question:

num_dict = {'a': 1, 'b': 2, 'c': 3}
print(sum(num_dict.values()))

The values method of any dictionary object return iterable of type dict_values. The sum function takes any iterable and returns its total sum.
An in-depth explanation of the code can be found here.

Check Sexy Prime numbers in Python

Sexy prime numbers are prime numbers which differ by 6. Also defined as a number n is sexy Prime if n is prime and n+6 is also prime number.

def is_prime(num):
    for i in range(2, int(num**0.5)+1):
        if(num%i==0):
            return False
        else:
            return True

if __name__ == "__main__":
    num = int(input("Enter any number to check sexy prime: "))
    if(is_prime(num) and is_prime(num+6)):
        print("{} is sexy Prime".format(num))
    else:
        print("{} is not sexy prime".format(num))

What is the algorithm behind Python sort function?

Thanks to my curiosity, I was randomly searching some interesting facts about python and find that Python uses Timsort in its sort function. It was developed by Tim Peters in 2002 to use in Python.

You may also like in-depth explanation of Python print function.

Top comments (6)

Collapse
 
bezirganyan profile image
Grigor Bezirganyan • Edited

1. To say

Python is Interpreted Language while Java is compiled language

is over simplification and not fully true.

First of all, if we define compiled languages those languages which translate high-level code to machine code (like C/C++), neither Java nor Python are compiled languages, since they translate (compile) high level code to bytecode, which, later, can be both compiled or interpreted to machine code. Furthermore, today the famous implementations of both languages use a technique called Just-in-time compiling (JIT).

So, when you say Java is compiled language, you cannot say that python is interpreted and vise-versa. Those languages can both be compiled and interpreted.

2. When you compare arrays and lists, again there is a problem with definitions.

Python has a builtin data type list, but it doesn't have array data type, so when you compare, you have to indicate what do you mean by saying list and what do you mean by saying array. This is necessary, because list has a lot different meaning in computer science than it does in python. In CS we refer to lists as linked lists. In python, list is more similar to dynamic arrays (or vectors in C++ stl, with a difference that in python a list can contain objects of different types) than to linked lists (at least in CPython).

So, if you get a question like this during the interview, you need to clarify what they mean:

  • CS: linked list vs array?
  • Python list vs array from a library (eg NumPy.array())
  • etc
Collapse
 
digvijaysingh profile image
Digvijay Singh

Thanks for the comment I have updated the question, it referred to the array of C language.
Sorry for the confusion created.

Collapse
 
ocasoprotal profile image
OcasoProtal

Look how unreadable it is. This is the main reason to avoid list comprehension because it makes code hard to read for humans.

No, you should not avoid list comprehensions, you should avoid unreadable code! That's quite a difference.

Collapse
 
digvijaysingh profile image
Digvijay Singh

I agree. We can definitely use list comprehension to make out code look short and beautiful but complex logic in a list comprehension must be avoided.

Collapse
 
nikolaias profile image
Nik Gil • Edited

Python is Very Easy to learn as compared to java.

While I do agree it still leaves room to some subjectivity. Anyone used to static typed languages for many years will find python unnatural. Especially all the quirks it provides.

in Java, multiple inheritance is done through the interface.

This is more commonly referred to as composition, not inheritance.

you should avoid list comprehension because sometimes it makes code hard to read for humans.

This shouldn't be pigeonholed to list comprehensions, or even python. It's simple as "don't write code someone else can't read"

Difference between Python list and array

If talking about python in particular you need to mention more. Like array.array is essentially a thin wrapper on a C array so another limitation is that it should be single type.

they are read-only, example tuple

Caveat - if a tuple holds references to mutable objects then said mutable objects can be mutated, making the tuple appear mutable. Not really a fault of tuple but if someone mentioned that then this would be my next question.

Collapse
 
digvijaysingh profile image
Digvijay Singh • Edited

Thanks for all these suggestions.
Sorry for the list vs array confusion, the interviewer referred to the array of C language because it was in my resume :).
I think the immutable definition needs some more reference for the explanation, I have added a link which describes that.
Please let me know if anything needs a fix.