10 Python Interview Questions and Answers for Freshers

Tags: python, beginners, career
Published: 2020-03-25 07:46:18


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](https://www.payscale.com/research/US/Job=Python_Developer/Salary)). 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](https://www.guru99.com/difference-compiler-vs-interpreter.html) 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](https://holycoders.com/python-list-comprehension/) 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.](https://stackoverflow.com/questions/9755990/why-can-tuples-contain-mutable-items)
Thanks to [Nik Gil](https://dev.to/nikolaias) 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](https://holycoders.com/python-program-to-sum-all-values-in-dictionary/).

### 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](https://en.wikipedia.org/wiki/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](https://holycoders.com/python-print-function/).