Gulberg Green, Islamabad

Programming Fundamentals · Lesson 02

Variables and data types

Give names to text, whole numbers, and decimal values.

A variable is a name that refers to a value. Assignment uses the equals sign. Python evaluates the expression on the right and binds its value to the name on the left.

Imagine labelling supplies for a programming workshop. A name like notebooks tells a reader more than a name like n. Use lowercase words separated by underscores when a name has several words.

Values have types. A string holds text, an integer holds a whole number, and a float represents a number with a fractional part. The way a program uses a value depends on its type.

Read the example

Python 3
topic = "Python"
notebooks = 3
price = 12.5
print(topic)
print(notebooks * price)
Expected output
Python
37.5

The first line of output uses the stored string. Multiplying the integer 3 by the float 12.5 produces the float 37.5. Putting the price in quotation marks would change it into text.

Try it yourself

Change the number of notebooks to four. Predict the total, then add a variable called total and print its value.

Show a worked solution
notebooks = 4
price = 12.5
total = notebooks * price
print(total)  # 50.0

Check your understanding

Your lesson quiz

Answer all five questions. Score at least 4 out of 5 (80%) to pass. You can retry as often as you like.

1. What is the type of the value "12.5"?
2. What does notebooks = 3 do?
3. What is the result of 4 * 12.5?
4. Which name clearly describes a count of notebooks?
5. Which value is an integer?

Quiz progress is saved in this browser. View certificates →

Learn together

Was this lesson helpful?

Comments and reactions are not open yet.

WhatsApp