Python Interview Questions

Fundamentals

What is Python?

Python is a high-level, interpreted language focused on readability and productivity. It supports multiple paradigms (procedural, OOP, functional).

# Hello World
print('Hello Python')
Basics

Core data types

int, float, str, bool, list, tuple, dict, set.

user = {'id': 1, 'name': 'Ada'}
nums = [1,2,3]
tupled = (1, 'a')
unique = {1,2,2}  # {1,2}
Collections

List comprehensions

Concise way to build lists.

squares = [x*x for x in range(5) if x%2==0]  # [0,4,16]
Advanced

Generators

Lazily produce values using yield.

def gen():
  for i in range(3):
    yield i
for v in gen(): print(v)
Advanced

Decorators

Wrap functions to add behavior.

def log(fn):
  def wrapper(*args, **kwargs):
    print('calling', fn.__name__)
    return fn(*args, **kwargs)
  return wrapper

@log
def add(a, b): return a+b
Tooling

Virtual environments

Isolate dependencies using venv or conda.

python -m venv .venv
# activate: Windows
# .venv\Scripts\activate
Error Handling

Exceptions

Use try/except/finally to handle errors.

try:
  1/0
except ZeroDivisionError as e:
  print('error:', e)
finally:
  print('cleanup')
Concurrency

Asyncio basics

Cooperative concurrency with async/await and event loop.

import asyncio

async def work():
  await asyncio.sleep(1)
  return 'done'

async def main():
  res = await asyncio.gather(work(), work())
  print(res)

asyncio.run(main())
OOP

Dataclasses

Simplify class boilerplate with auto-generated __init__, __repr__, and comparisons.

from dataclasses import dataclass
@dataclass
class User:
  id: int
  name: str
Types

Typing module

Add type hints for better tooling and optional static checks (mypy).

from typing import List, Optional

def total(xs: List[int]) -> int:
  return sum(xs)
Basics

Context managers

Use with to manage resources safely; implement via __enter__/__exit__ or contextlib.

Concurrency

Multiprocessing vs threading

Threads share memory but are limited by the GIL for CPU-bound tasks; multiprocessing bypasses GIL at the cost of IPC overhead.

Tooling

Packaging and dependency management

Use pip/poetry for dependencies; publish with build/twine; pin versions and lock to ensure reproducibility.