Saturday, March 28, 2026

1.3 Python 3.28.26

 

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:

    1. Using curly braces with elements

    2. 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

Sunday, March 22, 2026

1.2 Python 3.22.26

 

1.2 Gen AI: Python Refresher

Sun, 22 Mar 26

Scalar Data

  • Integer - whole numbers without fractional parts

  • Float - decimal numbers representing fractional parts

  • Boolean - true or false values

  • Complex data type - complex numbers like a + bj where a is the real part and b is imaginary part

    • Example: 3+4j where real part is 3.0 and imaginary part is 4.0

Aggregated Data

  • Collection of values organized in different structures

  • String - sequence of characters in single quotes, double quotes, or triple quotes

  • Set - {} unordered collection of unique items in Python

  • List - [] ordered collection of items which are mutable

  • Tuple - () ordered collection that are immutable

  • Dictionaries - {} unordered collection of key-value pairs

  • Goal is to store data efficiently and understand how it works with AI

Data Assignment

  • Python variables are references to objects, but actual data is contained in the objects

  • Use ‘id()’ function to get the memory location

  • Same memory location example:

    • x=34, y=x

    • print(x, id(x)) shows x = 34; id of x: 123

    • y = 34; id of y: 123 (same memory location)

  • Different memory location example:

    • y=78 creates new integer object

    • x = 34; id 123

    • y = 78; id 456

  • References verified using ‘id(x)’ function

Do the demo ‘Demo_05_Storing_user_Information…’

  • Successfully completed in Jupyter notebook

Taking user inputs of different data types

name = input("Enter your name: ") # String input age = int(input("Enter your age: ")) # Integer input height = float(input("Enter your height in meters: ")) # Float input active = input("Are you active? (yes/no): ").lower() == "yes" # Boolean input

Displaying the stored information

print("\nUser Information:") print(f"Name: {name} (Type: {type(name)})") print(f"Age: {age} (Type: {type(age)})") print(f"Height: {height} meters (Type: {type(height)})") print(f"Active Status: {active} (Type: {type(active)})")

Python Operators

  • Operators are symbols or keywords to perform operations

  • Arithmetic operators: +, -, *, /, %, //, **

    • Single division (/) gives float output with integer and fractional parts

    • Double division (//) gives integer part only

    • Modulo (%) gives remainder when dividing two numbers

  • Assignment operators: =, +=

  • Comparison operators: ==, !=, <, >, <=, >=

  • Logical operators:

    • and (true if both statements are true)

    • or (true if one statement is true)

    • not (reverses result - true if false)

  • Miscellaneous operators:

    • is (True if both variables are same object)

    • is not (True if both variables are not the same object)

    • Example: a={1,2,3}, b={1,2,3} returns false, but if a=b then true

    • in (True if value is present in object)

    • not in (True if value is not present in the object)

Lists

  • Ordered, mutable collection that can hold different data types

  • Created using square brackets []

  • Supports indexing (positive and negative)

  • Allows slicing to extract subsets

  • Key methods:

    • append() - adds element at end

    • pop() - removes element at specified index

    • extend() - adds multiple elements from another list

Tuples

  • Ordered, immutable collection of elements

  • Created using parentheses ()

  • Faster than lists for iteration

  • Supports indexing and slicing like lists

  • Tuple packing - assigning multiple values to single tuple

  • Tuple unpacking - extracting values into separate variables

  • Cannot modify elements once created

Dictionaries

  • Mutable, ordered collection of key-value pairs

  • Created using curly braces {}

  • Provides fast lookups using keys

  • Dictionary methods demonstrated:

    • .keys() - returns all keys

    • .values() - returns all values

    • .items() - returns key-value pairs as tuples

    • .pop() - removes and returns value for given key

  • Nested dictionaries supported for complex data structures

Do Demo 6

  • Completed comparison operators demo successfully

Do the ‘Guided Setup’ for ‘Lesson 1’

  • Guided practice available with questions and answers for self-study

  • Designed to reinforce concepts without grading