Python Foundations · Lesson 2 of 12

Variables and data types

Assignment, strings, integers, floats and booleans. Learn through short examples, practical tasks and targeted questions.

← All lessons

Variables and data types

Variables name values so a program can use or update them later. A string stores text, an integer stores whole numbers, a float stores decimal numbers and a Boolean stores True or False. Read each assignment carefully, then predict the type and value before printing them.

Watch out: Putting a number in quotation marks creates a string. That changes what operations the program can perform with the value.

1 · Understand

Assignment, strings, integers, floats and booleans. Study the code and predict what it does before running it. Change a value or string and observe how the output changes.

name = "Sam"
age = 15
height = 1.72
print(name, age, height)
Download example .py ↓

2 · Practise

Create variables for a product name, quantity and price. Display the details and the total price.

Plan your inputs, processing and outputs. Write the code in Mu, IDLE or PyCharm; test at least three cases.

Show a reference solution after attempting the task
product = "Notebook"
quantity = 3
price = 2.50
print(product, quantity, price)
print("Total:", quantity * price)
Download reference solution .py ↓

3 · Debug the code

Identify the problem and explain how to fix it before revealing the answer.

score == 10
print(score)
Reveal the corrected program and explanation
score = 10
print(score)

Compare the two snippets. Identify the changed line, run the corrected version and explain the effect.

4 · Check your understanding

Which statement assigns the value 12 to a variable?

5 · Challenge and reflect

Extension: Improve your solution to deal with unexpected input, add a second feature or display results more clearly.

Reflect: What was the hardest part? What would you test if someone else used your program?

Want extra practice? Try interactive Python tracing and debugging questions →

APPLY THE IDEA

Use this skill beyond the example

Trace assignments in order, especially when a variable is updated. If score starts at 4 and the program executes score = score + 3 twice, write the value after each line before checking the result.

Quick check: Why does a variable change value when assigned a new expression?

Check: Assignment evaluates the expression and stores the resulting value under the variable name, replacing its previous value.