Track 3 · Lesson 5 of 5
Run your first Python
Build real Python programs by placing pieces into blanks, then run them in the page and see what they print.
You do not need to type a single character to start. Each box below is a whole program with a few gaps in it. Tap a piece from the list and it drops into a gap, then press Run and the program executes right there on the page.
Every box starts with the switch Put pieces in the right place for me turned on, so a piece you tap goes where it belongs. Turn it off once you want to place them yourself, and the mistakes become yours to find.
What you'll be able to do
- Put a six to ten line program together from pieces without typing.
- Follow a value through several lines to the print at the end.
- Read the error Python gives you when something is wrong.
Step 1: Two names and a print
print shows a value. + glues two strings together. This program has both, and two gaps.
Tap a piece to place it
One gap wants a value in quotes. The other wants the name you stored it under.
Turn the switch off when you want to place the pieces yourself, and the mistakes become yours to find.
Three lines, and the order matters. Line two creates name, so line three can use it. If you swap those two lines the program fails, because Python reads from the top down and will not use a name before it exists.
The quotes on "Termux" make it text. Without them Python would look for a name called Termux and fail.
Step 2: Carry a value through several lines
Real programs keep a value, do arithmetic on it, and print the result. This one works out the minutes in a day and then in a week.
Tap a piece to place it
One piece is the hours in a day. One builds the week from the day. One is the day figure itself.
Turn the switch off when you want to place the pieces yourself, and the mistakes become yours to find.
Nine lines, three gaps, and one piece that is wrong on purpose. per_day + days would give 1464, which is nonsense: you do not add days to minutes. Reading a variable name and thinking about what it holds is most of the skill here.
print takes several things separated by commas and puts a space between them, which is why the sentence comes out with the right spacing.
Step 3: A loop, and a running total
This is the shape most beginners find hardest: two values changing inside a loop, then three prints at the end.
Tap a piece to place it
Both indented lines run a hundred times. The third gap is worked out once, after the loop.
Turn the switch off when you want to place the pieces yourself, and the mistakes become yours to find.
Ten lines. Read the indentation, because it is doing the work: the two indented lines run a hundred times each, and the three prints run once at the end.
total += i means add i to total and store it back. count += 1 means add one. The average is worked out after the loop, from the two totals, which is why it can divide safely: count is 100 by then, never zero.
Put the two loop lines the wrong way round with the switch off and press Run. The average comes out wrong, and nothing tells you why. That is the kind of bug that takes an afternoon to find.
Step 4: A function with a loop inside it
A function is a named block you can call with different values. This one adds up whatever list you give it.
Tap a piece to place it
One gap adds to the running total inside the loop. The other hands the answer back after it.
Turn the switch off when you want to place the pieces yourself, and the mistakes become yours to find.
Eight lines, and the same function called twice with different lists. That is the point of a function: the loop is written once and used for any list.
return is what hands the value back to whoever called. Without it the function returns None, and print(total_of(...)) would show None. return 0 is there as a trap: it would hand back zero no matter what the loop did.
Step 5: Now type one yourself
Placing pieces teaches the shape. Typing teaches the vocabulary. This box is empty on purpose, and you can edit it.
Runs in your browser, and nothing is sent anywhere. Python starts loading in the background as you reach this cell, about six megabytes once. After that every cell on the site runs instantly until you close the tab.
Change the values and press Run. print with commas handles the spacing for you, which is why there are no stray spaces in the output.
Step 6: A mistake, and what Python says about it
This one is broken already. Press Run and read the last line of what comes back.
Python is case sensitive. Compare the name on line one with the name on line two.
name = "Termux" print("Hello, " + name)Runs in your browser, and nothing is sent anywhere. Python starts loading in the background as you reach this cell, about six megabytes once. After that every cell on the site runs instantly until you close the tab.
The last line reads NameError: name 'Name' is not defined. Read that line first, every time, because it names the problem exactly. Something called Name was used but never created. There is a name on the line above with a lowercase n, and Python treats those as two entirely different names.
Step 7: One more, all yours
Write a function that hands a word back reversed, then print the reversed form of Termux. The answer is xumreT.
A slice with a step of -1 walks a string from the end to the start: word[::-1]
def backwards(word): return word[::-1] print(backwards("Termux"))Runs in your browser, and nothing is sent anywhere. Python starts loading in the background as you reach this cell, about six megabytes once. After that every cell on the site runs instantly until you close the tab.
Quick reference
| Piece | What it does |
|---|---|
print(...) | show a value |
name = value | store a value under a name |
"text" | literal characters, not a name |
a + b | glue two strings, or add two numbers |
total += i | add to a running total |
| four spaces | what puts a line inside a loop or a function |
def and return | define a function, and hand a value back |
word[::-1] | the whole string, walked backwards |
| the last line of a traceback | what actually went wrong |
Next steps
- Working with text in Python takes the string handling further.
- Reading and searching text is the same job done with shell tools instead.
- Getting started with Termux is where the rest of the course begins.
Test yourself on this
4 questions and 1 task where you run a real command, with the reasoning explained either way.