Lesson 4 of 5 · Variables · 3 min read · Updated
Changing the value of a variable
A variable can get a new value at any time. Learn to change it and see what happens to the old value.
Understand
A variable can change. You can give it a new value whenever you want.
When you do, the variable forgets the old value and keeps the new one.
Note
This is why it is called a variable: its value can vary.
Use it
Here score starts as 10. Then it is given the value 15.
score = 10
print(score)
score = 15
print(score)
10
15
The first print shows 10. After the new line, score holds 15, so the second print shows 15.
Practice
Fill in the blank so that score becomes 20 and the code prints 20.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
20
The variable score should hold 20.
Need a hint?
- Write the new value after the second equals sign.
- Write
score = 20.
Show a solution
score = 10
score = 20
print(score)
Fill in the blank so that name becomes Ravi and the code prints Ravi.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
Ravi
The variable name should hold "Ravi".
Need a hint?
- Text needs quotes around it.
- Write
name = "Ravi".
Show a solution
name = "Asha"
name = "Ravi"
print(name)
Print score as 10. Then give score the value 15 and print it again.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
10
15
The variable score should hold 15.
Need a hint?
- Add the new value on the comment line.
- Write
score = 15.
Show a solution
score = 10
print(score)
score = 15
print(score)
After x = 3 and then x = 8, what does x hold?
Why it works
A new value can even be of a different type. The variable simply holds the new value.
thing = 5
print(thing)
thing = "five"
print(thing)
5
five
Going deeper
This part is optional. Python lets you give values to two variables in one line. Python matches them in order.
a, b = 1, 2
print(a)
print(b)
1
2
The same idea swaps two values. Here a and b exchange their values.
a = 1
b = 2
a, b = b, a
print(a)
print(b)
2
1
Before the swap, a was 1 and b was 2. After it, a is 2 and b is 1.
To swap two values, you cannot just copy one into the other. Look at what happens when you try:
a = 1
b = 2
a = b
b = a
print(a)
print(b)
2
2
The line a = b replaced the old value of a, so both variables ended up holding 2. In a, b = b, a, Python first reads both values on the right and only then assigns them, so nothing is lost.
In one line, give x the value 4 and y the value 9. Then print x and print y.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
4
9
The variable x should hold 4.
The variable y should hold 9.
Need a hint?
- Write the names on the left and the values on the right, in the same order.
- Write
x, y = 4, 9.
Show a solution
x, y = 4, 9
print(x)
print(y)
a is 1 and b is 2. Swap them in one line so that the code prints 2 and then 1.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
2
1
Need a hint?
- Put both names on each side of the equals sign.
- Write
a, b = b, a.
Show a solution
a = 1
b = 2
a, b = b, a
print(a)
print(b)
Create a free account to save your progress, or sign in.