☰ Contents · Computing

Loop practice and a review test

Lessons 42–43 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
43

Test: Python basics

Textbook: p. 86
GoalReviews the Python basics covered – variables, data types, arithmetic, strings, if…else and for – and solves mixed tasks.
New words
variable and type: a name with a value of type int, float, str or bool · o‘zgaruvchi va turoperation and expression: + - * / // % ** with brackets and the order of operations · amal va ifodacondition: a bool expression that if or elif checks · shartloop: for repeats the body a known number of times · sikl
Explanation

Before the test recall the main rules. A variable name starts with a letter or an underscore, is not a keyword, and capital and small letters differ. input() returns a string: if a number is needed, int() or float() is written. // and % are for whole division and the remainder; the last digit of a number is x % 10 and the tens digit is x // 10 % 10. In conditions do not forget the difference between == (comparison) and = (assignment); and/or/not make compound conditions; after if and else come a colon and an indent of 4 spaces. In the loop for i in range(a, b), b is not included; a sum starts from 0 and a product from 1. If you see an error, read the message from the end and find the line number. Always check the answer by hand on a small example.

Worked examples
Separating a digit: x = int(input()) ⏎ print(x // 100 % 10) If 4825 is typed, the hundreds digit 8 is printed. Setting the output: ism = input() ⏎ print("Omad,", ism, end="!") If Aziz is typed, Omad, Aziz! is printed (the exclamation mark at the end comes from end).
A mistake to find: x = 5 ⏎ if x = 5: ⏎     print(x) – SyntaxError: the condition needs ==, while = is assignment. The correct line is if x == 5: (with a colon at the end).
Class activity

“Mini test”: pairs write 4 short tasks in each other’s notebook (variable, type, if, for) and hide the answers; the partner solves them and then they check together.

Practice
1
A book has 360 pages. If 25 pages are read every day, in how many days is the book finished (even if the last day has fewer pages)?
2
Write a program that finds the hundreds digit of a positive whole number (8 for 4825).
3
What does for i in range(3): print(i * i, end=" ") print?
4
Why is the line if x = 5: wrong?