Top 20 Python Interview Questions for Freshers (with Answers)
GoCode Team25 June 20263 min read
Python is the most common language freshers name on their resumes — which means interviewers have a well-worn set of questions to test whether that line is real. Here are twenty that come up constantly, with answers short enough to actually say in an interview.
Core language
- List vs tuple? Lists are mutable, tuples are immutable. Tuples can be dict keys and are slightly faster; use them for fixed collections.
- What is a dictionary? A hash-map of key–value pairs with average O(1) lookup. Keys must be hashable.
- How is memory managed? Reference counting plus a cycle-detecting garbage collector, all managed by the Python runtime.
- What is the GIL? The Global Interpreter Lock lets only one thread execute Python bytecode at a time — so threads help I/O-bound work, while CPU-bound work needs multiprocessing.
isvs==?==compares values;iscompares identity (same object in memory).- Shallow vs deep copy? Shallow copies the outer container but shares nested objects; deep copy duplicates everything recursively.
- What are *args and **kwargs? They collect extra positional and keyword arguments so a function can accept a variable number of inputs.
- Mutable default argument trap? Defaults are evaluated once at definition; a default list persists across calls. Use
Noneand create the list inside.
Functions and OOP
- What is a lambda? An anonymous single-expression function, typically used as a key function for
sorted,minormax. - What is a decorator? A function that takes a function and returns an enhanced version — used for logging, timing, authentication.
- What is a generator? A function with
yieldthat produces values lazily, saving memory on large sequences. - List comprehension? A concise way to build lists:
[x*x for x in nums if x > 0]— say it reads as "collect x squared for every positive x". - @staticmethod vs @classmethod? Static methods get no implicit first argument; class methods receive the class itself and are commonly used as alternative constructors.
- How does inheritance work? A class can extend one or more parents; method lookup follows the Method Resolution Order (MRO).
- What are dunder methods? Double-underscore hooks like
__init__,__str__,__len__that let your objects work with built-in syntax.
Practical
- How do you handle exceptions?
try/except/else/finally; catch specific exceptions, never a bareexcept. - How do you read a file safely?
with open(path) as f:— the context manager closes it even on errors. - What is a virtual environment? An isolated interpreter + packages per project, so dependencies don't clash system-wide.
- How would you remove duplicates while keeping order?
list(dict.fromkeys(items))— dicts preserve insertion order. - How do you reverse a string? Slicing:
s[::-1]. Be ready to also do it with a loop if asked.
Interview tip: for every answer here, be ready with a two-line code example. "Can you show me?" is the standard follow-up — and where prepared candidates separate themselves.
Practice writing these in a real editor with instant feedback rather than reading them the night before — recall under pressure is what the interview actually tests.