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
topic = "Python"
notebooks = 3
price = 12.5
print(topic)
print(notebooks * price)
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.
Quiz progress is saved in this browser. View certificates →
Learn together
Was this lesson helpful?
Comments and reactions are not open yet.
Join the conversation
Ask a question or share what you learned. Your display name and comment will be public after review. Please leave out private information. How feedback is stored