What a variable is

A variable is a name attached to a value. In Python you create one simply by assigning with = — there is no separate declaration, and you never write the type yourself:

age = 27
price = 9.99
name = "Grace"
is_student = True
nothing = None

print(age, price, name, is_student, nothing)
27 9.99 Grace True None

Python figures out the type from the value on the right. You can check it at any time with type():

print(type(age))
print(type(price))
print(type(name))
print(type(is_student))
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>

A variable can later point at a different value — even one of a different type. This is called dynamic typing and it is one of the reasons Python code is short.

Naming rules and conventions

  • Names contain letters, digits and underscores, and cannot start with a digit: total_2 is fine, 2total is a syntax error.
  • Names are case-sensitive: score, Score and SCORE are three different variables.
  • You cannot use Python's 35 keywords (if, for, class, None…) as names.
  • Convention (PEP 8): use snake_case for variables and functions, UPPER_CASE for constants, CamelCase for classes.
  • Pick descriptive names. seconds_per_day = 86400 tells a reader more than s = 86400.

The core data types

TypeExampleWhat it holds
int42, -7, 10**100Whole numbers of any size — Python integers never overflow
float3.14, 2.0, 1e-9Decimal numbers (64-bit double precision)
str"hi", 'hi', """multi-line"""Text, Unicode by default
boolTrue, FalseTruth values (a subclass of int: True + True == 2)
NoneTypeNone"No value" — the default return of a function that returns nothing
list[1, 2, 3]Ordered, changeable sequence — see lists
tuple(1, 2)Ordered, unchangeable sequence
dict{"a": 1}Key → value mapping — see dictionaries
set{1, 2, 3}Unordered collection of unique values

Type conversion (casting)

Input from a user always arrives as a string. To do maths with it you convert explicitly:

raw = input("Enter your birth year: ")   # e.g. 1999
year = int(raw)
print("You turn", 2026 - year, "this year")

print(float("3.5") * 2)
print(str(42) + " apples")
print(int(7.9))        # truncates, does not round
print(round(7.9))      # rounds
Enter your birth year: 1999 You turn 27 this year 7.0 42 apples 7 8

Converting something that cannot be converted raises a ValueErrorint("hello") for example. The lesson on errors and exceptions shows how to handle that gracefully.

Multiple assignment and swapping

x, y = 10, 20
x, y = y, x          # swap without a temporary variable
print(x, y)

a = b = c = 0        # same value to several names
print(a, b, c)

first, *rest = [1, 2, 3, 4]
print(first, rest)
20 10 0 0 0 1 [2, 3, 4]
Practise variables in the app — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
Get it free

Mutable vs immutable

Numbers, strings, booleans and tuples are immutable: once created they never change, so "modifying" one really creates a new object. Lists, dictionaries and sets are mutable: they change in place. The difference matters when two names refer to the same object:

a = [1, 2, 3]
b = a            # b is the SAME list, not a copy
b.append(4)
print(a)

c = a.copy()     # a real copy
c.append(5)
print(a, c)
[1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4, 5]

This catches almost every beginner once. Remembering that = copies a reference, not the data, will save you a long debugging session later.

Frequently asked questions

Do I have to declare variable types in Python?

No. Python is dynamically typed: the type comes from the value you assign. You can add optional type hints such as age: int = 27 for readability and tooling, but they are not enforced at runtime.

What is the difference between int and float?

int holds whole numbers of unlimited size; float holds decimals in 64-bit precision. Dividing two ints with / always gives a float; use // for whole-number division.

Why does 0.1 + 0.2 not equal 0.3 in Python?

Floats are stored in binary and cannot represent most decimals exactly, so 0.1 + 0.2 gives 0.30000000000000004. Use round(), or the decimal module for money.

What does None mean?

None is Python's null value. It signals 'no value here' and is what a function returns when it has no return statement.