Lesson 1
Your first program
After this lesson: You'll write your first program that prints text on the screen and send it to the judge.
Video
The video for this lesson is coming soon.
Explanation
Hi! I'm Bajtek, a robot who loves programming. I'll be with you during the whole course. Today you'll write your first program!
What is a program?
A program is a list of instructions for a computer. The computer carries them out in order, from top to bottom - one after another, like a recipe.
A computer is a very obedient but very precise helper. It does exactly what you write - it doesn't guess what you meant. If a recipe says "add flower" instead of "add flour", a person understands, but a computer doesn't.
We write the instructions in a language the computer understands. We'll use Python - it's simple, and it's used by NASA scientists, game makers and programmers at Google.
Example: instructions for a robot
A robot chef gets a list of instructions and does them in order, one after another. Press โถ and watch! ๐ค
The robot makes a sandwich ๐ฅช
- 1๐Take some bread
- 2๐งSpread butter
- 3๐งPut cheese on top
- 4๐ฝ๏ธServe the sandwich
Press โถ and watch the robot make a sandwich - step by step, in the order of the instructions.
Now we swap steps 2 and 4 ๐
The same instructions, just in a different order. What will the robot do?
- 1๐Take some bread
- 2๐ฝ๏ธServe the sandwich
- 3๐งPut cheese on top
- 4๐งSpread butter
Press โถ and watch the robot make a sandwich - step by step, in the order of the instructions.
A program is just the same - the computer runs the lines in order, from top to bottom. The order of the lines matters!
The print instruction
The first instruction you'll learn is print. It tells the computer: write something on the screen.
- After the word
printwe put brackets( )- whatever is inside appears on the screen. - Text inside the brackets goes in quotes, e.g.
print("Hi!"). On the screen you'll see justHi!- without the quotes. - Every
printprints one line, and the computer runs them in order, from top to bottom.
Watch the story - Bajtek will show you step by step how it works.
Bajtek meets print ๐ฌ
Click Next to go step by step.
Code
๐ฅ๏ธ Screen
The computer can't hear
Bajtek wants to greet Kuba on the computer screen. He says Hi! out loud - but the screen stays empty. The computer can't hear - it needs an instruction.
A robot's morning
A program is a list of instructions done in order. Press play and watch the robot do everything in that same order. ๐ค
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("I get out of bed")2print("I brush my teeth")3print("I eat breakfast")4print("I go to school")
โWatch the computer run a program
Click the right arrow. The yellow arrow shows the line the computer runs next, and the screen shows what it has printed so far.
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("Hi!")2print("I am Bajtek")3print("I like Python")
โThe order matters
The same three lines, but in a different order. Compare the screen with the previous program.
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("I like Python")2print("Hi!")3print("I am Bajtek")
โQuotes and an empty line
Step through the program. See that the quotes don't appear on the screen, and print() with nothing inside leaves an empty line.
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("Ala has a cat")2print("Top")3print()4print("Bottom")
โSpaces count too
The computer prints spaces in quotes exactly as you write them - that's how you can move text to the right.
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("A B")2print("A B")3print(" moved")
โStairs ๐ช
Every step has one more hash: 1, 2, 3, 4, 5. They all start at the left edge of the screen.
Click the arrow - every print draws one more line of the picture.
1print("#")2print("##")3print("###")4print("####")5print("#####")
โStairs the other way ๐ช
The same stairs, but pushed right with spaces. Count the spaces before the hashes: 4, 3, 2, 1, 0 - the fewer spaces, the closer to the left edge. Spaces + hashes are always 5 characters, which is why the right side is straight.
Click the arrow - every print draws one more line of the picture.
1print(" #")2print(" ##")3print(" ###")4print(" ####")5print("#####")
โDrawing with print
Every line of the picture is one print. That's how pictures made of letters and signs are created - here a robot's face. The screen shows exactly what's in the quotes.
Click the arrow - every print draws one more line of the picture.
1print(" _____")2print("| o o |")3print("| ^ |")4print("| '-' |")5print(" -----")
โWatch out for typos!
The computer is very precise. These programs won't work:
Print("Hi")- capital P. Python tells capital and small letters apart.print(Hi)- no quotes.print("Hi"- no bracket at the end.print("Hi)- no quote at the end.
What happens with a typo?
The first line works, then the program stops on the second and prints an error. The third line never runs.
print("Start")Print("Hi")print("End")Click the arrow - Bajtek walks through the program line by line, from top to bottom.
1print("Start")2Print("Hi")3print("End")
โWhat will this program print?
print("Ala")print("has a cat")
How many lines appear on the screen?
print("A")print()print("B")print()
And now something festive! Every print is one branch of a Christmas tree ๐
A Christmas tree ๐
The spaces at the start push the stars right - that's why the tree is straight. Watch it grow from the top to the trunk.
Click the arrow - every print draws one more line of the picture.
1print(" *")2print(" ***")3print(" *****")4print(" *******")5print("*********")6print(" | |")7print("Merry Christmas!")
โA snowman โ
Click the arrow - every print draws one more line of the picture.
1print(" ___")2print(" (o o)")3print(" ( - )")4print(" ( )")5print("( )")6print(" -------")
โAn empty line - a gap in the picture
print() with nothing in the brackets prints an empty line. It's handy when you want a gap - for example between the verses of a poem. ๐
A poem with a gap
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("Row, row, row your boat")2print("gently down the stream.")3print()4print("Merrily, merrily,")5print("life is but a dream.")
โLet's draw with print! Every print is one line of a picture. We stack them one under another like blocks ๐งฑ We'll start with small pictures.
A square
Just three prints: the top, the middle and the bottom of the square. The top and the bottom are the same!
Click the arrow - every print draws one more line of the picture.
1print("+---+")2print("| |")3print("+---+")
โFaces from the keyboard ๐
The simplest pictures: three characters in one line. Tilt your head to the left - they're faces!
Click the arrow - every print draws one more line of the picture.
1print(":-)")2print(";-)")3print(":-D")
โA little car ๐
Three prints: the roof, the body and the wheels. The spaces at the start put the roof right above the car.
Click the arrow - every print draws one more line of the picture.
1print(" ____")2print(" _/____|__")3print(" -O----O-")
โGreat! Now something bigger - a whole house with five lines. ๐
A house made of print
Five print instructions - five lines on the screen: the roof, the windows, the door. Watch the picture grow line by line.
Click the arrow - every print draws one more line of the picture.
1print(" ________")2print("| _ _ |")3print("| |_||_| |")4print("| __ |")5print("|__| |__|")
โOrder matters
Python reads a program from top to bottom, line by line. If you swap two print lines, they swap places on the screen too:
| Program | Screen |
|---|---|
print("One") | One |
print("Two") | Two |
print("Three") | Three |
It's like a sandwich recipe: first bread, then butter, then cheese. The other way round would be weird! ๐ฅช
Countdown to launch
The rocket launches only after the last line. ๐
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("3...")2print("2...")3print("1...")4print("Liftoff!")
โSums in quotes
In quotes a sum is ordinary text. Python calculates nothing - it copies it character by character, plus and minus included.
Click the arrow and watch: what does Python calculate, and what does it just copy?
1print("7 + 2")2print("9 + 10")3print("100 - 1")
โHmm, where are the results? ๐ค For now we only print text. In the next lesson you'll learn how to make Python really calculate 7 + 2 and print 9!
How many lines appear on the screen?
print("A")print()print("B")print("C")
Which program prints exactly: Hey!
Fun fact
The first program almost every programmer in the world writes prints Hello, World!. This tradition is over 50 years old! ๐
Remember
- A program is a list of instructions run from top to bottom.
print("text")prints text - the quotes aren't shown.- Every print is one line,
print()is an empty line. - Python tells capital and small letters apart:
print, notPrint.
How does the judge work?
Every task says what your program has to print. When you click Submit, the judge:
- runs your program,
- compares what it printed with the correct answer,
- shows the result: OK if everything matches, or Wrong answer if something is different.
Capital letters, commas and exclamation marks matter!
Bajtek and the judge ๐ฌ
Click Next to go step by step.
Bajtek has a task
The task says: print Hello, World! Bajtek wrote a program. But how can he know it's right?
The judge compares answers
Task: print Hello, World! See what the judge says about different programs.
Hello, World!
print("Hello, World!")
Hello, World!
print("hello, world!")
hello, world!
print("Hello World")
Hello World
Print("Hello, World!")NameError: name 'Print' is not defined
The same text, character by character
To the judge these are three different answers! Only the first matches "Ala has a cat".
Click the arrow - we'll compare every line with the answer "Ala has a cat" character by character.
1print("Ala has a cat")2print("ala has a cat")3print("Ala has a cat")
โHow to solve a task step by step
- Read the statement and look at the example - it shows exactly what to print.
- Open the Submit tab and write the program.
- Click Run - you'll see what your program prints and can compare it with the example.
- When everything matches, click Submit. The judge checks the program and shows the result.
Great! Now it's your turn. First solve the tasks with me in the video, then try the practice tasks on your own.
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.