Variables
Section 2: Variables and inputLesson 2.1 · ⏱ 7 min
A variable is a box with a name that keeps a value. We write the name, then =, then the value.
name = "Robo"
age = 10
print(name)
print(age + 1)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Robo 11

📌 Note
= in programming means “store in”, not “equals” as in maths.📌 Another example: the variable changes
We put a value in the variable, then change it, and it remembers the last one.
score = 0
print("score:", score)
score = score + 10
score = score + 5
print("score:", score)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗score: 0 score: 15
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Two variables: name and city, then print them on one line.
👀 Show the solution🙈 Hide the solution
name = "Ali"
city = "Alexandria"
print(name, "lives in", city)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Ali lives in Alexandria
Result: Ali lives in Alexandria
Medium
Swap the values of two variables: a = 1 and b = 2.
👀 Show the solution🙈 Hide the solution
a = 1
b = 2
a, b = b, a
print(a, b)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗2 1
Result: 2 1
Hard
A counter: start at 100, take away 7 three times, and print the result.
👀 Show the solution🙈 Hide the solution
n = 100
n = n - 7
n = n - 7
n = n - 7
print(n)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗79
Result: 79

