Skip to content
Kuza Ndoto Academy

Python Interview Questions

Beginner → IntermediateChapter 10 · Page 55 of 567 min readUpdated 15 Nov 2026

These are questions that often come up in interviews for internships and junior Python roles, with short model answers. Don't memorise the wording: make sure you understand each idea well enough to explain it in your own words and back it up with a tiny example.

Core language

1. What is Python, and what are its main features?

Python is a high-level, general-purpose programming language known for readable syntax. It is interpreted (code is run by an interpreter without a separate compile step you manage), dynamically typed (variables don't have declared types; values do), and it has automatic memory management, a large standard library and a huge ecosystem of packages for web development, data science, AI and automation.

2. What is the difference between a list and a tuple?

Both are ordered sequences that can hold mixed types. A list is mutable (you can add, remove and change items) and uses square brackets. A tuple is immutable and uses round brackets. Tuples are slightly faster and lighter, can be used as dictionary keys (if their contents are hashable), and signal "this data shouldn't change", for example GPS coordinates. See Tuples.

3. What are mutable and immutable types?

A mutable object can be changed after it's created; an immutable one can't. Lists, dictionaries and sets are mutable. Integers, floats, strings, tuples and frozensets are immutable: "changing" a string actually creates a new string. This matters because two variables can refer to the same mutable object, so a change through one is visible through the other.

4. What is the difference between == and is?

== checks whether two values are equal. is checks whether two names refer to the same object in memory. Use is mainly for None: if value is None:.

a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b, a is b, a is c)
True False True

5. What is the difference between append() and extend()?

first = [1, 2]
first.append([3, 4])     # adds ONE item (the whole list)
second = [1, 2]
second.extend([3, 4])    # adds EACH item
print(first)
print(second)
[1, 2, [3, 4]] [1, 2, 3, 4]

Functions

6. What are *args and **kwargs?

They let a function accept any number of arguments. *args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dictionary. The names are conventions; the * and ** are what matter.

def show(*args, **kwargs):
    print(args, kwargs)

show(1, 2, name="Ali", county="Kilifi")
(1, 2) {'name': 'Ali', 'county': 'Kilifi'}

7. What is the problem with mutable default arguments?

Default values are created once, when the function is defined, not each time it's called. A list default is therefore shared between calls:

def add_item(item, basket=[]):
    basket.append(item)
    return basket

print(add_item("pen"))
print(add_item("book"))
['pen'] ['pen', 'book']

The fix is to use None as the default and create a new list inside the function: if basket is None: basket = [].

8. What is a decorator?

A decorator is a function that takes another function and returns a new function that adds behaviour around it, such as logging, timing, caching or checking permissions, without changing the original code. @my_decorator above def f is shorthand for f = my_decorator(f). Examples include @property, @functools.cache and web framework route decorators. See Decorators.

9. What is a generator, and why use one?

A generator is a function that uses yield to produce values one at a time, pausing between them. It's lazy: values are created only when requested, so memory use stays tiny even for millions of items or infinite sequences. Generator expressions such as (x * x for x in data) are the one-line version. The trade-off: a generator can only be iterated once. See Iterators & Generators.

10. What is the difference between a shallow copy and a deep copy?

A shallow copy creates a new outer container but shares the inner objects. A deep copy recursively copies everything.

import copy

original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)

original[0].append(99)
print(shallow)
print(deep)
[[1, 2, 99], [3, 4]] [[1, 2], [3, 4]]

Object-oriented programming

11. What is self, and what does __init__ do?

self is the first parameter of an instance method and refers to the object the method is called on; Python passes it automatically. __init__ is the initialiser that runs when an object is created, and it's where you set up the object's attributes with self.name = name. See The __init__ Method.

12. Explain inheritance and polymorphism.

Inheritance lets a child class reuse and extend a parent class's attributes and methods (class Manager(Employee):), with super() to call the parent's version. Polymorphism means different classes can be used through the same interface: if each class has a pay() method, code can call method.pay() without knowing the exact class. In Python this often relies on duck typing rather than a shared parent.

13. What is the difference between __str__ and __repr__?

Both return a string describing an object. __str__ is the readable, user-friendly version used by print(). __repr__ is the unambiguous, developer-focused version used in the shell and in lists, ideally looking like code that recreates the object, such as Money(1500, 'KES'). If only __repr__ is defined, Python uses it for both.

Errors, modules and the runtime

14. How does exception handling work in Python?

Code that might fail goes in a try block. If an exception occurs, Python jumps to the first matching except block. An optional else runs only if no exception occurred, and finally always runs, which makes it ideal for clean-up. You can raise your own exceptions with raise, and create custom ones by subclassing Exception. Good practice is to catch specific exceptions, never a bare except:. See Try, Except & Finally.

15. What does if __name__ == "__main__": do?

Every module has a __name__ variable. It's set to "__main__" when the file is run directly, and to the module's name when it's imported. Code inside this if block therefore runs only when the file is executed as a program, not when another file imports it. It's used for a script's entry point, demos and quick tests. See Creating Your Own Modules.

16. What is a virtual environment, and why use one?

A virtual environment is an isolated folder with its own Python interpreter and installed packages, created with python -m venv .venv. It keeps each project's dependencies separate, so two projects can use different versions of the same library, and it lets you record exact versions in requirements.txt for others to reproduce. See pip & Virtual Environments.

17. What is the GIL?

The Global Interpreter Lock is a mechanism in CPython (the standard Python implementation) that allows only one thread to execute Python bytecode at a time. Threads still help with I/O-bound work such as downloading files, because the lock is released while waiting. For CPU-heavy work, developers traditionally use the multiprocessing module or libraries that do work outside Python. Python 3.13 introduced an optional, experimental "free-threaded" build without the GIL, but the standard build still has it.

Interview tips

  • Think out loud. Interviewers care about how you reason, not just the final answer.
  • Use small examples. A three-line snippet often explains an idea better than a definition.
  • Admit what you don't know, then explain how you'd find out (documentation, a quick experiment in the shell).
  • Practise coding by hand. Common warm-ups include reversing a string (text[::-1]), counting words with a dictionary or Counter, FizzBuzz and finding duplicates in a list.
  • Show a project. Being able to walk through your own budget tracker or weather app is very persuasive.

Key points

  • Know the fundamentals deeply: mutability, == vs is, lists vs tuples, copying.
  • Be able to explain function features: *args/**kwargs, defaults, decorators and generators.
  • Understand OOP basics: classes, self, inheritance, polymorphism and dunder methods.
  • Back up every answer with a small, correct example, and test yourself with the Python Quiz.
Up next · Projects & PracticePython QuizTest yourself across the whole series.
→

Tip: use the ← and → keys to move between pages.