Your first Python steps: from data to decisions
Learn variables, conditions and functions with a small robot decision-making example.
Variables & conditions
Install Python 3 and create a file named robot.py. This educational example prints a decision and does not control real hardware.
Data and conditions
A variable names a value. An if statement chooses a path through your program. Indentation is part of Python syntax; use four spaces inside each block.
distance_cm = 18
if distance_cm < 20:
print("STOP")
else:
print("MOVE")Turn the logic into a function
A function lets you repeat the same decision for different inputs. This function returns STOP below 20 and MOVE otherwise. The for loop tests three distances.
def decide(distance_cm):
if distance_cm < 20:
return "STOP"
return "MOVE"
for distance in [10, 20, 35]:
print(distance, decide(distance))No video or audio has been uploaded for this section yet. The written notes are available.
No separate download has been attached yet.
Learning ping-pong
⌑ Practice unlocks after you mark this section as studied.
✦ Your AI learning companion
Ask for a simpler explanation, another example or a hint.
Instructor’s hint
18 is below the threshold of 20.
The tutor is available to enrolled students.