Course curriculum
Python · LESSON 2 OF 5

Variables and data types

Store values, distinguish numbers from strings and use clear names for changing information.

About 9 minutes · Beginner

Name a value

Assignment binds a name to a value. Use names that describe the meaning of the value, such as completed_units, instead of vague names such as x.

Python distinguishes integers, floating-point numbers, strings and booleans. The same name can later refer to another value, but predictable types make your code easier to understand.

Python
completed_units = 3
total_units = 8
remaining_units = total_units - completed_units
print(remaining_units)
Expected output
5

Text and numbers are different

The string "12" contains characters; the integer 12 is a number. Adding two strings joins them. Convert a numeric string with int when you need arithmetic.

Conversion can fail: int("twelve") raises ValueError. Validate external input before assuming it contains a number.

Python
pages = int("12")
print(pages + 3)
print("12" + "3")
Expected output
15
123

Format readable output

An f-string inserts expressions between braces. It lets you combine labels and values without manually converting every number.

Use == to compare values. A single = assigns a value and is not a comparison.

Python
subject = "Physics"
score = 42
print(f"{subject}: {score} marks")
Expected output
Physics: 42 marks

Try it yourself

Store a price of 50 and a quantity of 4. Calculate the total and print “Total: 200”.

Show a possible solution
Python
price = 50
quantity = 4
total = price * quantity
print(f"Total: {total}")

Keep exploring: official Python documentation.

0 of 5 completed on this device