Skip to content
Topic 2 of 6

Variables & Types

Store values in named variables and learn Python's basic types and how to convert between them.

15 min read 4 pages 5 practice games Jump to practice ↓

Page 1

Variables and assignment

A variable is a name that refers to a value. You create a variable with an assignment statement: the name goes on the left of the equals sign (=) and the value goes on the right.

python
age = 15
name = "Ada"
age = age + 1     # reassign: age is now 16
print(name, age)  # Ada 16

The equals sign means "store the value on the right under the name on the left"; it is not a test for equality. Python has no separate declaration step: a variable exists from the moment you first assign to it.

You can reassign a variable at any time, even to a value of a different type, because Python is dynamically typed. The name simply points at the new value.

Multiple assignment sets several variables in one line: x, y = 1, 2 sets x to 1 and y to 2. You can also give several names the same value with a = b = 0.

Page 2

Naming rules

A variable name can contain letters, digits and underscores (_). A name may start with a letter or an underscore. It cannot start with a digit, so 2nd_place is invalid but place_2 is fine. It cannot contain spaces or hyphens.

Names are case-sensitive: score, Score and SCORE are three different variables. A name cannot be a keyword such as if, for, while, def, class, True, False or None, because keywords already have a fixed meaning in Python.

By convention, Python variable names use snake_case: lowercase words joined by underscores, like total_price. A name written in all capitals, like MAX_SIZE, signals a constant that shouldn't change, though Python doesn't enforce it.

python
user_name = "sam"   # valid, snake_case
_count = 3          # valid: may start with an underscore
MAX_SIZE = 100      # a constant, by convention
# 2nd_place = 1     # SyntaxError: can't start with a digit
# my-name = "x"     # SyntaxError: hyphen not allowed

Page 3

Basic types

Every value in Python has a type. The basic built-in types are:

  • int: whole numbers like 42, -7 or 0. Python ints have no fixed size limit.
  • float: numbers with a decimal point like 3.14 or -0.5. Floats are stored approximately, so 0.1 + 0.2 gives 0.30000000000000004.
  • str: a string of text in quotes, like "hello".
  • bool: one of the two values True and False.
  • NoneType: the type of the single value None, which means "no value".

The built-in function type() tells you the type of a value:

python
print(type(42))     # <class 'int'>
print(type(3.0))    # <class 'float'>
print(type("42"))   # <class 'str'>
print(type(True))   # <class 'bool'>
print(type(None))   # <class 'NoneType'>

Note that "42" in quotes is a str, not an int. In Python, bool is a subtype of int: True behaves like 1 and False like 0.

Page 4

Casting between types

Casting means converting a value to another type by calling the type's name as a function:

  • int() converts to an integer. int("42") gives 42. int(3.9) gives 3, because int() truncates toward zero instead of rounding.
  • float() converts to a float. float("2.5") gives 2.5 and float(7) gives 7.0.
  • str() converts to a string. str(10) gives "10".
  • bool() converts to True or False. bool(0) and bool("") give False; most other values give True.
python
age_text = "16"
age = int(age_text)          # "16" -> 16
print(age + 1)               # 17
print(str(age) + " years")   # 16 years
print(int(-2.7))             # -2

A string that doesn't look like a number can't be converted: int("hello") raises a ValueError. int("3.5") also raises a ValueError; use int(float("3.5")) instead. The input() function always returns a str, so cast it before doing arithmetic.

Finished reading?

Mark it as read for +20 XP. Optional: practice is open either way.

Practice

Pass any one game to unlock Topic 3: Operators & Expressions.

Sign in to play, save your best and unlock Topics.
  • Reach −1,500 m
  • Reach 150 km
  • 7/10 without falling
  • Clear both boards
  • 150 points

Learn more