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.

The Art of Naming (PEP 8)

Naming Conventions

Python follows the PEP 8 style guide. For variables, we use snake_case.

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.

Understanding Data Types

The Four Core Types

Python automatically detects what kind of data you are storing:

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.

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.

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!

Avoiding Common Pitfalls

Watch Your Step!

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.