Interactive lesson · about 25 minutes

Python Variables and Data Types

Most early Python mistakes come from a wrong picture of what a variable is. This lesson gives you the right picture first, then tests it with questions you answer before you see the result.

By the end you will be able to:

  • Explain what happens when you assign a value to a name.
  • Name the five data types in this lesson and tell them apart.
  • Predict what happens when two names point to the same list.
  • Convert between types, and predict when a conversion fails.

1. A variable is a name, not a box

A common picture is a labeled box that holds a value. Python works differently. A variable is a name that points at a value. The equals sign attaches the name to the value on its right. It does not mean “is equal to.”

The name a points to the value 5. After a is set to 9, the name points to 9 instead.Before: a = 5a5After: a = 9a9
Reassigning a moves its tag to a new value. The old value is not changed.

Predict: what does this print?

x = 5
x = x + 1
print(x)
Choose what the program prints

2. Five data types you will use first

Every value has a type, and type(value) shows it. The table below lists the types in this lesson.

Five basic Python data types with examples
TypeHoldsExample
intWhole numbers42
floatNumbers with a decimal point3.14
strText, in quotes"hello"
boolTrue or FalseTrue
listAn ordered collection you can change[1, 2, 3]

Try it: sort each value into its type.

Step 1: choose a value. Step 2: choose the type it belongs to. Each answer is checked straight away.

Values

Types

    The hard one is usually "42". It looks like a number, but the quotes make it text, so you cannot add 1 to it directly. Note that True is a bool, even though Python can also treat it as 1 in arithmetic.

    3. Two names, one value

    This is the idea that trips up most beginners. Assigning one name to another does not copy the value. It makes a second name point to the same thing. For numbers and text, nothing changes between the two names, because you cannot change a number in place. For a list, you can change it in place, and then every name that points to it sees the change.

    Numbers: reassigning a name does not change the other name
    1a = 5
    2b = a
    3a = 9
    4print(b)

    Variables after this line

    No variables yet.

    Output: (nothing printed yet)

    Step 1 of 5

    Nothing has run yet.

    Lists: one list, two names
    1xs = [1, 2]
    2ys = xs
    3ys.append(3)
    4print(xs)

    Variables after this line

    No variables yet.

    Output: (nothing printed yet)

    Step 1 of 5

    Nothing has run yet.

    Two names, xs and ys, both point to one list containing 1, 2 and 3.xsys[1, 2, 3]
    One list, two names. Changing the list through either name changes it for both.

    Predict: what does this print?

    a = "hi"
    b = a
    b = b + "!"
    print(a)
    Choose what the program prints

    4. Converting between types

    Python does not guess what you mean. You convert a value by calling a type name as a function: int(), float() or str(). Some conversions work, some drop information, and some fail.

    Results of common type conversions
    CodeResultWhat happened
    int("42")42Text of digits becomes a whole number.
    int("4.2")ValueErrorint() does not accept a decimal point in text.
    int(4.9)4The decimal part is cut off, not rounded.
    float(3)3.0A whole number gains a decimal point.
    str(3)"3"The number becomes text, which you can join to other text.

    Predict: what does this print?

    print(int("4.2"))
    Choose what the program prints

    Predict: what is the type of the result?

    print(type(7 / 7))
    Choose what the program prints

    5. Check yourself

    Answer these before you look. If you get one wrong, go back to the section it comes from.

    Question 1

    a = [1]
    b = a
    a = [2]
    print(b)
    Choose what the program prints

    Question 2

    a = [1]
    b = a
    b.append(2)
    print(a)
    Choose what the program prints

    Question 3

    price = "9"
    print(int(price) + 1)
    Choose what the program prints

    Keep going

    Variables and data types are the first stage of the Python roadmap. The roadmap shows what to learn next, in what order, and when you are ready to apply.