AlgoBazaAlgoBaza
Python for kids ยท Module 1

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. 1๐ŸžTake some bread
  2. 2๐ŸงˆSpread butter
  3. 3๐Ÿง€Put cheese on top
  4. 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. 1๐ŸžTake some bread
  2. 2๐Ÿฝ๏ธServe the sandwich
  3. 3๐Ÿง€Put cheese on top
  4. 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 print we 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 just Hi! - without the quotes.
  • Every print prints 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.

?Bajtek
I can't see anything!๐Ÿง’
๐Ÿ—ฃ๏ธHi!โžก๏ธ๐Ÿ–ฅ๏ธempty

Code

๐Ÿ–ฅ๏ธ Screen

โŒ the computer can't hear
1

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.

Step 1 / 9

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.

Code
1print("I get out of bed")
2print("I brush my teeth")
3print("I eat breakfast")
4print("I go to school")
Screen
โ–
1/5 ยท line 1 runs next

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.

Code
1print("Hi!")
2print("I am Bajtek")
3print("I like Python")
Screen
โ–
1/4 ยท line 1 runs next

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.

Code
1print("I like Python")
2print("Hi!")
3print("I am Bajtek")
Screen
โ–
1/4 ยท line 1 runs next

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.

Code
1print("Ala has a cat")
2print("Top")
3print()
4print("Bottom")
Screen
โ–
1/5 ยท line 1 runs next

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.

Code
1print("A B")
2print("A B")
3print(" moved")
Screen
โ–
1/4 ยท line 1 runs next

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.

Code
1print("#")
2print("##")
3print("###")
4print("####")
5print("#####")
Screen
โ–
1/6 ยท line 1 runs next

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.

Code
1print(" #")
2print(" ##")
3print(" ###")
4print(" ####")
5print("#####")
Screen
โ–
1/6 ยท line 1 runs next

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.

Code
1print(" _____")
2print("| o o |")
3print("| ^ |")
4print("| '-' |")
5print(" -----")
Screen
โ–
1/6 ยท line 1 runs next

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.

1print("Start")
ยท2Print("Hi")
ยท3print("End")

Click the arrow - Bajtek walks through the program line by line, from top to bottom.

Code
1print("Start")
2Print("Hi")
3print("End")
Screen
โ–
1/3 ยท line 1 runs next
?

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.

Code
1print(" *")
2print(" ***")
3print(" *****")
4print(" *******")
5print("*********")
6print(" | |")
7print("Merry Christmas!")
Screen
โ–
1/8 ยท line 1 runs next

A snowman โ›„

โ€ฆ

Click the arrow - every print draws one more line of the picture.

Code
1print(" ___")
2print(" (o o)")
3print(" ( - )")
4print(" ( )")
5print("( )")
6print(" -------")
Screen
โ–
1/7 ยท line 1 runs next

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.

Code
1print("Row, row, row your boat")
2print("gently down the stream.")
3print()
4print("Merrily, merrily,")
5print("life is but a dream.")
Screen
โ–
1/6 ยท line 1 runs next

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.

Code
1print("+---+")
2print("| |")
3print("+---+")
Screen
โ–
1/4 ยท line 1 runs next

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.

Code
1print(":-)")
2print(";-)")
3print(":-D")
Screen
โ–
1/4 ยท line 1 runs next

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.

Code
1print(" ____")
2print(" _/____|__")
3print(" -O----O-")
Screen
โ–
1/4 ยท line 1 runs next

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.

Code
1print(" ________")
2print("| _ _ |")
3print("| |_||_| |")
4print("| __ |")
5print("|__| |__|")
Screen
โ–
1/6 ยท line 1 runs next

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:

ProgramScreen
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.

Code
1print("3...")
2print("2...")
3print("1...")
4print("Liftoff!")
Screen
โ–
1/5 ยท line 1 runs next

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?

Code
1print("7 + 2")
2print("9 + 10")
3print("100 - 1")
Screen
โ–
1/4 ยท line 1 runs next
?

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, not Print.

How does the judge work?

Every task says what your program has to print. When you click Submit, the judge:

  1. runs your program,
  2. compares what it printed with the correct answer,
  3. 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.

?Is this really right?
Bajtekprint("Hello, World!")
โžก๏ธ
๐Ÿ” The judgeโš™๏ธ
the program's screen
1

Bajtek has a task

The task says: print Hello, World! Bajtek wrote a program. But how can he know it's right?

Step 1 / 8

The judge compares answers

Task: print Hello, World! See what the judge says about different programs.

Correct answer
Hello, World!
Program
print("Hello, World!")
Printed
Hello, World!
Program
print("hello, world!")
Printed
hello, world!
Program
print("Hello World")
Printed
Hello World
Program
Print("Hello, World!")
Printed
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".

โš–๏ธ the answerAlahasacat
?

Click the arrow - we'll compare every line with the answer "Ala has a cat" character by character.

Code
1print("Ala has a cat")
2print("ala has a cat")
3print("Ala has a cat")
Screen
โ–
1/4 ยท line 1 runs next

How to solve a task step by step

  1. Read the statement and look at the example - it shows exactly what to print.
  2. Open the Submit tab and write the program.
  3. Click Run - you'll see what your program prints and can compare it with the example.
  4. 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.