Post
Variables and types in Python
How variables work in Python: assignment, names, the basic types int, float, str, bool and None, type(), and the NameError.
A variable in Python is a name that points to a value. You don’t declare it or give it a type: you assign it, and the type comes with the value.
Every example here was run with Python 3.13 and checked against the official tutorial and the type() docs.
Assignment
The equal sign assigns a value to a name:
count = 0
count += 1
print(count)
1
count += 1 is shorthand for count = count + 1.
You can assign several names in one line, and swap two values without a temporary variable:
x, y = 1, 2
print(x, y)
x, y = y, x
print(x, y)
1 2
2 1
The basic types
type() shows the type of a value:
x = 5
print(x, type(x))
pi = 3.14
print(pi, type(pi))
name = "Python"
print(name, type(name))
flag = True
print(flag, type(flag))
5 <class 'int'>
3.14 <class 'float'>
Python <class 'str'>
True <class 'bool'>
int: whole numbers, with no fixed size.1_000_000is valid; underscores are only for readability.float: numbers with a fractional part.str: text, covered in the next post.bool:TrueorFalse.None: the value for “nothing”. Its type isNoneType.
Converting between these types has its own post, and checking a type with isinstance() is in the TypeError post.
Common mistakes
Using a name before assigning it. This gives a NameError:
print(n)
NameError: name 'n' is not defined
Assign the name first, and check you spelled it the same way everywhere.
What’s next
Next: strings and the common methods for changing and searching text.