Overview
This lecture series introduces programming concepts using Python, starting with foundational topics like functions, variables, conditionals, loops, and advancing through exceptions, file I/O, regular expressions, and object-oriented programming. The course emphasizes practical problem-solving, best practices, and exposure to Pythonβs extensive features and libraries.
Course Introduction & Structure
- Course requires no prior programming experience.
- Begins with functions and variables, followed by conditionals, loops, exceptions, libraries, testing, file I/O, regular expressions, and object-oriented programming (OOP).
- Weekly lectures introduce concepts, followed by problem sets for hands-on practice.
- Visual Studio Code (VS Code) or other text editors can be used for coding.
Programming Basics: Functions, Variables, and Syntax
- Code is written as plain text, typically saved as .py files for Python.
- The print function outputs data to the screen and accepts arguments (inputs).
- Arguments influence function behavior, e.g., print("Hello World").
- Bugs are inevitable mistakes in code, often called syntax errors or logical errors.
- Syntax is strict; even small typos can cause errors.
User Input, Variables, and Data Types
- The input function retrieves user input (always as string/text).
- Variables store values for reuse; assignment operator (=) assigns values.
- To use user input in other functions, assign it to a variable and reference that variable.
- Comments (#) add notes to code for clarity; pseudocode outlines logic before coding.
- Strings are sequences of text; integers (int) are whole numbers without decimals.
- Convert input to integers (int(input())) before mathematical operations.
- Strings can be cleaned with methods like strip(), capitalize(), and title().
String Manipulation & Methods
- String concatenation uses + or formatting (e.g., f-strings for dynamic insertion).
- Multiple print arguments are separated by commas and add spaces automatically.
- String methods like strip(), capitalize(), title(), and split() help clean and process input.
Error Handling and Testing
- Syntax errors are fixed by correcting code structure.
- Runtime errors (like ValueError) occur during execution and can be caught with try/except blocks.
- Defensive programming anticipates and handles invalid inputs.
Conditionals and Logical Operators
- If, elif, and else are used to perform actions based on conditions.
- Comparison operators: >, >=, <, <=, == (equality), != (not equal).
- Logical operators: and, or combine multiple conditions.
- Indentation and colons are required to define code blocks in Python.
Loops and Iteration
- While loops repeat actions until a condition is false; risk of infinite loops.
- For loops iterate over lists, ranges, or items in data structures.
- The range() function generates a sequence of numbers for iteration.
- Break and continue control loop flow.
Data Structures: Lists, Dictionaries, and Sets
- Lists store ordered collections; items accessed by index (starting at 0).
- Dictionaries store key-value pairs for direct access.
- Sets store unique values without duplicates.
- Use list.append(), dict[key]=value, set.add() to add items.
File I/O and CSV Handling
- open(filename, "w" or "a" or "r") creates, appends, or reads files.
- Use with open(...) as file: for automatic file closing.
- The csv module handles reading/writing CSV files; DictReader/DictWriter map headers to data.
Regular Expressions (Regex)
- Regex defines text patterns for matching or extracting information.
- re.search(), re.sub(), and related functions match or replace patterns.
- Common symbols: . (any char), * (0+), + (1+), ? (0/1), ^ (start), $ (end), [] (set), | (or).
- Capture groups (parentheses) extract parts of a match.
Object-Oriented Programming (OOP)
- Classes define custom data types (templates); objects are instances.
- init initializes object attributes; self refers to the instance.
- Attributes (instance variables) store object-specific data.
- Methods are functions inside classes (including special str for representations).
- Properties (decorators @property, @setter) control access and validation.
- Inheritance allows new classes to reuse functionality from existing classes.
- Operator overloading enables custom behavior for operators like +.
Advanced Python Tools & Features
- Unit testing ensures code correctness; pytest automates tests.
- Type hints (e.g., def func(x: int) -> str) annotate variable and return types; mypy checks types.
- Global variables can be modified in functions using the global keyword.
- List and dictionary comprehensions allow concise data processing.
- Generators (yield) produce values one at a time, conserving memory.
- Command-line arguments are parsed using argparse for robust user input.
- Docstrings document functions/classes for automated help and documentation.
Key Terms & Definitions
- Function β Named block that performs actions, can accept arguments and return values.
- Variable β Named storage for data values in a program.
- String (str) β Sequence of text characters.
- Integer (int) β Whole number without decimal point.
- List β Ordered, mutable collection of items.
- Dictionary (dict) β Key-value pairs for fast lookups.
- Set β Unordered collection of unique items.
- Exception β Runtime error that can be caught and handled.
- Class β Blueprint for custom data types in OOP.
- Object β Instance of a class.
- Method β Function defined inside a class.
- Property β Controlled attribute access in classes.
- Inheritance β Reusing and extending functionality of another class.
- Regular Expression (Regex) β Pattern-matching tool for strings.
- Unit Test β Code that tests specific parts of a program for correctness.
- Type Hint β Annotation indicating variable/function expected data type.
- Generator β Function that yields values one at a time.
- Comprehension β Compact syntax to build lists/dicts/sets from iterables.
Action Items / Next Steps
- Complete any assigned problem sets for hands-on practice.
- Explore Pythonβs official documentation and tutorials for further learning.
- Practice writing and testing your own small projects using discussed concepts.
- Experiment with libraries like argparse, csv, and pytest.
- Apply object-oriented programming concepts to model real-world problems.