A variable is like a labeled box where you can store information. You give it a name and put a value inside. Later, you can use that name to get the value back.In real life:
Your name is a “variable” that stores what people call you
Your age is a “variable” that stores a number
Your favorite color is a “variable” that stores text
In Python, we create variables to store data our programs need to remember.
user_name = "Dave" # lowercase with underscores (Python style)userName = "Dave" # camelCase (works but not Python style)age2 = 30 # numbers are OK (not at start)_private = "secret" # underscore at start is OK
Not allowed:
Copy
2age = 30 # Can't start with numbermy-name = "Dave" # No hyphens (Python thinks it's subtraction)my name = "Dave" # No spacesclass = "Python" # Can't use Python keywords
In Python, we use lowercase letters with underscores between words. This is called “snake_case” and it’s the standard way to name variables in Python.
Copy
# Good Python stylefirst_name = "Alice"user_age = 25is_logged_in = Trueshopping_cart_total = 49.99# Avoid camelCase (this is for other languages)firstName = "Alice" # Works, but not Python styleuserAge = 25isLoggedIn = True
Use descriptive names for your variables! Instead of x or temp, use names like user_score or file_path. Your future self (and teammates) will thank you when reading the code later.