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 =.
name = "Wanjiru"
age = 24
town = "Nakuru"
print(name)
print(age)
print(town)Wanjiru 24 Nakuru
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.
balance = 500
print(balance)
balance = balance + 250
print(balance)
balance = "closed"
print(balance)500 750 closed
The line balance = balance + 250 reads the current value (500), adds 250 and stores the result back in balance.
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:
priceandPriceare different. - They cannot be a Python keyword such as
if,for,classorTrue.
| Name | Valid? | Why |
|---|---|---|
customer_name | Yes | letters and underscore |
total2 | Yes | digit is not first |
_count | Yes | may start with underscore |
2total | No | starts with a digit |
first-name | No | hyphen is not allowed |
class | No | reserved 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.
county, code, region = "Kisumu", 42, "Nyanza"
print(county)
print(code)
print(region)Kisumu 42 Nyanza
You can also give several variables the same value:
Swapping values
A neat Python trick is swapping two variables without a temporary helper.
morning = "tea"
evening = "uji"
morning, evening = evening, morning
print(morning, evening)uji tea
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.
item = "sukuma wiki"
price = 30
print("One bunch of", item, "costs", price, "shillings.")One bunch of sukuma wiki costs 30 shillings.
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:
name = "Otieno"
age = 31
print(f"{name} is {age} years old.")Otieno is 31 years old.
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
VAT_RATE instead of typing 0.16 everywhere makes your code easier to update when the rate changes.Exercises
Create a variable called county with the value "Mombasa".
= "Mombasa"Assign the values to both variables in one line.
first, second 10, 20Continue to Python Data Types.
SKOPE