Module 01 · Lesson 02
Your First Python Program
Write your first real programs: shape print() output with sep, end and escape sequences, and follow Python's rules for lines, indentation and comments.
- About 8 minutes
- Beginner friendly
- 12-question quiz at the end
What you will learn
- Show many values
- Pick the gap and the finish
- Escape codes and text art
- Read a syntax error
Your print() toolkit
Last lesson, `print()` showed one thing at a time. Today it learns three new tricks.
Commas
Put commas between values and
print()shows them all on one line, with one space between each.sep picks the gap
sep=swaps that space for anything you like: a dash, a colon, or nothing at all.end picks the finish
end=swaps the new line at the end, so the nextprint()carries on along the same line.
Neat output is everywhere
Programs talk to people through text laid out just right. Each of these is a few print() lines.
- Game scoresThe
Lives: 3 | Score: 1200bar at the top of a game is a few values with a chosen separator. - Progress barsLoading dots that grow along one line come from
end=, one print at a time. - ReceiptsShop tills line up items and prices in neat columns with tabs and a row of dashes.
- TicketsCinema and train tickets print the same layout every time, with a new name and seat filled in.
One line, three settings
This one line uses everything above. Tap each part to see its job.
Part 1 of 9
command:print shows values on the screen, the same command you met in the last lesson.
Prints Lives: 3!
Shape your own output
Pick an example, change it, then press Run. Say what you expect to see before you press.
Example 1 of 6Many values
Commas let one print() show several things, text and numbers mixed.
Your turn: Add a line that prints your three favourite foods on one line.
print("Player:", "Mo")
print("Lives:", 3)
print("red", "green", "blue")Press Run to see what it prints.Order matters: robot round two
Python runs your lines top to bottom, exactly as written. These mazes need more turns, so plan the order before you press Go.
Level 1 of 3Down the steps
The robot starts top left, facing right.
1. Add moves
2. Your program (0/14)
Tap the moves above to write your program.
Watch a ticket print, line by line
Press Step to run one line at a time. Watch which lines print and which do not.
Ready. Press Step to run the first line.
- # A ticket for the school trip
- place = "Zoo"
- print("TICKET")
- print("Trip", place, sep=": ")
- print("Seat\t12")
- print("=" * 10)
Output so far
Nothing printed yet.
What Python does
Python reads the program from the top. Nothing has run yet.
Names
None yet.
Quick guesses
Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.
Question 1 of 6
1.What does this print?
print("x", "y", "z", sep="")0 of 6 answered
Line rules, busted
Each card is something beginners often believe. Tap it to see what Python really does.
Misconception 1 of 4
Notes
Everything above, in words. Open it when you want the full explanation.
Read the notesNotesOpenClose
Syntax
The general shape of the statements in this lesson.
print(a, b, c) # values separated by one space
print(a, b, sep=", ") # choose the separator
print("text", end="") # no new line after the output
"line\nnext" "a\tb" "\"quoted\"" # new line, tab, quote
"ab" + "c" "-" * 3 # join and repeat text
# comment: skipped by PythonPress Run to see what it prints.Worked examples
Read the problem, run the code, then read why it works.
Example 1 of 2
A neat shop receipt
Print a small receipt with a title line, two items with prices separated by a tab, and a dashed line made with string repetition.
print("RECEIPT")
print("-" * 12)
print("Tea\t2.50")
print("Cake\t3.00")
print("-" * 12)
print("Total", "5.50", sep=": ")Press Run to see what it prints.Predict first
How it works(open after your guess)
Common pitfalls
Mistakes people make with this topic, and what to write instead.
Pitfall 1 of 3
A missing closing quote
Spot the bug
Can you see what is wrong? Run it to watch it go wrong, then open the fix.
print("Hello)Press Run to see what it prints.Show what goes wrong and the fixWhat goes wrong and the fix
In the real world
Where this shows up in software people use every day.
- Small business
A label for a parcel
Someone selling crafts online prints a short label for each parcel, with the name, the town and the order number laid out the same way every time.
Why Python fits
Using
sepand string repetition keeps the layout identical for every label. Changing the values is all it takes to print the next one.Pythonprint("To:", "Sam Lee") print("Town:", "Leeds") print("Order", "1042", sep=" #") print("=" * 16)OutputPress Run to see what it prints.
Key takeaways
Tick each one you could explain to a friend.
0 of 5
Common questions
Guess the answer first, then open it.
Should I use single or double quotes?
Why does print("a" + "b") show ab with no space?
How many spaces should I indent?
Can I put two statements on one line?
Where to go next
The quiz and the Lab live on their own pages, so this lesson stays about learning.
Finished this lesson?
Score 80% or more on the lesson quiz to mark it complete (+5 XP). Every lesson in a module unlocks that module's Lab.
Check what you learned
12 questions, with an explanation after every answer. A verified attempt counts towards opening the module Lab.
Keep going in the Lab
7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.
Spaced review
Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.
This lesson at a glance
The 6 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.
What else you can search for and explore
Curious to go further? Each card opens a web search in a new tab.
References
Look things up here whenever you need the exact details.