Lessons 55 · 1 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
55
Final test
Textbook: pp. 108–109
GoalReviews the main topics of the course – logic, kinds of algorithms, Python basics, conditions, loops, functions, modules and a graphical interface – and solves final tasks independently.
New words
kinds of algorithms: linear, branching (if) and repeating (for, while) · algoritm turlaricondition and loop: if/elif/else choose a path; for and while repeat commands · shart va siklfunction: def name(parameters), the result is returned with return · funksiyamodule: ready code loaded with import, for example random, math, tkinter · modul
Explanation
For the final test recall the four parts of the course. Logic: and, or, not, implication and equivalence, truth tables. Algorithm and model: the stages of solving a task, linear, branching and repeating algorithms, the flowchart. Python: variables and types, input() returning a string, arithmetic operations (// and % are especially important), strings and slices, the sep and end arguments of print(). Control: if/elif/else, for with range(), while with a condition, break and continue, functions (def, return, parameters), local and global variables, import and modules (random, math), tkinter windows. When solving a task first find the input and output data, work out 1–2 examples by hand, then write the code and test it with the same examples. Read an error message from the end. When writing code pay attention to every indent and colon.
Worked examples
A function and a loop: def kvadratlar(n): ⏎ s = 0 ⏎ for i in range(1, n + 1, 2): ⏎ s += i ** 2 ⏎ return s ⏎ print(kvadratlar(7)) The sum of the squares of 1, 3, 5, 7: 1 + 9 + 25 + 49 = 84.
The largest digit: x = int(input()) ⏎ m = 0 ⏎ while x > 0: ⏎ if x % 10 > m: ⏎ m = x % 10 ⏎ x //= 10 ⏎ print(m) If 5826 is typed, the digits are checked one by one from the end and 8 is printed. A slice with a step: print("TOSHKENT"[::2]) takes the letters with indexes 0, 2, 4, 6: TSKN.
Class activity
“Self-assessment”: each pupil marks a list of 12 course topics with “I know”, “partly” or “I will revise”; pairs make each other one task from each “partly” topic.
Practice
1
What is the result of a program that finds the sum 3 + 6 + 9 + … + 60?
630
2
Write a single print() that prints every second letter (starting from index 0) of the string "SAMARQAND" using a slice. What is the result?
print("SAMARQAND"[::2]) – SMRAD
3
For the following function, what is f(6)? def f(n): ⏎ if n < 2: ⏎ return n ⏎ return f(n - 1) + f(n - 2)
8
4
When is for chosen and when while? Explain with examples.
If the number of repetitions is known beforehand, for (the numbers from 1 to 10); if it is unknown and the loop stops by a condition, while (the sum of numbers until 0 is typed).