Variables & Data Types
Every program works with data. In this lesson you'll learn how Python stores, labels, and works with different kinds of information.
type() to inspect data typesWhat Is a Variable?
Think of a variable as a labeled container. You give it a name, and you store a piece of data inside. Later, you can use the name to retrieve or change what's stored.
In Python, you create a variable with the = operator. The name goes on the left, and the value goes on the right.
# Creating variables player_name = "Ada" score = 42 health = 98.5 print(player_name) # → Ada print(score) # → 42
Variable names in Python follow a few rules: they must start with a letter or underscore, can contain letters, numbers, and underscores, and are case-sensitive — so Score and score are two different variables.
The Three Core Data Types
Every value in Python has a type that tells Python what kind of data it is and what you can do with it.
Strings — str
Text data, wrapped in quotes. You can use single quotes 'hello' or double quotes "hello" — both work identically.
Integers — int
Whole numbers with no decimal point: 7, -3, 1000.
Floats — float
Numbers with a decimal point: 3.14, -0.5, 100.0.
Use type() to find out what kind of data a variable holds:
type("hello") → <class 'str'>
type(42) → <class 'int'>
type(3.14) → <class 'float'>
Challenge 1: Variables Warm-Up
Let's practice creating variables. Complete the code below so all three print statements produce the correct output.
• name — a string with your name (any name is fine)
• age — an integer
• gpa — a float
"Ada"), integers are whole numbers (25), and floats have a decimal point (3.7). Assign each on its own line above the print statements.
Type Conversion (Casting)
Sometimes you need to convert data from one type to another. Python makes this easy with built-in functions:
# String to integer user_input = "25" actual_number = int(user_input) print(actual_number + 5) # → 30 # Integer to string (for concatenation) score = 100 print("Score: " + str(score)) # → Score: 100 # Float to integer (truncates decimal) print(int(3.9)) # → 3
You can't concatenate a string and a number directly. "Score: " + 100 will cause a TypeError. You must convert the number first: "Score: " + str(100).
Challenge 2: Type Detective
Use the type() function to figure out the data types of several mystery values. Store each type name as a string.
42 is an int, but "42" has quotes so it's a str. 42.0 has a decimal point, so it's a float. What about "True"?
Challenge 3: Build a Player Card
Combine your variable and type conversion skills! Build a formatted player card string using concatenation and casting.
Player: Nova | Level: 7 | HP: 85.5
You'll need to use str() to convert the numbers!
+. Since level is an int and hp is a float, you'll need to wrap them in str() first. Something like: "Player: " + player + " | Level: " + str(level) + ...