Variables & Data Types

A Python variable doesn't come with a declared type attached to it — you assign a value, and Python figures out what kind of thing it is from the value itself.

Creating a variable

No let, var, or type keyword required. The = sign just points a name at a value:

python example.py
name = "Ava"
age = 29
height = 1.68
is_member = True

Dynamic typing

Python is dynamically typed: a variable isn't locked to one type forever. Reassign it and it happily becomes something else entirely:

python example.py
count = 5
count = "five"
print(count)
Output
five

No error, no warning — count simply refers to a string now instead of a number. This is convenient, but it also means a typo or a mixed-up variable won't necessarily be caught until the program actually runs into trouble using it.

Checking a type with type()

python example.py
print(type(29))
print(type("Ava"))
print(type(1.68))
print(type(True))
Output
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>

The core built-in types

  • int — whole numbers: 3, -12, 1000000
  • float — numbers with a decimal point: 3.14, -0.5
  • str — text, written in quotes: "hello"
  • bool — exactly one of True or False

None: the absence of a value

Python also has a special value, None, which represents "nothing here" rather than a zero or an empty string:

python example.py
score = None
print(score)
Output
None
Note: variable names can use letters, digits, and underscores (but can't start with a digit), are case-sensitive, and by convention use snake_casetotal_price, not totalPrice or TotalPrice.