Tutorials › Python Tutorial › Python Variables

Python Variables

10 min read · Lesson 4 of 24

A variable is a name that points to a value. Think of it as a label you stick on a box so you can find what is inside later. Variables let your program remember things: a customer's name, the price of a bag of maize, the number of tickets sold.

Creating variables

Python has no special command to declare a variable. You create one the moment you assign a value to it with =.

Example PYTHON
name = "Wanjiru"
age = 24
town = "Nakuru"
print(name)
print(age)
print(town)
Result
Wanjiru
24
Nakuru
Try it Yourself »

The = sign here does not mean "equals" as in maths. It means "store the value on the right in the name on the left".

Changing a variable

A variable can be given a new value at any time. It can even switch to a different type of value.

Example PYTHON
balance = 500
print(balance)
balance = balance + 250
print(balance)
balance = "closed"
print(balance)
Result
500
750
closed
Try it Yourself »

The line balance = balance + 250 reads the current value (500), adds 250 and stores the result back in balance.

Note: Python is dynamically typed: you never say "this variable holds a number". The type belongs to the value, not to the name. See Python Data Types for how to check a value's type.

Naming rules

Variable names must follow these rules:

  • They may contain letters, digits and underscores (_).
  • They must not start with a digit.
  • They cannot contain spaces or symbols such as -, @ or $.
  • They are case-sensitive: price and Price are different.
  • They cannot be a Python keyword such as if, for, class or True.
NameValid?Why
customer_nameYesletters and underscore
total2Yesdigit is not first
_countYesmay start with underscore
2totalNostarts with a digit
first-nameNohyphen is not allowed
classNoreserved keyword

Naming style

Python programmers use snake_case for variables: all lowercase, words joined with underscores, like monthly_rent or phone_number. Choose names that describe the value. rent beats r, and rent_kes is even clearer.

Assigning several variables at once

You can assign many variables in one line by separating names and values with commas.

Example PYTHON
county, code, region = "Kisumu", 42, "Nyanza"
print(county)
print(code)
print(region)
Result
Kisumu
42
Nyanza
Try it Yourself »

You can also give several variables the same value:

Example PYTHON
x = y = z = 0
print(x, y, z)
Result
0 0 0
Try it Yourself »

Swapping values

A neat Python trick is swapping two variables without a temporary helper.

Example PYTHON
morning = "tea"
evening = "uji"
morning, evening = evening, morning
print(morning, evening)
Result
uji tea
Try it Yourself »

Printing variables with text

The easiest way to mix text and variables in print() is to separate them with commas. Python adds a space between each item.

Example PYTHON
item = "sukuma wiki"
price = 30
print("One bunch of", item, "costs", price, "shillings.")
Result
One bunch of sukuma wiki costs 30 shillings.
Try it Yourself »

You can also join strings with +, but only if every part is already text. Trying "Age: " + 24 causes a TypeError. A cleaner way is the f-string, which you will meet in Python String Methods:

Example PYTHON
name = "Otieno"
age = 31
print(f"{name} is {age} years old.")
Result
Otieno is 31 years old.
Try it Yourself »

Constants

Python has no true constants, but by convention a name written in ALL CAPS means "please do not change this value".

VAT_RATE = 0.16
Tip: Using a named constant like VAT_RATE instead of typing 0.16 everywhere makes your code easier to update when the rate changes.

Exercises

Exercise

Create a variable called county with the value "Mombasa".

 = "Mombasa"
Exercise

Assign the values to both variables in one line.

first, second  10, 20

Continue to Python Data Types.

Sign in or create a free account to track progress, earn XP, badges and a certificate.