Python Variables
What you will learn
- What a variable is and how assignment works
- The rules and good habits for naming variables
- How to update a variable, including
+= - How to use variables in calculations
1. What a variable is
A variable is a name that refers to a value. You create one with =, which is called assignment:
age = 25
price = 99.5
name = "Rina"
is_student = True
print(age, price, name, is_student)
Output:
25 99.5 Rina True
Read age = 25 as "let the name age refer to the value 25". The = here is not the maths equals sign. It means "store the value on the right under the name on the left".
2. Changing a variable
A variable can be given a new value. The right side is calculated first:
age = 25
age = age + 1
print(age)
age += 4
print(age)
Output:
26
30
age = age + 1 means: take the current value, add 1, store the result back in age. age += 4 is a shorter way to write age = age + 4. There are similar forms: -=, *=, /=.
3. Naming rules
A name:
- may contain letters, digits and underscores,
- cannot start with a digit (
2nameis an error), - is case sensitive (
ageandAgeare different), - cannot be a Python keyword such as
iforfor.
2name = 3
gives SyntaxError: invalid decimal literal.
Good names
- Use lowercase words joined by underscores:
total_price,student_name. - Choose names that say what the value is:
areais better thana. - Do not use names that look the same, such as
land1.
4. Using variables in calculations
Worked example: area of a rectangle
width = 8
height = 5
area = width * height
print("Area:", area)
print(f"Area of {width} x {height} = {area}")
Output:
Area: 40
Area of 8 x 5 = 40
Change width to 10 and run again: the answer updates without any other edit. That is the point of variables.
5. Several at once, and swapping
a, b = 3, 4
print(a, b)
a, b = b, a
print(a, b)
x = y = 0
print(x, y)
Output:
3 4
4 3
0 0
a, b = b, a swaps the two values in one line, which is a Python specialty.
Common mistakes
- Using a variable before giving it a value (
NameError). - Confusing
=and==. One assigns, the other compares (chapter 5). - Starting a name with a digit or using spaces in a name.
- Mixing capital letters:
Totalandtotalare different names. - Vague names such as
x1,x2,x3that you forget the meaning of.
Key takeaways
- A variable is a name that refers to a value;
=assigns. - The right side is calculated first, so
n = n + 1adds one ton. - Names use letters, digits and underscores, cannot start with a digit and are case sensitive.
- Choose clear names so your code explains itself.
Practice
1. After x = 5 and x = x * 2, what is x?
Show answer
10.
2. Why is 1st_place = 1 not allowed?
Show answer
A variable name cannot start with a digit.
3. Write code that stores a price of 250 and a quantity of 3, and prints the total.
Show answer
For example: price = 250, quantity = 3, print(price * quantity) which shows 750.
4. After a = 1, b = 2, a, b = b, a, what are a and b?
Show answer
a is 2 and b is 1.
5. Which of these are valid variable names: total_price, 2nd, my-var, _count, Total?
Show answer
total_price, _count and Total are valid. 2nd starts with a digit, and my-var contains a hyphen, which Python reads as a minus sign.
6. After n = 3, n = n + 1 and then n = n * n, what is n?
Show answer
16: n becomes 4, then 4 times 4.