AlgoBazaAlgoBaza
Python for kids ยท Module 1

Lesson 18

Module 1 summary: First steps

After this lesson: You'll go over everything from Module 1 - print, calculating, text, variables, errors and reading input - and put it together in tasks.

Video

?

The video for this lesson is coming soon.

Explanation

Well done, this is the end of the first module! ๐ŸŽ‰ There's nothing new in this lesson. We'll go over everything you already know - topic by topic - and at the end there are tasks that put it all together.

What you already know

LessonsTopic
1, 2, 6, 8, 9print, comments, sep and end, special characters
3, 5, 10calculating: + - * / // % ** and the order of operations
4whole numbers, numbers with a dot and text
2, 7text: joining with + and repeating with *
11, 12variables and changing their value
13errors and their messages
14, 15, 16, 17input(), type() and changing types with int(), float(), str()

1. Printing: print

  • print("Hi") prints text, and print(2 + 3) the result of a calculation.
  • print(a, b) prints several things on one line, with a space between them.
  • sep="..." changes what goes between, and end="..." what goes at the end.
  • A line starting with # is a comment - a note for people. Python skips it.

Recap: print

Each line recalls one thing from the print lessons.

Code
1# a comment - Python skips it
2print("Hi!")
3print("Ala has a cat.")
4print("Bajtek likes coding.")
5print("I am", 10, "years old")
6print(1, 2, 3, sep="-")
7print("A", end="")
8print("B")
Screen
โ–
1/8 ยท line 2 runs next
?

What does print(1, 2, 3, sep="") print?

2. Calculating

SignWhat it doesExample
+ - *adds, subtracts, multiplies7 * 3 is 21
/divides (result with a dot)7 / 2 is 3.5
//divides without the remainder7 // 2 is 3
%the remainder of division7 % 2 is 1
**power2 ** 3 is 8

The order: brackets first, then **, then * / // %, and + - last.

Recap: operations

The same two numbers, different operations.

Code
1print("Adding: 7 + 2 =", 7 + 2)
2print("Subtracting: 7 - 2 =", 7 - 2)
3print("Multiplying: 7 * 2 =", 7 * 2)
4print("Dividing: 7 / 2 =", 7 / 2)
5print("Dividing without the remainder: 7 // 2 =", 7 // 2)
6print("The remainder: 7 % 2 =", 7 % 2)
7print("Power: 7 ** 2 =", 7 ** 2)
Screen
โ–
1/8 ยท line 1 runs next
?

What does print(2 + 3 * 2 ** 2) print?

?

What does print(365 // 7, 365 % 7) print?

The last digit

It'll come in handy in the tasks! The remainder of dividing by 10 is the last digit of the number.

472 % 10
1/2 ยท click the arrow - Python works out the next part

3. Text

  • "Ala" + "cat" joins texts: "Alacat".
  • "ha" * 3 repeats text: "hahaha".
  • The text "5" is not the number 5! "5" + "5" is "55", and 5 + 5 is 10.

Recap: text

Code
1print("Ala" + " " + "cat")
2print("ha" * 3)
3print("5" + "5")
4print(5 + 5)
Screen
โ–
1/5 ยท line 1 runs next

4. Variables

  • points = 10 puts a value into a box labelled points.
  • points = points + 5 - Python first works out the right side (10 + 5), then puts the result into the box.

Recap: variables

๐Ÿ“ฆ points
โญโ€ฆ
Code
1points = 10
2print("Points:", points)
3points = points + 5
4print("Points:", points)
5points = points * 2
6print("Points:", points)
Screen
โ–
1/7 ยท line 1 runs next
?

What will this program print?

x = 3
x = x * 2
x = x + 4
print(x, x % 4)

5. Errors

ErrorWhat it means
SyntaxErrorbadly written code (missing bracket or quote)
NameErrorno such name (a typo)
TypeErrortext and a number don't fit together
ValueErrorint() got text that isn't a number
ZeroDivisionErrordivision by zero

We read the message from the bottom: the last line says which error it is.

?

Which error does Python show for print("2" + 3)?

6. Reading input

  • name = input() reads one line as text.
  • We read a number in two steps: n_str = input(), then n = int(n_str).
  • float() turns text into a number with a dot, and str() turns a number into text.

Example: name and age

Type a name ๐Ÿง’ and press Enter โŽ, then an age ๐ŸŽ‚ and Enter โŽ again. The name stays text, and the age becomes a number.

Code
1name = input()
2age_str = input()
3age = int(age_str)
4print("Hello,", name + "!")
5print(name, age + 1)
Screen
โ–
1/2 ยท line 1 runs next
?

The input has two lines: 10 (sweets) and 3 (children). What does the program print?

sweets_str = input()
sweets = int(sweets_str)
children_str = input()
children = int(children_str)
print(sweets // children, sweets % children)

The last digit of a number

Type any number ๐Ÿ”ข (e.g. 472) and press Enter โŽ - the program prints its last digit.

Code
1number_str = input()
2number = int(number_str)
3last = number % 10
4print("Last digit:", last)
Screen
โ–
1/2 ยท line 1 runs next

Example: digits of a number

Type a two-digit number ๐Ÿ”ข (e.g. 47) and press Enter โŽ - the program prints the tens digit, the units digit and their sum.

๐Ÿ”ข number:?

Type a two-digit number on the screen and press Enter - you'll see it built from blocks.

Code
1number_str = input()
2number = int(number_str)
3tens = number // 10
4units = number % 10
5print("Tens digit:", tens)
6print("Units digit:", units)
7print("Digit sum:", tens + units)
Screen
โ–
1/2 ยท line 1 runs next

How to approach a task - a 5-step plan

  1. Read the statement and find: what's in the input, and what has to be printed.
  2. Look at the example - are the numbers on one line or several? What's the format of the answer?
  3. Read the data into variables with good names.
  4. Calculate the result - think which operation fits (+, *, //, %...).
  5. Print exactly what the task asks for - with no extra words. Run it on the example and compare.

Example: a clock

Type a number of seconds โฑ๏ธ (e.g. 135) and press Enter โŽ - the program turns them into minutes and seconds. See the whole plan in one program.

โฑ๏ธ All the seconds:?
minutes = ? // 60
?
left = ? % 60
?

Type a number of seconds on the screen and press Enter - you'll see how many minutes that is.

Code
1# 3. read the data
2seconds_str = input()
3seconds = int(seconds_str)
4# 4. calculate
5minutes = seconds // 60
6left = seconds % 60
7# 5. print
8print(minutes, "min", left, "s")
Screen
โ–
1/2 ยท line 2 runs next

Presents for the children ๐ŸŽ

Type the number of presents ๐ŸŽ (e.g. 47), then the number of children ๐Ÿง’ (e.g. 9). Press Enter โŽ after each number. The children can't be 0 - you can't divide by zero.

๐Ÿ‘œ in the sack: ?

Type the number of presents and press Enter.

Code
1presents_str = input()
2presents = int(presents_str)
3children_str = input()
4children = int(children_str)
5per_child = presents // children
6left = presents % children
7print("Each child gets:", per_child)
8print("Left in the sack:", left)
Screen
โ–
1/2 ยท line 1 runs next

How many coins? ๐Ÿช™

Type an amount in zloty ๐Ÿ’ฐ (e.g. 18) and press Enter โŽ - the program pays it with as few ๐Ÿช™ 5, 2 and 1 zloty coins as possible and says how many of each coin there are.

๐Ÿ’ฐ To pay:?
5 zlfives
?
2 zltwos
?
1 zlones
?

Type an amount on the screen and press Enter - you'll see which coins pay it.

Code
1amount_str = input()
2amount = int(amount_str)
3fives = amount // 5
4rest = amount % 5
5twos = rest // 2
6ones = rest % 2
7print("5 zloty coins:", fives)
8print("2 zloty coins:", twos)
9print("1 zloty coins:", ones)
Screen
โ–
1/2 ยท line 1 runs next

Remember

  • print prints, sep and end change the spacing, # is a comment.
  • Calculate with + - * / // % ** - remember the order of operations.
  • A variable is a box: x = x + 1 first calculates, then stores.
  • input() gives text - you get a number with int().
  • Read the statement and the example, read the data, calculate, print only what the task asks for.

Before you submit

Run the program on the example. Then think about other data: what if the number is 0? What if it's very large? The judge tests different cases, not just the example.

That's all of Module 1! Now the tasks - each one combines a few topics. Run your solution on the example and compare the output. Good luck!

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.