انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

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:
Robo
11
🐍 Open the code in Robo Python ↗
The example in Robo Python: the code on the right and what appeared on the left
The example in Robo Python: the code on the right and what appeared on the left
📌 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.

🐍 The variable changes
score = 0
print("score:", score)
score = score + 10
score = score + 5
print("score:", score)
🖥️ The output in Robo Python:
score: 0
score: 15
🐍 Open the code in Robo Python ↗
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Two variables: name and city, then print them on one line.

👀 Show the solution
🐍 Solution
name = "Ali"
city = "Alexandria"
print(name, "lives in", city)
🖥️ The output in Robo Python:
Ali lives in Alexandria
🐍 Open the code in Robo Python ↗

Result: Ali lives in Alexandria

Medium

Swap the values of two variables: a = 1 and b = 2.

👀 Show the solution
🐍 Solution
a = 1
b = 2
a, b = b, a
print(a, b)
🖥️ The output in Robo Python:
2 1
🐍 Open the code in Robo Python ↗

Result: 2 1

Hard

A counter: start at 100, take away 7 three times, and print the result.

👀 Show the solution
🐍 Solution
n = 100
n = n - 7
n = n - 7
n = n - 7
print(n)
🖥️ The output in Robo Python:
79
🐍 Open the code in Robo Python ↗

Result: 79

Finished the lesson? Mark it so your progress counts.