1.3 Gen AI: Python Refresher
Sat, 28 Mar 26
Python Data Structures Overview
Lists: ordered, mutable collections using square brackets
Support indexing, slicing, append/remove operations
Can contain mixed data types
Tuples: ordered, immutable collections using parentheses
Cannot be modified after creation
More memory efficient than lists
Dictionaries: key-value pairs using curly braces
Keys must be unique and immutable
Support nested dictionaries for complex data structures
Sets: unordered collections of unique elements
Automatically remove duplicates
Support mathematical operations (union, intersection, difference)
Set Operations and Use Cases
Union: combines unique elements from multiple sets
Intersection: returns common elements between sets
Difference: returns elements in one set but not another
Primary use case: removing duplicates from data collections
Creating sets:
Using curly braces with elements
Using set() constructor for empty sets
Conditional Statements (if/elif/else)
Basic if statement: executes code block when condition is true
if/else: provides alternative execution path
if/elif/else: allows checking multiple conditions sequentially
Nested conditionals: if statements within other if statements
Increases code complexity and runtime overhead
Python evaluates as false: numerical zero, boolean False, empty strings/lists/tuples/dictionaries
Loop Structures
For loops: iterate over sequences (lists, strings, tuples)
Syntax: for item in iterable:
Can iterate through each character in strings
While loops: execute while condition remains true
Requires manual counter management
Risk of infinite loops if condition never becomes false
Nested loops: loops within loops
Expensive in terms of computational resources
Outer loop completes full inner loop cycle for each iteration
Loop Control Statements
Break statement: terminates loop execution immediately
Exits loop when specific condition is met
Useful for searching operations
Continue statement: skips current iteration, proceeds to next
Allows filtering during iteration
Does not terminate entire loop
Loop with else: executes else block when loop completes normally
Else block skipped if break statement used
List Comprehensions
Concise syntax for creating lists in single statement
Format: [expression for item in iterable if condition]
Benefits:
Reduces code lines
Improves readability
Better performance than traditional loops
Dictionary comprehensions: {key: value for item in iterable}
Set comprehensions: {expression for item in iterable}
Programming Best Practices Covered
Code readability: use meaningful variable names
Performance optimization: avoid excessive nested loops
Memory efficiency: choose appropriate data structures
Modularity: break complex problems into smaller parts
Use AI assistants (Gemini, Copilot) for:
Adding code comments
Fixing indentation
Debugging assistance
Next Session Preview
Python functions and advantages
Code reusability and modularity
Function creation and calling
Arguments and parameters
Object-oriented programming basics
Hands-on demos requiring Colab or VS Code setup
Recommendation: have development environment ready before next class
No comments:
Post a Comment