Section Overview
In this section, you’ll learn how Python stores information using variables. You’ll also be introduced to Python’s basic data types: strings, numbers, and booleans. These are the building blocks of most Python programs.
By the end of this section, you should be able to:
- Create and use variables in Python
- Identify and work with different data types
- Convert between strings and numbers when needed
Lesson 1: What Is a Variable?
A variable is a name that stores a value. Think of it like a labeled box: the label is the variable’s name, and the contents are the value it holds.
Example:
name = "Alice"
age = 30
In this example:
name
stores a string ("Alice")age
stores a number (30)
Once you've stored a value, you can use it elsewhere in your program:
print(name)
print(age + 5)
Lesson 2: Data Types in Python
Python has several built-in data types. Here are the three most important for now:
- String (
str
) – text, surrounded by quotes
Example: "hello"
, 'Python'
- Integer (
int
) – whole numbers
Example: 5
, -42
, 2024
- Boolean (
bool
) – True or False values
Example: True
, False
You can check a variable’s type using the type()
function:
height = 175 print(type(height)) # Output: <class 'int'>
Lesson 3: Working with Strings and Numbers
Strings and numbers are not the same thing—even if they look similar.
age = "30" # This is a string
real_age = 30 # This is a number
print(age + age)
# Output: 3030 (string concatenation) print(real_age + real_age) # Output: 60 (numeric addition)
To convert between them, use:
int()
– convert string to numberstr()
– convert number to string
Example:
age = input("What is your age? ")
future_age = int(age) + 5 print("In five years, you'll be " + str(future_age) + ".")
Lesson 4: Naming Variables
Variable names should be descriptive, and they must follow these rules:
- Use letters, numbers, and underscores only
- Must start with a letter or underscore (not a number)
- Avoid spaces or special characters
Good examples:
user_name = "Ella"
total_score = 92
Bad examples:
2name = "Invalid" # starts with a number
user-name = "Error" # hyphens aren't allowed
Stick to lowercase with underscores for readability (called "snake_case").
Quiz: Check Your Understanding
1. Which of these is a valid variable name?
a) user-name
b) 2score
c) total_score
Answer: c) total_score
2. What data type is returned by the input()
function?
a) Integer
b) Boolean
c) String
Answer: c) String
3. What does this code output?
x = "4"
y = 4 print(x + str(y))
Answer: 44 (string concatenation)
4. True or False: You can add an integer directly to a string in Python.
Answer: False – you must convert the integer to a string first using str()
.
Practice Exercise: Age Calculator
Write a program that:
- Asks the user for their name
- Asks for their current age
- Calculates how old they will be in 10 years
- Prints a message like:
"Hi Mia, in 10 years you will be 35."
Starter code:
name = input("What is your name? ")
age = input("How old are you? ")
age_in_10 = int(age) + 10 print("Hi " + name + ", in 10 years you will be " + str(age_in_10) + ".")
Challenge: Add one more line to print what year that will be.