Variables: Names for Objects

In Python a variable is a name stuck onto an object. Watch names get created, move to new objects, and share one object.

Python · beginner · 6 min · 7 steps · free

What you'll learn

names.py

age = 30
name = "Asha"
age = age + 1          # makes a new number object; the name moves to it
nickname = name        # two names, one string object
score = 9.5
is_member = True
print(name, age, nickname, score, is_member)

Step by step

  1. age is a name for the integer object 30. Python works out the type from the value; there is no type to declare.
  2. A second name, name, for a string object. Text goes in quotes.
  3. The right side runs first: 30 + 1 makes a brand new object, 31. Then the name age moves to it. The 30 itself never changed.
  4. nickname = name does not copy the text. Both names now point at the very same string object.
  5. A float: a number with a decimal point.
  6. A boolean: True or False, with a capital letter.
  7. Five names, five kinds of value: int, str, float, bool, and one shared string. Every variable in Python works this way: a name pointing at an object.

Common mistakes

Best practices

Key takeaways

Check yourself

a = 5 then b = a then a = 6. What is b?

Answer: 5. b still points at 5; only a moved to 6.

Which value is a float?

Answer: 9.5. A decimal point makes a float; quotes make a string.

More in Python Basics