Python Programming I
Module 1: Core Fundamentals & Memory Architecture
Variables and Memory
Variables in Python are dynamically typed and act merely as references to objects in memory, rather than containers of data types. Everything in Python is an object, meaning an assignment like binds the identifier to an integer object with the value . When executing code, assignments evaluate from right to left, e.g.,
Built-in Data Types & Mathematical Representations
Python includes primary types such as boolean, integer, float, and string.
Integer: Arbitrary size variable-length objects representing integral numbers with absolute precision.
Float: Represents floating-point numbers using an approximated bit pattern format. The stored value is evaluated via the formula:
where the and are integers extracted from the memory bit pattern, and the is fixed.
Boolean: Subtype of integer evaluated as (
False) or (True).String: Characters mapped to bit patterns (e.g., UTF-8).
Module 2: Control Flow & Logic
Conditions and Branching
Python evaluates boolean expressions using short-circuit logic for and operators. Truth value testing natively interprets empty sequences, , and None as False, while all other objects default to True. Branching relies on if, elif, and else statements parsed top-to-bottom, executing exclusively one matched block.
Loops
- While loops: Execute continuously until a given boolean condition is evaluated as
False. - For loops: Iterate sequentially over iterables (like strings or lists).
Loop behavior can be interrupted utilizing (exits the loop entirely) or (forces the next iteration).
Module 3: Data Structures and Mutability
Core Collections
- Lists: Mutable sequences defining an ordered collection of mixed types.
- Tuples: Immutable sequences.
- Sets: Unordered collections of unique, hashable elements supporting mathematical operations like union and intersection.
- Dictionaries: Mutable, ordered mapping structures comprised of key-value pairs where keys must be hashable.
Indexing and Slicing
Sequence types allow range selection through a specific slicing syntax yielding a new view or copy:
where indices start at , is inclusive, is exclusive, and negative integers evaluate backwards.
The Alias Effect
When assigning a mutable object (like a list) to multiple variables, , both variables reference the identical memory object. Changes made through explicitly alter the object, rendering the mutation visible when accessing .
Module 4: Functions & Program Structure
Arguments and Parameters
Functions provide modularization to eliminate code duplication. They ingest data via formal parameters (definition) and actual parameters/arguments (invocation). Variable-length arguments are collected in tuples ( ) and dictionaries ( ).
Namespaces and Scope
Variables are resolved hierarchically across nested dictionaries mapping names to objects. Python searches scopes sequentially:
- Local namespace
- Enclosing namespace
- Global namespace
- Built-in namespace
Recursion & Generators
A recursive function continuously invokes itself to solve decreasingly complex sub-problems until triggering a base case. For example, a power function computing breaks down into the mathematical subtask:
and terminates when reaching the base recursion anchor:
where is returned directly.
Generators return elements lazily to conserve memory using the yield keyword, suspending their state between iterations.
Module 5: Exceptions & Debugging
Exceptions govern error handling. Python evaluates try, except, else, and finally blocks.
except: Intercepts specific exception types passed upwards via the calling hierarchy.else: Executes exclusively if no exception is raised.finally: Executes unconditionally for cleanup routines.
Breakpoints and the call stack allow developers to pause execution and analyze hierarchical variable states across namespaces.
Module 6: Object-Oriented Programming (OOP)
Classes and Objects
Classes operate as architectural blueprints generating uniquely instanced objects. Objects combine state/data ( ) and behavioral functions ( ). The special instantiation method __init__(self) initializes instance-specific attributes natively bound to the calling object.
Inheritance & Advanced Object Mapping
Subclasses extend base classes, explicitly inheriting all parent attributes and methods. All Python classes derive implicitly from the root object class.
If an invoked method requires resolution, Python determines the exact execution path through the Method Resolution Order (MRO) via a bottom-up hierarchy search.
Python supports syntactic operator overloading by overriding inherited dual-underscore "dunder" methods natively implemented within the object class. E.g., altering the operator's behavior demands overriding:
Module 7: Libraries and Ecosystem
File I/O & Modules
The open() function handles disk streams via relative or absolute paths, configured for binary (rb, wb) or string/text environments (r, w, a). The with statement serves as a context manager ensuring resource closure. Code structures are grouped logically within isolated files (modules) and hierarchical directories containing an __init__.py file (packages).
NumPy and Fast Computations
NumPy bypasses slow Python looping architectures by processing contiguous, uniformly-typed data blocks via multidimensional arrays implemented in C. For an -dimensional array flattened into a fast 1D array using row-major order, the memory offset address of an element located at row and column , given representing the total number of elements per row, is determined mathematically via: