LESSON 02 · BEGINNER PATH9 min

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

PYTHON
learner = "Sam"
completed = 4
print(learner, completed)
EXPECTED OUTPUT
Sam 4

Assignment gives the values names. print() can display two values separated by a space.

02. Inspect two types

PYTHON
score = 8.5
ready = True
print(type(score).__name__)
print(type(ready).__name__)
EXPECTED OUTPUT
float
bool

A 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 run
Need input()? Add one value per line
Ctrl / ⌘ + Enter to run
OUTPUT
Your output appears here.
Your turnCreate a variable named next_lesson with the number 4. Print a sentence that includes the learner's name and that number.
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.

    01.What is the type of 4.0?

    02.What does str(5) produce?

    03.After x = 2 and x = "two", what is the type of x?

    Typical mistakes

    Everyone meets these errors. See what causes them and how to fix them.

    Case-sensitive names

    COMMON MISTAKE
    name = "Maya"
    print(Name)
    FIX
    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

    COMMON MISTAKE
    age = 12
    print("Age: " + age)
    FIX
    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.