๐ ๐ Python Variables#
๐ฅจ Introduction to Variables: โYo, Whatโs a Variable?โ#
In Philly terms, a variable is like a cheesesteak order: it holds something specific and meaningful (like โWhiz witโ means โCheesesteak with Cheese Whiz and onionsโ).
Definition:#
A variable is a named container that stores data, like a string, number, or list.
Why use variables?#
To organize your code.
To make your code reusable.
To make your data easy to reference and modify.
# Example: Philly Cheesesteak Orders
order_1 = "Whiz wit"
order_2 = "American witout"
print(order_1, "and", order_2)
Whiz wit and American witout
๐๏ธ Variable Naming Rules: โKeep it Respectable Like the Liberty Bellโ#
Rules for Naming Variables:#
Must start with a letter or underscore (
_
).Can include letters, numbers, and underscores, but no spaces or special characters.
Are case-sensitive (
hoagie
andHoagie
are different).Use descriptive names (donโt be like a lost tourist at the Italian Market).
# Good Variable Name
hoagie_order = "Italian with extra peppers"
# Bad Variable Name
x = "What kind of order is this?"
โพ Variable Types: โFrom Soft Pretzels to Phillies Home Runsโ#
In Python, variables can hold different data types. Hereโs a quick rundown:
Common Variable Types:#
Integer (
int
): Whole numbers, like the number of pretzels in your bag.Float (
float
): Numbers with decimals, like the price of a cheesesteak.String (
str
): Text, like the names of Philly landmarks.Boolean (
bool
): True/False, like โIs Wawa the best? True.โList (
list
): A collection, like all your favorite toppings.
# Examples
pretzel_count = 5 # Integer
cheesesteak_price = 12.99 # Float
landmark = "Liberty Bell" # String
is_wawa_best = True # Boolean
favorite_toppings = ["onions", "peppers", "mushrooms"] # List
๐ Assigning Variables: โCatch the SEPTA of Python Logicโ#
You can assign values to variables using the =
operator. Itโs like deciding your subway route: Broad Street Line or Market-Frankford Line?
# Broad Street Line Cheesesteak
broad_st_line = "Pat's"
# Market-Frankford Line Cheesesteak
market_frankford_line = "Geno's"
๐ถ Variable Reassignment: โChange It Up Like the Schuylkill River Tideโ#
Variables can change their value during the program.
favorite_sport = "Baseball" # Phillies!
print("Favorite sport:", favorite_sport)
# Changing the value
favorite_sport = "Football" # Eagles!
print("Updated favorite sport:", favorite_sport)
Favorite sport: Baseball
Updated favorite sport: Football
๐ Multi-Variable Assignment: โJawn Everything at Onceโ#
In Philly, โjawnโ means anything and everything. Python lets you assign multiple variables at once, like grabbing everything you need for a South Philly tailgate.
# Assigning multiple variables at once
hoagie, pretzel, soda = "Italian", "Soft Pretzel", "Birch Beer"
print(hoagie, pretzel, soda)
Italian Soft Pretzel Birch Beer
๐ Constants: โAlways True Like Rocky Balboaโ#
Constants are variables that donโt change (by convention, written in all caps).
# Example: Philly's Famous Landmark
CITY = "Philadelphia"
BEST_FOOD = "Cheesesteak"
๐ ๏ธ Variable Type Conversion: โSwitch It Up Like a Philly Foodieโ#
Sometimes, you need to convert variables to a different type.
# Example: Pretzel Math
pretzel_price = "2.50" # String
total_price = float(pretzel_price) * 3 # Convert to float and calculate
print("Total price for 3 pretzels: $", total_price)
Total price for 3 pretzels: $ 7.5
๐จ Fun Philly Example: Build Your Cheesesteak#
Hereโs a fun example where you build a cheesesteak order using variables:
# Build your Philly Cheesesteak
bread = "Amoroso roll"
meat = "ribeye steak"
cheese = "Cheez Whiz"
toppings = ["fried onions", "mushrooms"]
# Display the order
print("Your cheesesteak order:")
print(f"Base: {bread}, {meat}, with {cheese} and toppings: {', '.join(toppings)}")
Your cheesesteak order:
Base: Amoroso roll, ribeye steak, with Cheez Whiz and toppings: fried onions, mushrooms