๐Ÿ“– ๐Ÿ““ 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:#

  1. Must start with a letter or underscore (_).

  2. Can include letters, numbers, and underscores, but no spaces or special characters.

  3. Are case-sensitive (hoagie and Hoagie are different).

  4. 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:#

  1. Integer (int): Whole numbers, like the number of pretzels in your bag.

  2. Float (float): Numbers with decimals, like the price of a cheesesteak.

  3. String (str): Text, like the names of Philly landmarks.

  4. Boolean (bool): True/False, like โ€œIs Wawa the best? True.โ€

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