Give things a name.
Store values, change them, and give your future self readable code.
01 The idea
A variable name refers to a value. Assignment with = binds the name to that value; another assignment can bind it to a different value. Choose names that describe what a value means. Python does not require a type declaration for each name.
02 The experiment
Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.
project_name = "Orbit"
open_tasks = 4
open_tasks = open_tasks - 1
print(project_name)
print(open_tasks)Orbit 3
03 Under the hood
- 1
project_name refers to text; open_tasks refers to an integer. Python keeps track of the types of the values.
- 2
The right side of an assignment is evaluated first. open_tasks - 1 produces 3, then the name is updated.
- 3
Use snake_case for ordinary names. A name like open_tasks communicates more than x. Names are case-sensitive, so score and Score are different.
04 Your turn, builder
Create a battery_level variable with value 100. Reduce it by 15 and print the remaining level.
Reveal one possible solution +
battery_level = 100
battery_level -= 15
print(battery_level)05 Check your signal
After count = 3 followed by count = count + 2, what is count?
Experiment. Logged.
When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.
Keep exploring
Go deeper with the official Python guide ↗.