Modules/Python/ Names and Kinds of Data

Variables and Types

A variable is a label on a value. Python picks the type of the value for you — and the type decides what you can do with it.

Assign, then ask what it is

The = sign binds a name to a value. type() asks Python what kind of value the name currently points at.


                        

Looks can deceive

"10" and 10 look almost identical. They are different types, and the type changes the behaviour.


                        

The types you will meet first

int whole numbers, like 36. Arithmetic keeps them exact.
float numbers with a decimal point, like 3.14.
str text, written in quotes. The + on a string glues rather than adds — see the editor on the left.
bool True or False, the answer to yes/no questions.
Python infers the type from the value you assign. x = "10" and x = 10 are both fine — they just make x different kinds of thing. This is why type() is your friend: when code behaves oddly, the first question is always "what did Python think this was?"

Variables and Types: A Practical Guide

A name is not a box. It is a label that points at a value.

Quick Context

When you write age = 36, Python creates the value 36 and points the name age at it. The value lives on its own; the name is just a label. Assign again and the label moves — the old value is left behind, and once nothing references it, it is cleaned up.

Why types matter

The type of a value decides what the operators mean. + on two ints adds them; + on two strs concatenates them. Neither is "wrong" — the behaviour follows the type. When a program does something surprising, nine times out of ten a value is a different type than you assumed, and type() settles it in one line.

Interactive Exploration Guide

  1. Read the outputs. In the first editor, check the order: the two values print, then their two types. The last line runs only after the first three have finished.
  2. Reuse a name. Add age = "six" as a new last line and run. The label age now points at a string — same name, new value, new type.
  3. Compare the arithmetic. The second editor prints "10"+"10" = "1010" but 10+10 = 20. That is the whole lesson in two lines.
  4. Break the glue. Try print(a + b) in the second editor and run. Python's error message is a type complaint: you cannot add a string and an int, and it tells you exactly which types collided.

Key Takeaway

A variable is a label, not a container. The type of the value it labels is chosen by Python, and it dictates behaviour — especially what + means. Whenever behaviour looks wrong, ask type() first; it is the single most useful question you can ask a running program.

Check yourself

0 of 3

Answer without scrolling back up.

  1. x = "10" makes x:

  2. Which of these is NOT a built-in Python type?

  3. You reuse a variable name for a new value. The old value:

Cheat sheet

Variables and Types

When you write age = 36, Python creates the value 36 and points the name age at it. The value lives on its own; the name is just a label. Assign again and the label moves — the old value is left behind, and once nothing references it, it is cleaned up.

PYTHON · vizlearn.in/python/variables_and_types.html