Lesson 4: Variables and Data Types
Understand how Python stores and manages information using variables and different data types.
Lesson 4: Variables and Data Types
Programming isn't just about printing text; it's about handling information. To do this, we use Variables. Think of a variable as a labeled box where you can store a piece of data so you can use it later.
Lesson Overview
In this lesson, we will:
- Create Variables: Learning how to "declare" and "assign" data.
- Explore Data Types: Understanding the difference between text, numbers, and logic.
- Variable Naming: The rules for naming your "boxes."
- Changing Variables: Seeing how data can evolve in a program.
1. What is a Variable?
In Python, you create a variable by giving it a name and setting it equal to a value.
# Create a variable named 'username' and store the text "PixelCoder"
username = "PixelCoder"
# Create a variable named 'level' and store the number 10
level = 10
print(username)
print(level)
Key Concept: The = sign in Python doesn't mean "is equal to" (like in math). It means "Assign this value to this name."
2. Basic Data Types
Python automatically figures out what kind of data you are storing. These are the four most common types:
A. Strings (str)
Used for text. Must always be inside quotes (single ' or double ").
name = "Alex"
message = 'Welcome to Python!'
B. Integers (int)
Used for whole numbers (positive or negative). No decimal points.
age = 25
score = -5
C. Floats (float)
Used for decimal numbers.
price = 19.99
pi = 3.14159
D. Booleans (bool)
Used for logic. Can only be True or False. (Note the capital letters!)
is_game_over = False
is_logged_in = True
3. Why Data Types Matter
You can do different things with different types. For example, you can add two integers together, but you can't subtract a string from a boolean!
# Math with numbers
health = 100
damage = 20
remaining_health = health - damage # Result: 80
# Connecting strings (this is called concatenation)
greeting = "Hello " + "World" # Result: "Hello World"
4. Variable Naming Rules
Python has a few rules for what you can name your variables:
- Must start with a letter or an underscore
_. - Cannot start with a number.
- Can only contain letters, numbers, and underscores (no spaces or hyphens!).
- Case-sensitive:
Nameandnameare two different boxes.
Pro Tip (Snake Case): In Python, we usually use underscores to separate words in variable names, like player_score or is_user_authorized.
Practice Exercise: The Personal Profile
Create a new file named profile.py. Create the following variables and print them:
first_name(a string)last_name(a string)age(an integer)height_in_meters(a float)loves_linux(a boolean)
Bonus: Try to add first_name and last_name together with a space in between to create a full_name variable!
Quick Knowledge Check
- What symbol do we use to assign a value to a variable?
- What is the difference between an integer and a float?
- Which of these names is invalid:
user_1,1_user, or_user? - What data type would you use to store whether a user has a subscription?
Key Takeaways
- Variables store information for later use.
- The assignment operator is
=. - Strings = Text, Integers = Whole Numbers, Floats = Decimals, Booleans = True/False.
- Python is case-sensitive and uses snake_case for naming.
What’s Next?
We know how to store data, but how do we get data from the user? In Lesson 5, we’ll learn about Input and Output to make our programs interactive!