InterviewSkill

Python Interview Questions

Core Python concepts for software, data, ML, and AI engineering interviews.

8 questions
Python

What is the difference between a list and a tuple?medium

Type
conceptual
Topic
list-tuple
Frequency
common
Tags
list, tuple
Answer

Lists are mutable, while tuples are immutable.

Explanation

Lists can be changed after creation, while tuples are fixed. Tuples are useful for stable records and can be hashable when their values are hashable.

Follow-upWhen would you choose a tuple over a list?

What are decorators?medium

Type
conceptual
Topic
decorators
Frequency
common
Tags
decorators
Answer

Decorators wrap functions or classes to extend behavior.

Explanation

A decorator takes a callable and returns another callable, often used for logging, caching, validation, authorization, or timing without changing the original body.

Follow-upHow do decorators with arguments work?

What is the GIL?hard

Type
conceptual
Topic
gil
Frequency
common
Tags
gil
Answer

The Global Interpreter Lock allows one CPython thread to execute Python bytecode at a time.

Explanation

The GIL simplifies memory management but limits CPU-bound threading. Threads still help for I/O-bound work, while multiprocessing helps CPU-bound work.

Follow-upWhy can Python threads still improve I/O-heavy programs?

What is a generator?medium

Type
conceptual
Topic
generator
Frequency
common
Tags
generator
Answer

A generator lazily produces values using yield.

Explanation

Generators keep only their current state in memory, making them useful for streams, large files, and pipelines where a full list would be expensive.

Follow-upHow is a generator expression different from a list comprehension?

What is the difference between shallow copy and deep copy?medium

Type
conceptual
Topic
shallow-copy-deep-copy
Frequency
common
Tags
shallow, copy, deep, copy
Answer

A shallow copy copies the outer object; a deep copy recursively copies nested objects.

Explanation

Shallow copies still share nested mutable values, while deep copies duplicate nested values to avoid accidental shared mutation.

Follow-upWhen can deep copy be risky or expensive?

Why are mutable default arguments risky in Python?medium

Type
conceptual
Topic
mutable-default-arguments
Frequency
common
Tags
functions, defaults, mutability
Answer

They are created once at function definition time, so later calls can share the same object.

Explanation

A default like items=[] keeps state across calls, which can create surprising bugs. Use None as the default and create a new list or dict inside the function.

Follow-upHow would you safely default a list argument?

What problem do context managers solve?medium

Type
conceptual
Topic
context-managers
Frequency
common
Tags
context-manager, resources, exceptions
Answer

They guarantee setup and cleanup around a block of code.

Explanation

Context managers power patterns like with open(...) because __enter__ prepares the resource and __exit__ releases it even when an exception occurs.

Follow-upHow would you create a custom context manager?

What is the difference between an iterable and an iterator?medium

Type
conceptual
Topic
iterable-iterator
Frequency
common
Tags
iterable, iterator, iteration
Answer

An iterable can create an iterator; an iterator returns values one at a time with next().

Explanation

Lists are iterable because iter(list) returns an iterator. An iterator keeps iteration state and raises StopIteration when exhausted.

Follow-upWhy can an exhausted iterator not be reused directly?