Variables and Basic Data Types
Variables: Your Labeled Boxes
Storing Data
In Python, variables are like labeled boxes. You give the box a name, and you put data inside it using the assignment operator (=).
This allows your program to remember and reuse information later.
Welcome to your first step in coding! Think of a variable as a labeled box. You give it a name, like 'user_age', and then you place a value inside using the equals sign. Now, whenever your program sees 'user_age', it knows you mean the number 25.
- Variables act as containers for data values.
- The '=' symbol assigns a value to a name.
- Variables make code reusable and readable.
The Art of Naming (PEP 8)
Naming Conventions
Python follows the PEP 8 style guide. For variables, we use snake_case.
- Do:
user_score,total_price - Don't:
1st_place(cannot start with a digit) - Don't:
UserScore(CamelCase is for other things)
Naming things correctly is a superpower. In Python, we use 'snake_case'—that means all lowercase letters with underscores between words. 'user_age' is perfect. But be careful! You can't start a name with a number, and remember that Python is case-sensitive, so 'Age' with a capital A is a totally different box than 'age' with a lowercase a.
- Use snake_case: all lowercase with underscores.
- Names cannot start with a digit.
- Python is case-sensitive: 'Age' and 'age' are different.
Understanding Data Types
The Four Core Types
Python automatically detects what kind of data you are storing:
- Integers (int): Whole numbers (10, -5).
- Floats (float): Decimals (3.14, 20.0).
- Strings (str): Text in quotes ("Hello").
- Booleans (bool): Logical True or False.
Python is smart enough to know what's in the box. Whole numbers are called Integers. If there's a decimal point, it's a Float. Text data, wrapped in quotes, is a String. And finally, Booleans are simple logical values: either True or False.
- Integers are for counting whole units.
- Floats are for precise measurements.
- Strings always require quotes.
- Booleans represent logic (on/off).
Math and Formatting
Calculations & f-strings
You can perform arithmetic directly with variables using +, -, *, and /.
To display results nicely, use an f-string: f"Text {variable}".
Variables aren't just for sitting in boxes; we can use them! You can multiply a 'price' variable by a 'quantity' variable to get a total. To tell the user the result, use an f-string. Just put an 'f' before your quotes and put the variable name inside curly braces. It's the cleanest way to format your output.
- Arithmetic operators: +, -, *, /.
- Division (/) always returns a float.
- f-strings (formatted strings) combine text and variables easily.
Code a User Message
Imagine you have two variables: name = "Alex" and score = 50. Write an f-string that would output: Alex has 50 points!
Now it's your turn. Try writing the f-string needed to print the message 'Alex has 50 points!' using the variables provided. Remember the 'f' and the curly braces!
- Start with f and quotes.
- Use curly braces {} for variables.
- Match the exact output format.
Avoiding Common Pitfalls
Watch Your Step!
- Type Mismatch: You can't add text and numbers directly (
"Age: " + 25). - Case Sensitivity:
scoreis notScore. - Type Checking: Use
type(variable)if you're unsure.
Even pros make mistakes. A common one is trying to add a number to a string using a plus sign—Python will throw an error! Use f-strings instead. Also, always check your spelling and capitalization. If you ever get confused about what's inside a box, just use the 'type' function to ask Python directly.
- Never mix strings and numbers with a '+' sign.
- Verify variable types using the type() function.
- Double-check capitalization in variable names.