Module 2: Variables and Data Types
Now that you can write a basic program, it's time to learn how to work with data. This module introduces variables—the fundamental way programs remember information.
1. What is a Variable? The "Box" Analogy
Imagine you have a collection of boxes. Each box can hold something inside, and to keep things organized, you put a label on each box. A **variable** in programming is exactly like one of these labeled boxes. It's a named storage location in the computer's memory that holds a value. You give it a name (the label), and you can put data inside it.
You can change the contents of the box, and you can look inside to see what it holds. This ability to store and manage data is what makes programming powerful.
🧠 Core Concept: The Labeled Box
Variable Name: The label you write on the box (e.g., `user_age`).
Value: The item you put inside the box (e.g., the number `25`).
Assignment (`=`): The act of putting an item into the box.
In Python, creating a variable is simple. You use the assignment operator (`=`) to give a value to a variable name. This is called **declaration and initialization**.
Here, we've created three variables: `player_score` which holds the number 100, `player_name` which holds the text "Alex", and `is_game_over` which holds a false value. We can now use these names in our code to refer to their values. For instance, we can print them:
This would display "Alex" and "100" on the screen.
Rules for Naming Variables
Python has a few simple rules for naming your variables:
- A variable name must start with a letter or an underscore (`_`). It cannot start with a number.
- The rest of the name can contain letters, numbers, and underscores.
- Variable names are case-sensitive. `score`, `Score`, and `SCORE` are three different variables.
- It's a best practice (called a convention) to use `snake_case` for variable names in Python, where words are lowercase and separated by underscores. This makes names like `player_high_score` easy to read.
2. The Building Blocks: Primitive Data Types
The "item" you put inside your variable box has a **type**. Is it a number? Is it text? Is it a simple yes/no? Python needs to know the type of data to understand how to work with it. We'll start with the four most common and basic types, often called **primitive data types**.
Data Type | Python Name | Description | Example |
---|---|---|---|
Integer | `int` | Whole numbers, positive or negative, without decimals. | `10`, `-5`, `0`, `12345` |
Float | `float` | Numbers with a decimal point. Used for representing fractional values. | `3.14`, `-0.001`, `2.0` |
String | `str` | A sequence of characters, used for text. Must be enclosed in single (`'`) or double (`"`) quotes. | `"Hello"`, `'Python'`, `"123"` |
Boolean | `bool` | Represents one of two values: `True` or `False`. Used for logical operations. | `True`, `False` |
One of Python's most beginner-friendly features is **dynamic typing**. This means you don't have to explicitly tell Python the data type of a variable. Python automatically figures it out based on the value you assign.
3. Inspecting a Variable's Type
What if you're not sure what type of data a variable holds? Python gives you a handy built-in function called `type()` to find out. This is very useful for debugging and understanding your code.
Let's use the `type()` function on the variables we created earlier:
Running this code will produce the following output, showing you how Python classifies each value:
<class 'float'>
<class 'str'>
<class 'bool'>
4. Changing Types: Casting
Sometimes you need to convert a value from one data type to another. This process is called **type casting** or **type conversion**. For example, you might get a number as user input, but it will arrive as a string. To perform mathematical calculations, you'll need to convert it to an `int` or `float`.
Python provides simple functions to do this: `int()`, `float()`, and `str()`.
Example 1: Converting to an Integer
Let's say a user enters their age. The `input()` function (which we'll cover later) always gives us a string. We need to cast it to an integer.
Example 2: Converting to a String
You can't easily join numbers and strings together. To display a number alongside text, you often need to convert the number to a string first.
⚠️ Be Careful with Casting!
You can only cast values that make sense. For example, `int("123")` works perfectly, but `int("hello")` will cause a `ValueError` because the string "hello" cannot be interpreted as a whole number.
You've Completed Module 2!
Excellent work! You now understand the most critical concept in programming: how to store, label, and manage different types of data using variables. This skill is the bedrock of everything you will build from now on.
Next up, we'll learn how to make your programs intelligent by enabling them to make decisions based on this data.
On to Module 3: Control Structures & Decision Making →