Python Fundamentals
Transition to Python to prototype faster with readable, expressive code.
Content
Installing Python and venv
Versions:
Watch & Learn
AI-discovered learning video
Sign in to watch the learning video for this topic.
Installing Python and venv — CS50 next step after C data structures
You just finished building sets, maps, and graphs in C. Now imagine doing the same work without malloc drama or manual hash functions. Welcome to Python land — where productivity shows up wearing slippers.
Why this matters (and why your past C work helps)
You learned how data structures are implemented under the hood in C: memory management, collisions in hash tables, adjacency lists for graphs. That knowledge is gold. In Python, built-in structures like dict, set, and high-level graph libraries (e.g., networkx) let you test ideas fast without rewriting low-level plumbing.
But before you sprint into coding, you need a solid development sandbox. That sandbox is a local Python installation plus a virtual environment (venv). Install them properly and you avoid version conflicts, global package mess, and mysterious runtime errors that make you cry into your keyboard at 2 AM.
What we will cover
- How to install Python on macOS, Linux, and Windows
- What a venv is, and why you should always use one
- Creating, activating, and using a venv with examples
- Tying it back to CS50: testing data structures in Python
1) Install Python
Check first
Open a terminal (or PowerShell on Windows) and run:
python3 --version
# or
python --version
If it prints a version like 3.10.x or 3.11.x, you already have Python. If not, install it below.
macOS
- Easiest for many: Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install python
- Or download from python.org installer.
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install python3 python3-venv python3-pip
Windows
- Download from python.org and check "Add Python to PATH" in the installer.
- Or use the Microsoft Store version (works, but sometimes awkward for scripts).
- If using PowerShell, you might prefer running:
python --versionafter install.
Troubleshooting tip: if running python starts Python 2.7 (legacy), prefer calling python3 or adjust PATH to point to the modern Python.
2) What is venv and why use it?
- venv creates an isolated directory with its own Python interpreter and site-packages.
- It prevents global package collisions: you can have project A depend on networkx 2.x and project B on networkx 3.x without fights.
- It keeps your system Python clean. When you break things inside a venv, you just delete the folder and restart. No surgery required.
Micro-explanation: In C you might compile multiple programs linking different versions of libraries with care. venv gives you that per-project isolation automatically for Python packages.
3) Create and activate a venv (step-by-step)
From your project folder, pick a name for the venv (common: venv or .venv):
macOS/Linux
# create
python3 -m venv .venv
# activate
source .venv/bin/activate
# deactivate later
deactivate
Windows (PowerShell)
# create
python -m venv .venv
# activate
.\.venv\Scripts\Activate.ps1
# or (old style)
.\.venv\Scripts\activate.bat
# deactivate
deactivate
When activated, your shell prompt usually prefixes with the venv name — a friendly reminder you are inside the sandbox.
Installing packages inside venv
pip install networkx numpy
# freeze versions for reproducibility
pip freeze > requirements.txt
Recreate the environment (on another machine or later):
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
4) Quick demo: reimplementing a small C idea in Python
Remember writing an adjacency list in C? In Python it's one-liner concise and readable:
from collections import defaultdict
graph = defaultdict(list)
# add edge u->v
graph['A'].append('B')
graph['A'].append('C')
Or use networkx for more features:
import networkx as nx
G = nx.DiGraph()
G.add_edge('A','B')
G.add_edge('A','C')
print(list(G.successors('A')))
See? No malloc, no free, no pointer arithmetic. Your C experience still helps because you understand the tradeoffs: Python's dicts/sets are powerful but have different performance and memory characteristics.
5) Extra practical tips
- Use a descriptive venv name if you juggle multiple versions, e.g., .venv-py311.
- Commit requirements.txt to your repo, not the venv folder.
- Want multiple Python versions? Try pyenv alongside venvs.
- Virtualenv vs venv: venv is built-in and usually enough for CS50 projects. virtualenv gives some extras but you rarely need them.
- If Windows blocks Activate.ps1 due to execution policy, run PowerShell as admin and set:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser.
Key takeaways
- Install Python using the method that matches your OS and workflow; verify with
python3 --version. - Always create a venv per project:
python3 -m venv .venvthen activate. - Use pip and requirements.txt to manage dependencies reproducibly.
- Your C data structure knowledge transfers: Python hides memory management but the complexity and tradeoffs remain.
This is the moment where the concept finally clicks: set up the environment first, then code fearlessly.
Final micro-assignments (5 minutes each)
- Install Python and create a venv for a new folder called cs50_py.
- Activate the venv and install networkx.
- Write a 10-line script that constructs a small graph and prints neighbors.
- pip freeze > requirements.txt and add it to a git repo (but not the .venv folder).
Go on. Do that now. Then come back and we will port one of your C implementations to Python and compare runtime and memory behavior like the pros you are becoming.
Tags: beginner, python, cs50, venv, humorous
Comments (0)
Please sign in to leave a comment.
No comments yet. Be the first to comment!