Skip to content
InterviewTigerInterviewTiger
Back to the journal
Technical interviews

Python interview questions for freshers (with answers)

The Python questions asked most in fresher and junior developer interviews, covering data types, OOP, common functions and simple coding tasks.

Python code on a dark editor screen next to the InterviewTiger logo9 min read

Python is now the first language taught in most Indian engineering colleges and the first language asked about in many fresher interviews — for software development, data science, automation and testing roles. These questions cover the topics that come up most often, with short answers you can explain in your own words and small code examples to study.

Basics

  • What is Python and why is it popular? — Python is an interpreted, dynamically typed, high-level language. It is popular because the syntax is close to English, the standard library is large, and it has strong communities in data science, web development and automation.
  • What is the difference between a compiled and an interpreted language? — A compiled language (C, Java) is translated to machine code before running. Python is interpreted: each line is translated and run one at a time by the Python interpreter. This makes debugging easier but can be slower for computation-heavy tasks.
  • Is Python call-by-value or call-by-reference? — Neither exactly. Python passes object references by value. For immutable objects (int, str, tuple), changes inside a function do not affect the caller. For mutable objects (list, dict), changes do.

Data types

  • What is the difference between a list, a tuple and a set? — List: ordered, mutable, allows duplicates. Tuple: ordered, immutable, allows duplicates (use for data that should not change). Set: unordered, mutable, no duplicates (use for membership tests).
  • When would you use a dictionary over a list? — When you need to look up a value by a key instead of by position. Dictionary lookup is O(1) on average; list search is O(n).
  • What is the difference between '==' and 'is'? — '==' checks whether two values are equal. 'is' checks whether two variables point to the exact same object in memory.
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)   # True  — same values
print(a is b)   # False — different objects in memory

c = a
print(a is c)   # True  — same object

Mutability

Mutable objects can be changed after creation (list, dict, set). Immutable objects cannot (int, float, str, tuple). This matters for function arguments and dictionary keys — only immutable types can be used as keys.

# String is immutable
s = 'hello'
# s[0] = 'H'  ← raises TypeError

# List is mutable
lst = [1, 2, 3]
lst[0] = 99   # works fine
print(lst)    # [99, 2, 3]

Functions

  • What is a lambda function? — An anonymous one-line function. Use it when you need a small function in one place and do not want to name it.
  • What do *args and **kwargs do? — *args collects extra positional arguments into a tuple. **kwargs collects extra keyword arguments into a dictionary. They let a function accept any number of arguments.
  • What is a decorator? — A function that takes another function and extends its behaviour without modifying it. Commonly used for logging, authentication and caching.
# lambda
double = lambda x: x * 2
print(double(5))  # 10

# *args and **kwargs
def show(*args, **kwargs):
    print(args)    # (1, 2, 3)
    print(kwargs)  # {'name': 'Raj'}

show(1, 2, 3, name='Raj')

# decorator
def log(func):
    def wrapper(*args, **kwargs):
        print(f'Calling {func.__name__}')
        return func(*args, **kwargs)
    return wrapper

@log
def add(a, b):
    return a + b

add(2, 3)  # prints 'Calling add', returns 5

List comprehensions

A concise way to create a list from an existing one. It is faster than a for loop for simple transformations and reads well when kept short.

numbers = [1, 2, 3, 4, 5, 6]

# Squares of even numbers
result = [x**2 for x in numbers if x % 2 == 0]
print(result)  # [4, 16, 36]

Error handling

Use try/except to handle errors without crashing the program. Put only the code that might fail inside the try block — not the whole function.

def safe_divide(a, b):
    try:
        return a / b
    except ZeroDivisionError:
        return 'Cannot divide by zero'
    finally:
        print('Division attempted')  # runs always

print(safe_divide(10, 2))   # 5.0
print(safe_divide(10, 0))   # Cannot divide by zero

OOP in Python

  • What is __init__? — The constructor method, called when an object is created. It sets the object's initial state.
  • What is self? — A reference to the current instance of the class. It is the first parameter of every instance method.
  • What is the difference between a class method and a static method? — A class method takes cls as its first argument and can access or modify class state. A static method takes no implicit first argument — it is just a function that belongs to the class's namespace.
  • What is inheritance? — A child class gets all the attributes and methods of its parent class and can override or extend them.
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f'{self.name} makes a sound'

class Dog(Animal):
    def speak(self):
        return f'{self.name} barks'  # override

d = Dog('Rex')
print(d.speak())  # Rex barks

Commonly asked coding tasks

  • Check if a string is a palindrome.
  • Count the frequency of each character in a string.
  • Find the two largest numbers in a list without sorting.
  • Remove duplicates from a list while keeping the order.
  • Flatten a nested list.
# Palindrome check
def is_palindrome(s):
    s = s.lower().replace(' ', '')
    return s == s[::-1]

print(is_palindrome('racecar'))  # True
print(is_palindrome('hello'))    # False

# Remove duplicates, keep order
def remove_dupes(lst):
    seen = set()
    return [x for x in lst if not (x in seen or seen.add(x))]

print(remove_dupes([1, 2, 2, 3, 1]))  # [1, 2, 3]

What interviewers notice

  • Whether you say the approach before you write code.
  • Whether you use the right data structure (a set for 'is x in collection?', not a list).
  • Whether you handle edge cases: empty list, None, zero.
  • Whether you can explain what any line in your code does.

Keep practising

Keep preparing

View all guides
Fresher confidently answering an interview question at a desk7 min read

Behavioral interviews

“Why should we hire you?” — best answers for freshers