Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Variables & Data Types

12 min 20 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 2 of 7 · View course roadmap
Step 1

Learn the idea

A variable is a named box that stores a value. You create one with = (the assignment operator):

Python has four essential basic types:

  • str — text: "hello"
  • int — whole numbers: 42
  • float — decimals: 3.14
  • bool — truth values: True / False

Use type() to check what type a value is. Variable names should be lowercase with underscores: user_age, not UserAge. Python is dynamically typed — a variable can hold any type, and you never declare the type up front.

Where you'll use this

Every configuration file, user record and shopping cart is variables of these four types. Type confusion (a "5" that should be a 5) is the single most common bug class in real codebases — banks have lost money to it.

Common mistakes

  • Using a variable before assigning it → NameError. Python reads top-to-bottom.
  • Confusing = (assign) with == (compare).
  • Expecting "5" + 5 to work — Python never silently converts between str and int the way JavaScript does. Convert explicitly with int() or str().

Pro tip

Name variables for what they contain, not their type: total_price beats tp beats x. Six months from now, the reader of your code is you.

Step 2

Watch how it runs — line by line


        

Press play to watch Python execute this code, one line at a time.

Step 3

Try it yourself

Blank · autosaved

The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.

PYexample.py
+ Enter to run
Output appears here…
Step 4

Pass the challenge +20 XP

Blank · autosaved

Create a variable language with the value "Python" and a variable year with the value 1991. Then print them both on one line with a single print, producing Python 1991.

Target output
Python 1991
PYchallenge.py
Run your code to check it…
Step 5

Check your understanding

1. What type is the value 3.14?
2. Which is a valid Python variable name?
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path