Programming Notes: Python Basics
Data types, control flow, functions, files, and testing
What this packet includes
● Lecture recap and concise concept summaries
● Quick-reference tables and formulas
● Worked examples with step-by-step solutions
● Practice questions with answer key
● Glossary of key terms
● Further reading suggestions
Core Syntax
Python is dynamically typed; indentation defines blocks.
Primitives: int, float, bool, str; Collections: list, tuple, dict, set.
Use slicing for sequences: s[a:b:c].
Control and Functions
if/elif/else for branching; for/while for iteration.
Define with def; return values explicitly; document with docstrings.
Comprehensions create lists/dicts concisely; prefer readability.
Files and Testing
Use with open(...) as f: to ensure files close.
Basic testing via assert and doctest; prefer small pure functions for testability.
Time complexity rough guide: O(1), O(log n), O(n), O(n log n), O(n^2).
Common Methods
Type Examples
list append, pop, sort, reverse, extend
dict get, keys, values, items, update
str split, join, replace, find, format
Worked Example: Word count script
● Open a text file and count lines, words, characters.
● Store counts in a dict and print neatly.
● Add a CLI argument for filename and handle FileNotFoundError.
Worked Example: FizzBuzz variants
● Print numbers 1..n replacing multiples of 3 with 'Fizz', 5 with 'Buzz', 15 with 'FizzBuzz'.
● Add unit tests to verify behavior for edge cases.
Practice Questions
● Write a function that returns the nth Fibonacci number iteratively.
● Given a list of dicts, sort by a key with missing values.
● Parse a CSV and compute average of a numeric column.
● Implement a function to check if a string is a palindrome ignoring punctuation.
● Explain why dict lookup is average O(1).
Answer Key (self-check)
Q1 — See solution outline below.
Q2 — See solution outline below.
Q3 — See solution outline below.
Q4 — See solution outline below.
Q5 — See solution outline below.
Glossary
Mutable: Object that can change in place (e.g., list, dict).
Immutable: Object that cannot change in place (e.g., tuple, str).
Comprehension: Concise syntax for building lists/dicts/sets.
Lecture Notes Space
• Note 1: ________________________________
• Note 2: ________________________________
• Note 3: ________________________________
• Note 4: ________________________________
• Note 5: ________________________________
• Note 6: ________________________________
• Note 7: ________________________________
• Note 8: ________________________________
• Note 9: ________________________________
• Note 10: ________________________________
• Note 11: ________________________________
• Note 12: ________________________________
• Note 13: ________________________________
• Note 14: ________________________________
• Note 15: ________________________________
• Note 16: ________________________________
• Note 17: ________________________________
• Note 18: ________________________________
• Note 19: ________________________________
• Note 20: ________________________________
• Note 21: ________________________________
• Note 22: ________________________________
• Note 23: ________________________________
• Note 24: ________________________________
Further Reading
● OpenStax textbooks (free): Calculus, Chemistry, Microeconomics, Statistics, Computer Science
● MIT OpenCourseWare lectures for foundational courses
● Khan Academy topic playlists for quick refreshers