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
| Lessons | Topic |
|---|---|
| 1, 2, 6, 8, 9 | print, comments, sep and end, special characters |
| 3, 5, 10 | calculating: + - * / // % ** and the order of operations |
| 4 | whole numbers, numbers with a dot and text |
| 2, 7 | text: joining with + and repeating with * |
| 11, 12 | variables and changing their value |
| 13 | errors and their messages |
| 14, 15, 16, 17 | input(), type() and changing types with int(), float(), str() |
1. Printing: print
print("Hi")prints text, andprint(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, andend="..."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.
1# a comment - Python skips it2print("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")
โWhat does print(1, 2, 3, sep="") print?
2. Calculating
| Sign | What it does | Example |
|---|---|---|
+ - * | adds, subtracts, multiplies | 7 * 3 is 21 |
/ | divides (result with a dot) | 7 / 2 is 3.5 |
// | divides without the remainder | 7 // 2 is 3 |
% | the remainder of division | 7 % 2 is 1 |
** | power | 2 ** 3 is 8 |
The order: brackets first, then **, then * / // %, and + - last.
Recap: operations
The same two numbers, different operations.
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)
โ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.
3. Text
"Ala" + "cat"joins texts:"Alacat"."ha" * 3repeats text:"hahaha".- The text
"5"is not the number5!"5" + "5"is"55", and5 + 5is10.
Recap: text
1print("Ala" + " " + "cat")2print("ha" * 3)3print("5" + "5")4print(5 + 5)
โ4. Variables
points = 10puts a value into a box labelledpoints.points = points + 5- Python first works out the right side (10 + 5), then puts the result into the box.
Recap: variables
1points = 102print("Points:", points)3points = points + 54print("Points:", points)5points = points * 26print("Points:", points)
โWhat will this program print?
x = 3x = x * 2x = x + 4print(x, x % 4)
5. Errors
| Error | What it means |
|---|---|
SyntaxError | badly written code (missing bracket or quote) |
NameError | no such name (a typo) |
TypeError | text and a number don't fit together |
ValueError | int() got text that isn't a number |
ZeroDivisionError | division 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(), thenn = int(n_str). float()turns text into a number with a dot, andstr()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.
1name = input()2age_str = input()3age = int(age_str)4print("Hello,", name + "!")5print(name, age + 1)
โ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.
1number_str = input()2number = int(number_str)3last = number % 104print("Last digit:", last)
โ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.
Type a two-digit number on the screen and press Enter - you'll see it built from blocks.
1number_str = input()2number = int(number_str)3tens = number // 104units = number % 105print("Tens digit:", tens)6print("Units digit:", units)7print("Digit sum:", tens + units)
โHow to approach a task - a 5-step plan
- Read the statement and find: what's in the input, and what has to be printed.
- Look at the example - are the numbers on one line or several? What's the format of the answer?
- Read the data into variables with good names.
- Calculate the result - think which operation fits (
+,*,//,%...). - 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.
Type a number of seconds on the screen and press Enter - you'll see how many minutes that is.
1# 3. read the data2seconds_str = input()3seconds = int(seconds_str)4# 4. calculate5minutes = seconds // 606left = seconds % 607# 5. print8print(minutes, "min", left, "s")
โ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.
Type the number of presents and press Enter.
1presents_str = input()2presents = int(presents_str)3children_str = input()4children = int(children_str)5per_child = presents // children6left = presents % children7print("Each child gets:", per_child)8print("Left in the sack:", left)
โ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.
Type an amount on the screen and press Enter - you'll see which coins pay it.
1amount_str = input()2amount = int(amount_str)3fives = amount // 54rest = amount % 55twos = rest // 26ones = rest % 27print("5 zloty coins:", fives)8print("2 zloty coins:", twos)9print("1 zloty coins:", ones)
โ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.