Overview
Introductory notes for CS50’s Python cover fundamentals through intermediate topics: I/O, data types, control flow, functions, exceptions, data structures, files/CSV, modules and packages, regex, OOP, testing, CLI tools, typing, comprehensions, generators, and APIs.
Course Scope and Structure
- No prior experience required; weekly lectures plus problem sets applying concepts to realistic problems.
- Topics include functions/variables, conditionals, loops, exceptions, libraries, unit tests, file I/O, regex, OOP, procedural/functional styles.
Programming Environment and Running Code
- Use a text editor (VS Code recommended) and CLI to run programs with: python file.py.
- Python is both language and interpreter; interactive mode (REPL) is useful for quick tests.
Core Concepts: Functions, Parameters, Returns
- Functions perform actions, take arguments, and may return values for reuse in further computation.
- Define with def name(...):; use main() to organize logic, define helpers below, and call main() at end.
- Scope limits variable visibility to defining function; return provides outputs.
Basic I/O, Variables, and Strings
- print outputs text; input reads user input, optionally with a prompt.
- Variables store values; assignment uses =, copying right to left; prefer descriptive names.
- Concatenate with + or multiple arguments to print, customizing sep and end named parameters.
Print Function Parameters
| Parameter | Meaning | Default |
|---|
| objects | Values to print (any number) | |
| sep | Separator between objects | " " |
| end | String appended after output | "\n" |
Comments, Pseudocode, and String Handling
- Comments begin with # and improve readability; draft logic with pseudocode before coding.
- Use quoting and escapes for embedded quotes; \n for newline; raw strings r"..." preserve backslashes.
Common String Methods
| Method | Purpose | Example Effect |
|---|
| strip() | Remove leading/trailing whitespace | " a b " → "a b" |
| capitalize() | Uppercase first char, lowercase rest | "dAVID" → "David" |
| title() | Title-case each word | "david maen" → "David Maen" |
| split(sep) | Split into list on separator | "a b" → ["a","b"] |
Numbers, Types, and Formatting
- input returns strings; convert to int or float for arithmetic; + concatenates strings and adds numbers.
- round(x, ndigits) controls precision; floats have finite precision; Python ints are unbounded.
Numeric Operations
| Operator | Meaning |
|---|
| + | Addition / string concatenation |
| - | Subtraction |
| ***** | Multiplication |
| / | Division (float result) |
| % | Modulo (remainder) |
String Formatting with F-Strings
- f"...{expr}..." embeds expressions; format specifiers control output width/precision and grouping.
- Examples: f"{z:,}" for thousands separator; f"{z:0.2f}" for two decimal places.
Style, Debugging, and Documentation Literacy
- Prefer readability over over-nesting; temporary variables can clarify logic.
- Syntax errors come from invalid code; practice improves debugging.
- Read official docs; brackets in signatures mark optional parameters; distinguish parameters vs arguments.
Conditionals and Boolean Logic
- Use if/elif/else for branching; comparators include >, >=, <, <=, ==, !=; = is assignment.
- or is True if any condition is True; and requires all True; simplify mutually exclusive checks.
Structured Conditional Examples
| Scenario | Code/Logic | Output/Behavior |
|---|
| Compare x and y | if x < y; elif x > y; else | Prints relation efficiently |
| Equal vs not equal | if x != y else | Choose appropriate message |
| Grade assignment | if score >= 90, elif >= 80, etc. | Use chained comparisons when helpful |
| Even/Odd | if x % 2 == 0 | Parity check via modulo |
match/case Pattern Matching
- match variable with multiple case options; _ acts as catchall.
- Cleaner alternative to long elif chains for discrete mappings.
Loops: while and for
- while repeats while condition True; ensure updates to avoid infinite loops; Control-C interrupts.
- for iterates over sequences or range(n); use _ for unused loop variables; "text\n" * n repeats text succinctly.
Iteration Patterns
| Pattern | Use Case | Syntax | Notes |
|---|
| Direct list iteration | Process items | for item in items: ... | No indices needed |
| Range-based count | Repeat n times | for _ in range(n): ... | Use _ when unused |
| Index over list | Need positions | for i in range(len(items)): ... | Access items[i] |
| Dict keys | Iterate names/ids | for k in d: ... | Default is keys |
| String multiply | Repeat text | "x\n" * n | Use end="" when needed |
Input Validation Loops and Exceptions
- Use while True with break/continue to re-prompt until valid input.
- try/except handles runtime errors like ValueError from int("cat"); else runs only if try succeeds.
- pass in except suppresses noisy messages to silently re-prompt.
Reusable Input: get_int
- Encapsulate integer prompting; return parsed int; accept prompt parameter for flexibility.
- EAFP style: attempt int(input(prompt)) in try; on ValueError, continue loop.
Indentation Semantics
- Indentation defines blocks for functions, loops, and try/except; enforced for readability and clarity.
Command-Line Arguments with sys
- sys.argv is a list of arguments; argv[0] is program name; validate length to avoid IndexError.
- Use slices like sys.argv[1:] to skip program name; sys.exit("message") exits early on errors.
Modules, Imports, and Standard Library
- import loads functionality; prefer import module then module.func(...) to avoid name collisions.
- random.choice picks an element; random.randint returns inclusive int; random.shuffle shuffles in place.
- statistics.mean computes average of a list.
Third-Party Packages and pip
- Install with pip install package (e.g., cowsay, requests); use guards to validate CLI inputs.
- cowsay renders ASCII art; requests performs HTTP; json prints and parses JSON with dumps and response.json().
JSON and APIs
- JSON maps to Python dicts/lists; iTunes API returns JSON for search queries.
- Build URL with query parameters; parse response JSON; iterate results list to extract fields.
Creating and Using Your Own Modules
- Place reusable functions in a separate .py; protect main logic with if name == "main": to avoid running on import.
Unit Testing: assert and pytest
- Test small, pure functions; assert raises AssertionError on failure; pytest discovers and runs test_* functions.
- Split tests into focused cases; use pytest.raises for exceptions.
File I/O: Text and CSV
- Use with open(...) as file: to manage files; "w" writes/truncates; "a" appends; write adds no newline automatically.
- Read lines and rstrip("\n") to avoid blank lines; sorted(...) orders lines; reverse with reverse=True.
CSV Handling Approaches
| Approach | Tool | Benefit |
|---|
| Manual split | str.split(",") | Simple, but fragile with commas in data |
| csv.reader | csv.reader(file) | Handles quoting; unpack rows cleanly |
| csv.DictReader | csv.DictReader(file) | Header-aware; order-agnostic access |
| csv.writer | writer.writerow([...]) | Proper quoting on write |
| csv.DictWriter | writer.writerow({...}) | Fieldnames control column order |
Data Structures: Lists, Dicts, Sets
- Lists are ordered, zero-indexed sequences; iterate by item or index.
- Dicts map keys to values; iterate keys by default; access values via key.
- Sets store unique items; add for deduplication; sort on output when needed.
Sorting Structures
- Accumulate dicts and sort with key functions; pass named functions or lambdas.
- key functions extract sort keys; reverse=True inverts order.
Sorting Helpers and Lambdas
| Helper | Purpose |
|---|
| get_name(d) | return d["name"] |
| get_house(d) | return d["house"] |
| lambda d: d["name"] | Inline key extractor |
2D Output and Abstraction
- Build print_column(height) and print_row(width) for reuse; nested loops print squares.
- Simplify row printing with "#" * width; outer loop repeats rows.
Binary Files and Images with Pillow
- Distinguish text vs binary files; Pillow (PIL) opens images and saves animated GIFs.
- Load images from CLI args; save first frame with save_all, append_images, duration, and loop parameters.
Pillow GIF Save Parameters
| Parameter | Meaning |
|---|
| save("costume.gif") | Output filename |
| save_all=True | Save multiple frames |
| append_images=[...] | Additional frames |
| duration=200 | Milliseconds per frame |
| loop=0 | Infinite loop |
Regular Expressions: Validation and Extraction
- Patterns match, validate, or transform text; re.search finds matches; use raw strings for backslashes.
Regex Constructs and Uses
| Construct | Meaning | Example | Use Case |
|---|
| ^ / $ | Start/end anchors | ^\w+@[^@]+.(?:com | edu)$ |
| . / . | Any char / literal dot | a.+b vs a.b | Wildcard vs period |
| + / * / ? | 1+ / 0+ / 0 or 1 | \w+, \s*, (sub.)? | Quantity control |
| [] / [^] | Set / complement | [^@]+ | Exclude @ |
| a-z, 0-9 | Ranges | [a-z0-9_] | Username chars |
| \w, \d, \s | Shorthands | \w+.\w+ | Word-like tokens |
| () / (?:) | Capture / non-capture | (com | edu), (?:www.)? |
| ** | ** | Alternation | (com |
| flags | Regex options | re.IGNORECASE | Case-insensitive |
Regex in Practice
- Email: enforce single @ with [^@]+ on both sides, escape dot in .edu, add ^...$ anchors.
- Names: capture "Last, First" as ^(.+),\s?(.+)$ and reorder groups to "First Last".
- Twitter: optional protocol and www., domain twitter.com/, capture username ([a-z0-9_]+) with re.IGNORECASE.
- Use walrus operator (:=) to assign match objects inside conditions; re.sub for substitutions.
Object-Oriented Programming (OOP) Basics
- Model entities with classes; create instances; methods operate on self; attributes store state.
- init initializes and validates; str defines friendly string representation.
Returning Multiple Values and Data Choices
- Return tuples and unpack, but immutability prevents edits; lists are mutable but positional.
- Prefer dicts for clarity (named fields) or classes for structure and behavior.
Properties, Encapsulation, and Validation
- Use @property getters and @x.setter setters to validate on assignment; store with underscore (e.g., _house).
- Assign via self.attr in init to route through setter; underscore signals “private” by convention.
Class and Instance Methods; Class Variables
- Class variables are shared across instances; instance variables are per object.
- @classmethod receives cls; suitable for factories/utilities like Student.get().
- Example “Sorting Hat” uses class variable houses and @classmethod sort to choose a house.
Inheritance and Operator Overloading
- Factor shared logic into superclasses; call super().init in subclasses.
- Overload operators with special methods like add to define behavior for domain objects.
Globals vs Locals; OOP for State
- Reading globals works; assigning without global raises UnboundLocalError; shadowing hides globals locally.
- Encapsulate mutable state (e.g., Account balance) in a class; expose read-only property and methods for updates.
Account Class Components
| Component | Purpose | Key Details |
|---|
| class Account | Bank account | Encapsulates state/behavior |
| init | Initialize | self._balance = 0 |
| @property balance | Read-only | Return self._balance |
| deposit / withdraw | Mutations | Update internal state |
Constants, Type Hints, and mypy
- Use ALL_CAPS constants by convention; class-level constants for shared values.
- Add type hints to parameters and returns; mypy checks for mismatches and missing returns.
Type Hint Examples and Checks
| Code Element | Hint/Check | Outcome |
|---|
| Parameter n: int | Must pass int | mypy flags str from input |
| Local number: int = input() | Incompatible assignment | mypy reports mismatch |
| -> None / -> str | Return types | mypy detects violations |
| int(...) conversion | Runtime cast | Aligns types with hints |
Docstrings and Documentation
- Triple-quoted docstrings document purpose, params, types, raises, returns; tools can extract and test them.
Docstring Structure Example
| Section | Content |
|---|
| Summary | Brief one-line description |
| Param/Type | Parameter name and type |
| Raises | Exceptions that may occur |
| Returns/Rtype | Description and return type |
Command-Line Parsing with argparse
- Prefer argparse over manual sys.argv for validated types, defaults, and help messages.
argparse Usage Pattern
| Step | Code/Concept | Notes |
|---|
| Create parser | ArgumentParser(description=...) | Program overview |
| Add arg | add_argument("-n", type=int, default=1) | Automatic conversion |
| Parse args | args = parse_args() | Uses sys.argv |
| Use args | for _ in range(args.n): ... | Clean logic |
| Help/Errors | -h/--help | Auto usage/errors |
Unpacking, Varargs, and Functional Tools
- Use * to unpack sequences to positional args; ** unpacks dicts to named args.
- Define *args and **kwargs to accept variable arguments; built-ins like print use this pattern.
- map applies a function to each item; Class.method references (e.g., str.upper) can be passed directly.
Comprehensions, filter, and Sorting
- List and dict comprehensions provide concise transformations and filtering; filter selects by predicate.
- Use sorted with key lambdas to order collections by fields; split long comprehensions for readability.
Transformation and Filtering Approaches
| Approach | Purpose | Signature | Example | Output |
|---|
| map | Transform each item | map(func, iterable) | map(str.upper, words) | All items transformed |
| filter | Keep items matching | filter(func, iterable) | filter(is_gryffindor, students) | Subset of items |
| List comp | Transform/filter | [expr for x in it if cond] | [s["name"] for s in students if s["house"]=="Gryffindor"] | New list |
enumerate and Generators
- enumerate yields (index, value) pairs for indexed iteration, replacing range(len(...)).
- Generators with yield produce items lazily, reducing memory and improving responsiveness for large outputs.
Eager vs Generator Patterns
| Pattern | Eager list | Generator |
|---|
| Function body | Build list, return list | for ...: yield item |
| Memory | Stores entire list | Stores iteration state only |
| Responsiveness | Delayed completion | Immediate streaming |
Key Terms & Definitions
- Function: Reusable action; may take parameters and return values.
- Argument/Parameter: Passed value vs named input in definition.
- Return value: Result provided by a function.
- Variable: Named storage; assigned with =.
- String/Integer/Float: Text, whole numbers, real numbers with decimals.
- Method: Function bound to a type or class instance.
- Concatenation: Joining strings end-to-end.
- CLI/REPL: Command line interface; interactive interpreter.
- Bug/Comment: Code error; non-executable note prefixed by #.
- F-string: Formatted string literal with embedded expressions.
- Escape character: Backslash for special characters.
- Boolean expression: True/False expression.
- Flowchart: Diagram of control flow.
- For/While loop: Iteration constructs for sequences and conditions.
- Exception/try-except-else/pass: Runtime error handling constructs.
- break/return: Exit loop; exit function (and loop).
- sys.argv/sys.exit: Command-line args; terminate program with message.
- Module/Package: Reusable .py file; collection of modules, installable via pip.
- JSON: Text format mapping to dicts/lists.
- CSV: Comma-separated text rows; columns separated by commas.
- DictReader/DictWriter: CSV utilities using headers for dict mapping.
- Lambda: Anonymous function; often for key functions.
- Regex/Anchors/Groups/Flags: Pattern matching components and options.
- Class/Object/Attribute/Method: OOP constructs; data and behavior organization.
- init/str: Special methods for initialization and string representation.
- Property/Decorator: Managed attribute; @ syntax to modify behavior.
- Classmethod/Inheritance/super: Class-level behavior; reuse and extend classes.
- Operator overloading: Define operator behavior via special methods.
- Set: Unordered unique collection.
- Global/Local/Shadowing: Variable scope concepts.
- Constant (convention): ALL_CAPS name signaling immutability by convention.
- Type hint/mypy: Static typing annotations and checker.
- Docstring: In-code documentation for tools and readers.
- argparse: CLI argument parsing library.
- Unpacking/*args/**kwargs: Flexible argument passing techniques.
- map/filter/comprehension/enumerate/generator: Functional and iteration tools.
Action Items / Next Steps
- Practice writing scripts using input, print, variables, string methods, and type conversions.
- Refactor code into functions with parameters, defaults, and return values; use main() organization.
- Validate input with while True and try/except; encapsulate as get_int(prompt).
- Explore standard modules (random, statistics), third-party packages (cowsay, requests), and JSON APIs.
- Use csv.DictReader/DictWriter for robust CSV parsing and writing; sort structured data with key functions.
- Build and test classes with init, str, properties, classmethods, and inheritance; consider operator overloading where natural.
- Add unit tests with pytest; convert side-effect functions to return values for easier testing.
- Adopt f-strings and formatting specs; type hints with mypy; document with docstrings.
- Prefer argparse for CLIs; use unpacking, *args/**kwargs, map/filter, comprehensions, enumerate, and generators for clean, efficient code._