Programming Languages

Python

A readable, dynamically typed, batteries-included language. Fast to write, slower to run, dominant for scripting, data, and glue.

Level: beginner

What is it?

Python is a dynamically typed, garbage-collected, general-purpose language designed for readability. The reference implementation, CPython, compiles source to bytecode and interprets it. Everything is an object, indentation delimits blocks, and the standard library covers files, networking, JSON, testing, and much more.

Why does it matter?

Python has the shortest path from an idea to a working program, and a huge ecosystem for data analysis, machine learning, automation, and web back ends. It is also a good language for learning: you can express algorithms without ceremony.

Mental model

A very capable Swiss Army knife with the blade guard on. It is quick and friendly for almost everything, and when you need the raw speed of a specialized tool, you call out to one — C, NumPy, or a database — from Python.

How it works

  • Dynamic typing with optional hints. Duck typing: what matters is the methods an object has.
  • Built-in containers: list, tuple, dict, set — with comprehensions like [x * x for x in nums if x > 0].
  • CPython's global interpreter lock (GIL) lets only one thread run Python bytecode at a time, so CPU-bound work uses processes or native extensions, while I/O-bound work can use threads or asyncio.
  • Virtual environments (venv) and pip isolate dependencies per project.
  • Generators and iterators let you process data lazily instead of building whole lists.
  • Slower per operation than compiled languages, so move hot loops to libraries or algorithms with better complexity.

Steps

  • Write source
  • CPython compiles to bytecode
  • The interpreter loop runs it
  • Objects live on the heap
  • Reference counts and GC reclaim them

Code example: Batteries included: count words in a few lines

from collections import Counter

text = "the quick brown fox jumps over the lazy dog the end"
counts = Counter(text.split())
print(counts.most_common(1))   # [('the', 3)]

Real-world usage

  • Scripting, automation, and glue code
  • Data analysis and machine learning
  • Web back ends (Django, FastAPI, Flask) and command-line tools

Common mistakes

  • Using threads for CPU-bound parallelism and expecting speed-ups under the GIL.
  • Mutable default arguments and modifying a list while iterating over it.
  • Building huge lists when a generator would stream the data.

Interview questions

What is the GIL and what does it mean for concurrency?

The global interpreter lock lets one thread execute Python bytecode at a time in CPython. Threads still help with I/O waits, but CPU-bound Python code needs multiprocessing, native extensions, or a different implementation.

What is the difference between a list and a tuple?

Lists are mutable and tuples are immutable. Tuples can be dictionary keys and signal a fixed record; lists are for collections that change.

How does Python manage memory?

Objects live on the heap and are reference counted, so most are freed the moment the count reaches zero. A cycle collector reclaims reference cycles.

Prerequisites

Related concepts

Continue learning

Browse the full computer science library