Python Interview Questions for Freshers (with Answers)
Python interviews for entry-level roles test whether you understand how the language behaves, not how many libraries you can name. Most rounds mix concept questions with a short live coding task on strings, lists or dictionaries.
Core language
What is the difference between a list and a tuple?
A list is mutable: you can add, remove and change elements. A tuple is immutable, slightly faster, and can be used as a dictionary key or set member if its contents are hashable. Use a tuple for a fixed record, a list for a collection that changes.
Which types are mutable and which are immutable?
int, float, bool, str, tuple and frozenset are immutable. list, dict, set and most user-defined objects are mutable. It matters because a mutable object passed to a function can be changed by it.
What is the difference between == and is?
== checks whether two objects have equal values. is checks whether they are the same object in memory. Use is only for None, True and False.
What are *args and **kwargs?
*args collects extra positional arguments into a tuple and **kwargs collects extra keyword arguments into a dict, so a function can accept any number of arguments.
Why is a mutable default argument a bug?
Default values are created once, when the function is defined. def add(x, items=[]) reuses the same list across calls, so items grows every time. Use items=None and create the list inside the function.
What is the difference between a shallow copy and a deep copy?
A shallow copy (copy.copy, list slicing) creates a new outer object but shares the nested objects. A deep copy (copy.deepcopy) copies everything recursively, so changes to nested data do not affect the original.
What is a list comprehension?
A compact way to build a list from an iterable: [x * x for x in nums if x % 2 == 0]. It is usually clearer and faster than the equivalent for loop with append.
Functions, iterators and classes
What is a decorator?
A function that takes another function and returns a new one that adds behaviour before or after it, applied with the @name syntax. Logging, timing, caching and access checks are common uses.
What is a generator and why use one?
A function that uses yield to produce values one at a time, pausing between them. It never builds the whole sequence in memory, so it suits large files and streams.
What is the difference between @staticmethod and @classmethod?
A class method receives the class as its first argument (cls) and is often used for alternative constructors. A static method receives neither the instance nor the class; it is a plain function kept inside the class for organisation.
What is __init__, and what is self?
__init__ is the initialiser that runs when an object is created and sets its attributes. self is the instance the method is called on; Python passes it automatically as the first argument.
What is the GIL?
The Global Interpreter Lock in CPython lets only one thread execute Python bytecode at a time. Threads still help for I/O-bound work such as network calls; for CPU-bound work you use multiprocessing or libraries that release the GIL.
How does exception handling work in Python?
Code that may fail goes in try; except catches specific exception types; else runs if nothing was raised; finally always runs and is used for clean-up. Catch the narrowest exception you can instead of a bare except.
What does the with statement do?
It runs a block inside a context manager that guarantees clean-up, for example with open(path) as f: closes the file even if an error occurs.
Coding tasks to practise
- Reverse the words in a sentence; check whether two strings are anagrams.
- Find the first non-repeating character using a dictionary or collections.Counter.
- Remove duplicates from a list while preserving order.
- Flatten a nested list.
- Read a file and count word frequencies.
- Find the two numbers in a list that add up to a target, in O(n).