Variables and Types
Store values in variables and distinguish common Python data types.
Video coming to YouTube
Work through the full lesson and run its code while the video is being prepared for publication.
Understand the concept
A variable is a name that refers to a value. Assign a value with =, then use the name in later expressions. Python determines the type from the value you assign.
Beginners commonly use integers, floating-point numbers, strings, and booleans. Use type() to inspect a value, and int(), float(), or str() when an explicit conversion is needed.
- Assignment uses =
- int, float, str, and bool
- Convert types explicitly
See it step by step
Read the code, predict the output, then compare it with the result.
01. Store and display values
learner = "Sam"
completed = 4
print(learner, completed)Sam 4Assignment gives the values names. print() can display two values separated by a space.
02. Inspect two types
score = 8.5
ready = True
print(type(score).__name__)
print(type(ready).__name__)float
boolA decimal value is a float; True is a bool. type(...).__name__ shows each type's name.
Try it in Python
Edit the example and run it. Python starts in your browser the first time you click Run.
Python console
Ready to runNeed input()? Add one value per line
Your output appears here.
Need a hint?
Assign next_lesson = 4. Then print a line that uses both learner and next_lesson; print(learner, 'starts lesson', next_lesson) is one valid shape.
Complete the task and select Check task to verify your code.
Quick quiz
Three questions. You can change your answers and try again.
Typical mistakes
Everyone meets these errors. See what causes them and how to fix them.
Case-sensitive names
name = "Maya"
print(Name)name = "Maya"
print(name)What happens: NameError because Name and name are different names.
Use exactly the same spelling and capitalization each time.
Mixing text and numbers
age = 12
print("Age: " + age)age = 12
print("Age: " + str(age))What happens: TypeError when joining a string and an integer.
Convert the number to a string before concatenating it with text.