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
No comments:
Post a Comment