Variables and Data Types (int, float, str, bool)
Friendly introduction
Imagine your computer is a backpack. Variables are like labeled pockets where you store different kinds of things. Some pockets hold whole numbers, some hold decimals, some hold words, and some hold true/false answers. In Python, these "kinds of things" are called data types.
In this lesson, you'll learn:
- What variables are
- The 4 basic data types: int, float, str, bool
- How to use them in simple programs
What is a variable?
A variable is a name that stores a value in your program.
Example:
score = 10
player_name = "Alex"
is_game_over = False
print(score)
print(player_name)
print(is_game_over)Tips for naming variables:
- Use letters, numbers, and underscores (_) only.
- Don't start with a number. Example: age2 is OK, 2age is not.
- No spaces. Use underscores: total_apples
- Python is case-sensitive: Age and age are different.
The 4 basic data types
a) int (integer)
Whole numbers (no decimals), like -5, 0, 12.
apples = 5
bananas = 3
total_fruit = apples + bananas
print(total_fruit) # 8b) float
Numbers with decimals, like 3.14, 2.0, -0.5.
price = 2.5
quantity = 3
cost = price * quantity
print(cost) # 7.5c) str (string)
Text inside quotes. You can use single ' or double " quotes.
greeting = "Hello"
name = 'Maya'
message = greeting + ", " + name + "!"
print(message) # Hello, Maya!d) bool (boolean)
True or False (must be capitalized).
is_raining = True
have_umbrella = False
need_umbrella = is_raining and not have_umbrella
print(need_umbrella) # TrueHow to check a variable's type
Use the type() function to see what kind of data is stored.
age = 14
height = 1.62
nickname = "Sky"
is_student = True
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(nickname)) # <class 'str'>
print(type(is_student))# <class 'bool'>Converting between types (casting)
Sometimes you need to change a value's type:
- int(x): to an integer
- float(x): to a float
- str(x): to a string
- bool(x): to a boolean
Examples:
# str to int
age_text = "14"
age_number = int(age_text)
print(age_number + 1) # 15
# int to str for printing
points = 20
print("You have " + str(points) + " points.") # You have 20 points.
# float to int (decimals get cut off)
pi = 3.14
print(int(pi)) # 3
# bool conversion
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False (empty string)
print(bool("Hi")) # True (non-empty string)Mini activity: Build a "Student Card"
Task:
- Create these variables with your own info:
- name (str)
- age (int)
- height_m (float) e.g., 1.55
- has_bus_pass (bool) True or False
- Print a neat summary in 2 lines:
- Line 1: Name and age next year
- Line 2: Height and whether you have a bus pass
- Print the type of each variable using type().
Starter code:
# 1) Make variables
name = "YourNameHere"
age = 14
height_m = 1.60
has_bus_pass = True
# 2) Print a summary (try f-strings or +)
print("Name: " + name + " | Age next year: " + str(age + 1))
print("Height: " + str(height_m) + " m | Bus pass: " + str(has_bus_pass))
# 3) Check types
print(type(name))
print(type(age))
print(type(height_m))
print(type(has_bus_pass))Optional challenge:
Change height_m to a string with 2 decimals. Hint: use round(height_m, 2) or format with f"{height_m:.2f}".
Summary
- Variables store values you can use later.
- int: whole numbers
- float: decimals
- str: text
- bool: True/False
- Use type() to check a variable's type.
- Convert types with int(), float(), str(), bool() when needed.
Great job! You now know the building blocks for almost every Python program.