Overview
This lecture series introduces programming with Python, covering key concepts from functions, variables, and conditionals to more advanced topics like object-oriented programming, regular expressions, file IO, and additional Python features and libraries.
Functions & Variables
- Functions group reusable code into named blocks for specific tasks.
- Variables store data values (e.g., numbers, strings) in memory for later use.
- Assignment uses
=
to copy a value into a variable (not mathematical equality).
Conditionals & Loops
- Conditionals (
if
, elif
, else
) let code make decisions based on Boolean expressions.
- Comparison operators:
>
, <
, >=
, <=
, ==
(equality), and !=
(not equal).
- Loops (
while
, for
) repeat actions multiple times; range()
generates sequences for loops.
- The
break
keyword exits a loop early; continue
skips to the next iteration.
Data Types & Structures
- Strings (
str
), Integers (int
), Floats (float
), Booleans (bool
) are basic types.
- Lists store ordered, mutable collections; dictionaries (
dict
) store key-value pairs.
- Tuples are immutable ordered collections; sets store unique elements.
Input/Output & Exceptions
input()
reads user input as a string; type conversion may be needed.
- File I/O uses
open()
, reading/writing files with context managers (with
statement) for safety.
- Exceptions handle errors gracefully with
try
, except
, else
, and finally
blocks.
- Custom exceptions can be raised with
raise
.
Libraries & Modules
- Libraries (or modules) let you reuse third-party or your own code through
import
.
- Examples:
random
(randomness), statistics
(averages), csv
(comma-separated files), argparse
(command line arguments).
Unit Testing
- Unit tests automatically check code correctness using functions like
assert
.
- Tools like
pytest
help automate running multiple tests efficiently.
Regular Expressions
- Regular expressions (
re
module) define patterns to validate or extract information from strings.
- Special symbols include
.
, *
, +
, ?
, []
, ^
, $
, and escape sequences like \d
.
Object-Oriented Programming (OOP)
- Classes define custom data types; objects are instances of classes.
- Instance variables (attributes) and methods (functions) encapsulate data and behavior.
- Special methods:
__init__
(constructor), __str__
(string representation), property decorators, operator overloading (__add__
).
- Inheritance allows a class to inherit attributes and methods from another class.
Advanced Python Features
- List comprehensions and dictionary comprehensions create collections concisely, often with conditionals.
- Generators (
yield
) allow functions to return a sequence of values one at a time, saving memory.
- Type hints annotate expected argument and return types for clarity and static analysis (
mypy
).
- Docstrings with triple quotes document functions for later reference.
Key Terms & Definitions
- Function — Named block of code that performs a specific task.
- Variable — Symbolic name storing a value in memory.
- Boolean Expression — A statement that is either
True
or False
.
- Exception — An error detected during execution that can be handled using
try/except
.
- Library/Module — A file or collection of files containing reusable Python code.
- Unit Test — Code that verifies the correctness of other code.
- Regular Expression — Pattern describing sets of strings for matching text.
- Class — Blueprint for creating objects (instances) with specific attributes and methods.
- Instance Variable — Data unique to each object created from a class.
- Property — Controlled access to instance variables using getter/setter methods.
- List/Dictonary Comprehension — Shorthand for creating lists or dictionaries with a single expression.
- Generator — Function that yields values one at a time using
yield
.
Action Items / Next Steps
- Practice with weekly problem sets focused on key concepts.
- Review and annotate Python documentation for unfamiliar functions or modules.
- Experiment with file I/O, exception handling, and regular expressions on sample data.
- Try writing your own classes, methods, and unit tests for a small project.
- Explore third-party Python packages and their documentation.