Python is used everywhere: web development, automation, data science, AI and security tooling. These Python interview questions and answers cover the language features interviewers ask about most, with short code examples you can run.

Basics
1. What kind of language is Python?
Python is a high-level, interpreted, dynamically typed, general-purpose language. It emphasises readable code and has a large standard library. CPython, the reference implementation, compiles code to bytecode and runs it on a virtual machine.
2. Explain dynamic typing and duck typing.
Variables do not have fixed types; values do. With duck typing, Python cares about what an object can do, not its class. If an object has the methods you call, it works. Type hints, checked by tools like mypy, add optional static checking.
3. Mutable vs immutable types?
- Mutable: list, dict, set and most custom objects.
- Immutable: int, float, str, tuple and frozenset.
Mutability matters because changing a mutable object affects every variable that refers to it.
4. List vs tuple?
Lists are mutable and use square brackets. Tuples are immutable and use parentheses. Tuples can be dictionary keys, and they signal that data should not change.
5. is vs ==?
== compares values. is checks whether two names refer to the same object. Use is mainly for None: if x is None.
Functions and data
6. What are *args and **kwargs?
They collect extra positional arguments into a tuple and extra keyword arguments into a dictionary.
7. Why is a mutable default argument a bug?
Default values are created once, when the function is defined. So a default list keeps changes between calls. Use None instead:
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items8. What is a list comprehension?
A compact way to build lists: squares = [n * n for n in range(10) if n % 2 == 0]. Python also has dictionary and set comprehensions.
9. Shallow copy vs deep copy?
A shallow copy (copy.copy) copies the outer object but shares nested objects. A deep copy (copy.deepcopy) copies everything recursively.
10. How do you write a prime-checking function?
def is_prime(n: int) -> bool:
if n < 2:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return TrueAdvanced features
11. What is a decorator?
A function that wraps another function to add behaviour, such as logging or timing, without changing its code. You apply it with @decorator_name.
12. Generators vs lists?
A generator produces values one at a time with yield, so it uses little memory. A list stores all values at once. Generators suit large files and streams.
13. What is a context manager?
An object used with with that sets up and cleans up resources automatically, such as closing files: with open("log.txt") as f:.
14. What is the GIL?
The Global Interpreter Lock in CPython lets only one thread execute Python bytecode at a time. Threads still help with I/O-bound work. For CPU-bound work, use multiprocessing or native extensions. Recent Python versions also offer an optional free-threaded build without the GIL.
15. How does exception handling work?
try:
value = int(user_input)
except ValueError:
print("Not a number")
else:
print("Success")
finally:
print("Always runs")Tooling
16. Why use virtual environments?
They isolate project dependencies. Create one with python -m venv .venv, activate it and install packages with pip. Tools such as Poetry and uv add dependency locking.
17. What is PEP 8?
The official style guide for Python code: naming, indentation, line length and more. Formatters such as Black and linters such as Ruff help enforce it.
For automation practice, see our shell scripting interview questions.
Keep preparing
Practise each answer out loud, and run the commands in a lab rather than memorising them. Interviewers often ask follow-up questions, so make sure you understand why each answer is true.
Sources
Preparing for an interview? Browse more questions in our interview preparation hub. For businesses, we also offer security services such as VAPT, server hardening and ISO 27001 readiness.
