ლექცია 08 · VIII კვირაLecture 08 · Week VIII
შუალედური —
სრული გამეორებაMidterm —
full revision
I–VII კვირის მასალა · 10 ტესტური + 10 ღია სავარჯიშო კითხვა · ტიპური შეცდომები Weeks I–VII · 10 test + 10 open practice questions · typical mistakes
ეს დეკი გამოცდის წინა კონსულტაციისთვისაა. დაურიგე სტუდენტებს — თავად გაივლიან. კითხვები გამოცდის ფორმატს იმეორებს, მაგრამ არა ზუსტ შინაარსს.
This deck is for the pre-exam consultation. Hand it out — they can work through it themselves. The questions mirror the exam format, not its exact content.
რუკაThe map
რა გავიარეთWhat we have covered
I · შესავალიI · Introduction
- ისტორია, PVM, ბაიტკოდი
- წანაცვლება, ორწერტილი
- კომენტარები, 35 keyword
- History, the PVM, bytecode
- Indentation, the colon
- Comments, 35 keywords
II · ტიპებიII · Types
- int, float, str, bool, None
type(), კონვერტაციაinput()→ str, f-string
- int, float, str, bool, None
type(), castinginput()→ str, f-strings
III · პირობებიIII · Conditions
== != > < >= <=and or notif / elif / else
== != > < >= <=and or notif / elif / else
IV · ციკლებიIV · Loops
for+range()while, 3 ნაწილიbreak continue else
for+range()while, its 3 partsbreak continue else
V · ჩაშენებულიV · Built-ins
abs round divmod summath: sqrt, ceil, floor, pirandom: randint, choice, seed
abs round divmod summath: sqrt, ceil, floor, pirandom: randint, choice, seed
VI · ფუნქციებიVI · Functions
def,return≠print- ნაგულისხმევი,
*args - scope,
lambda
def,return≠print- Defaults,
*args - Scope,
lambda
VII · სტრიქონებიVII · Strings
- ინდექსი, slice,
[::-1] strip split join replacefind index count in
- Indexing, slicing,
[::-1] strip split join replacefind index count in
ფორმატიFormat
- 10 ტესტი × 1 ქულა
- 10 ღია × 2 ქულა
- სულ 30 ქულა
- 10 test × 1 point
- 10 open × 2 points
- 30 points in total
გამეორება 1/4Revision 1/4
ტიპები და ოპერაციები — ერთ ეკრანზეTypes and operators — on one screen
7 / 2 # 3.5 always a float
7 // 2 # 3 the whole part
7 % 2 # 1 the remainder
7 ** 2 # 49 exponentiation
2 + 3 * 4 # 14 * binds tighter than +
2 ** 3 ** 2 # 512 right to left
-2 ** 2 # -4 power first, then the minus
int("42") # 42
int(9.99) # 9 truncates
round(9.99) # 10 rounds
round(0.5) # 0 to the nearest even!
str(100) + "ლ" # "100ლ"
bool(0), bool(""), bool([]), bool(None) # all False
bool("False") # True!
0.1 + 0.2 == 0.3 # False
გამეორება 2/4Revision 2/4
პირობები და ციკლებიConditions and loops
if score >= 91:
grade = "A"
elif score >= 81:
grade = "B"
else:
grade = "F"
# chaining
if 0 <= x <= 100: ...
# ternary
s = "დიდი" if n > 10 else "პატარა"
range(5) # 0 1 2 3 4
range(2, 6) # 2 3 4 5
range(0, 10, 2) # 0 2 4 6 8
range(10, 0, -1) # 10 … 1
for i in range(1, 11):
if i == 5:
continue # skips this step
if i == 8:
break # ends the loop
else:
print("finished without break")
ორი ხაფანგი
range(1, 5) — 5 არ შედის · while-ში მთვლელის გაზრდა დაგავიწყდა → უსასრულო ციკლი.
Two trapsrange(1, 5) — 5 is not included · forgetting to increment the counter in a while → an infinite loop.
გამეორება 3/4Revision 3/4
ფუნქციებიFunctions
def area(w, h=1): # h has a default, so it comes last
"""მართკუთხედის ფართობი. / Area of a rectangle."""
return w * h # return ≠ print
print(area(4, 5)) # 20
print(area(4)) # 4
print(area(h=5, w=4)) # 20 keyword arguments
def stats(*nums): # a variable number of arguments
return min(nums), max(nums)
low, high = stats(3, 9, 1) # unpacking
x = 10
def f():
x = 5 # local! does not touch the global
f()
print(x) # 10
square = lambda n: n ** 2 # anonymous
გამეორება 4/4Revision 4/4
სტრიქონებიStrings
s = "Python"
s[0] # 'P'
s[-1] # 'n'
s[0:3] # 'Pyt' 3 is excluded
s[::-1] # 'nohtyP'
len(s) # 6
s.upper() s.lower() s.title() s.strip()
s.replace("a", "b")
s.split(",") # str → list
",".join(["a", "b"]) # list → str
s.find("th") # 2, or -1 if not found
s.index("th") # 2, or ValueError if not found
s.count("t")
"th" in s # True
"123".isdigit() # True
f"{name:<10}{score:>8.2f}" # alignment
f"{n:05}" # 00042
f"{0.256:.1%}" # 25.6%
გახსოვდესსტრიქონი უცვლელია:
s.upper() არაფერს ცვლის — უნდა დაწერო s = s.upper().
RememberStrings are immutable: s.upper() changes nothing — you must write s = s.upper().
სავარჯიშოPractice
ტესტური კითხვები — 1–5Test questions — 1–5
1. რას დაბეჭდავს print(type(10 / 2))?
1. What does print(type(10 / 2)) print?
<class 'float'> — ოპერატორი / ყოველთვის float-ს აბრუნებს, მაშინაც კი, როცა შედეგი მთელია.
<class 'float'> — the / operator always returns a float, even when the result is a whole number.
2. რომელი სახელია დაუშვებელი ცვლადისთვის? _x · x2 · 2x · X
2. Which of these is an invalid variable name? _x · x2 · 2x · X
2x — იდენტიფიკატორი ციფრით ვერ დაიწყება.
2x — an identifier cannot start with a digit.
3. რას დაბეჭდავს print(list(range(1, 10, 3)))?
3. What does print(list(range(1, 10, 3))) print?
[1, 4, 7].[1, 4, 7].4. რას აბრუნებს "Python".find("z")?
4. What does "Python".find("z") return?
-1. index() იმავე შემთხვევაში ValueError-ს ისვრის.
-1. In the same situation index() raises a ValueError.
5. რას დაბეჭდავს? s = "abc"; s.upper(); print(s)
5. What does this print? s = "abc"; s.upper(); print(s)
abc. სტრიქონი უცვლელია — შედეგი არსად შეინახა.
abc. Strings are immutable — the result was never stored.
სავარჯიშოPractice
ტესტური კითხვები — 6–10Test questions — 6–10
6. რას დაბეჭდავს print(bool("False"))?
6. What does print(bool("False")) print?
True — ესაა არაცარიელი სტრიქონი. მცდარია მხოლოდ ცარიელი "".
True — it is a non-empty string. Only the empty "" is falsy.
7. რას აბრუნებს ფუნქცია return-ის გარეშე?
7. What does a function without a return return?
None.None.8. რას დაბეჭდავს print(divmod(17, 5))?
8. What does print(divmod(17, 5)) print?
(3, 2) — მთელი ნაწილი და ნაშთი, tuple-ად.
(3, 2) — the quotient and the remainder, as a tuple.
9. რომელი ჩანაწერი შეაბრუნებს სტრიქონს? 9. Which expression reverses a string?
s[::-1].s[::-1].10. რას დაბეჭდავს print(2 + 3 * 4 ** 2)?
10. What does print(2 + 3 * 4 ** 2) print?
50. ჯერ 4**2=16, მერე 3*16=48, ბოლოს 2+48=50.
50. First 4**2=16, then 3*16=48, finally 2+48=50.
სავარჯიშოPractice
ღია კითხვები — 1–5Open questions — 1–5
1. აღწერეთ Python-ის პროგრამის შესრულების ეტაპები. 1. Describe the stages of executing a Python program.
წყარო
.py → ინტერპრეტატორი თარგმნის ბაიტკოდში (ინახება __pycache__-ში, .pyc) → PVM (Python-ის ვირტუალური მანქანა) ასრულებს ბაიტკოდს → შედეგი. სწორედ ბაიტკოდის გამო მუშაობს ერთი და იგივე ფაილი სხვადასხვა ოპერაციულ სისტემაზე.
Source .py → the interpreter compiles it to bytecode (stored as .pyc in __pycache__) → the PVM (Python Virtual Machine) executes that bytecode → the result. It is precisely this bytecode layer that lets the same file run on different operating systems.
2. რით განსხვავდება for და while? მოიყვანეთ თითო მაგალითი.
2. How do for and while differ? Give one example of each.
for — გავლა ცნობილ თანმიმდევრობაზე ან რაოდენობაზე (for i in range(10)). while — სრულდება, სანამ პირობა ჭეშმარიტია, როცა რაოდენობა წინასწარ არაა ცნობილი (პაროლის შემოწმება).
for walks a known sequence or count (for i in range(10)). while runs as long as a condition holds, for cases where the count is not known in advance (checking a password).
3. რა განსხვავებაა ლოკალურ და გლობალურ ცვლადს შორის? 3. What is the difference between a local and a global variable?
ლოკალური იქმნება ფუნქციის შიგნით და მისი დასრულებისას ქრება — გარედან მიუწვდომელია. გლობალური ცხოვრობს მთელი პროგრამის განმავლობაში; ფუნქციას მისი წაკითხვა შეუძლია, შეცვლა — მხოლოდ
global-ით (რაც არასასურველია).
A local variable is created inside a function and disappears when it ends — it is unreachable from outside. A global lives for the whole program; a function can read it, but changing it requires global (which is discouraged).
4. რას ნიშნავს „სტრიქონი უცვლელია“ და რა გამომდინარეობს აქედან? 4. What does “strings are immutable” mean, and what follows from it?
შექმნის შემდეგ სტრიქონის ცალკეული სიმბოლოს შეცვლა შეუძლებელია (
s[0] = "x" → TypeError). ყველა მეთოდი ახალ სტრიქონს აბრუნებს, ამიტომ შედეგი ცვლადში უნდა შევინახოთ: s = s.upper().
Once created, an individual character cannot be changed (s[0] = "x" → TypeError). Every method returns a new string, so the result must be stored: s = s.upper().
5. რატომ არის 0.1 + 0.2 != 0.3?
5. Why is 0.1 + 0.2 != 0.3?
float ორობით სისტემაში ინახება, სადაც 0.1 და 0.2 ზუსტად ვერ ჩაიწერება (ისევე, როგორც 1/3 ათობითში). ჯამი გამოდის 0.30000000000000004. შედარებისთვის იყენებენ round()-ს ან math.isclose()-ს.
A float is stored in binary, where 0.1 and 0.2 cannot be represented exactly (just as 1/3 cannot in decimal). The sum comes out as 0.30000000000000004. For comparison, use round() or math.isclose().
სავარჯიშოPractice
ღია კითხვები — 6–10Open questions — 6–10
6. რას აკეთებს break, continue და ციკლის else?
6. What do break, continue and a loop’s else do?
break — ციკლს მთლიანად წყვეტს. continue — მიმდინარე ბიჯს ტოვებს და შემდეგზე გადადის. else — სრულდება მხოლოდ მაშინ, თუ ციკლი break-ის გარეშე დასრულდა.
break ends the loop entirely. continue skips the current iteration and moves on. else runs only if the loop finished without a break.
7. რა განსხვავებაა return-სა და print-ს შორის?
7. What is the difference between return and print?
print ტექსტს ეკრანზე გამოაქვს — ეს ადამიანისთვისაა, დაბრუნებული მნიშვნელობა None-ია. return მნიშვნელობას გამომძახებელს უბრუნებს — მისი შემდგომი გამოთვლებში გამოყენება შესაძლებელია.
print writes text to the screen — that is for a human, and the returned value is None. return hands a value back to the caller, so it can be used in further calculations.
8. რას ნიშნავს ნაგულისხმევი (გაჩუმებით) პარამეტრი? რა წესი მოქმედებს? 8. What is a default parameter, and what rule applies to it?
პარამეტრი, რომელსაც აღწერისას მნიშვნელობა ენიჭება და გამოძახებისას შეიძლება არ გადავცეთ:
def greet(name, msg="გამარჯობა"). წესი — ასეთი პარამეტრები ყოველთვის ბოლოში იწერება.
A parameter given a value in the definition, which the caller may omit: def greet(name, msg="hello"). The rule — such parameters always come last.
9. აღწერეთ სამი განსხვავება find()-სა და index()-ს შორის.
9. Describe the difference between find() and index().
ორივე ეძებს ქვესტრიქონს და აბრუნებს პირველი დამთხვევის ინდექსს. განსხვავება: ვერ პოვნისას
find აბრუნებს -1-ს, index კი ისვრის ValueError-ს. ამიტომ find უსაფრთხოა, როცა არსებობა დარწმუნებული არაა.
Both search for a substring and return the index of the first match. The difference: when nothing is found find returns -1, while index raises a ValueError. So find is the safe choice when you are not sure it is there.
10. რას აკეთებს split() და join()? მოიყვანეთ მაგალითი.
10. What do split() and join() do? Give an example.
split() სტრიქონს ჰყოფს გამყოფის მიხედვით და სიას აბრუნებს: "a,b".split(",") → ['a','b']. join() პირიქით — სიის ელემენტებს ერთ სტრიქონად აერთებს: ",".join(['a','b']) → "a,b".
split() breaks a string on a separator and returns a list: "a,b".split(",") → ['a','b']. join() does the opposite — it joins list elements into one string: ",".join(['a','b']) → "a,b".
სავარჯიშოPractice
პრაქტიკული ამოცანებიCoding exercises
- ციფრების ანალიზიმთელი რიცხვისთვის იპოვე: ციფრების ჯამი, რაოდენობა, უდიდესი ციფრი და შებრუნებული რიცხვი. მხოლოდ
%და//-ით - ფუნქცია
is_prime(n)დაწერე ფუნქცია და გამოიყენე 1–100 დიაპაზონში ყველა მარტივი რიცხვის დასაბეჭდად - ტექსტის სტატისტიკასიმბოლოების, სიტყვების, ხმოვნების რაოდენობა; ყველაზე გრძელი სიტყვა; შებრუნებული ტექსტი
- ბანკის კალკულატორითანხა და წლიური პროცენტი შეიტანე; დაბეჭდე ბალანსი ყოველი წლისთვის, სანამ არ გაორმაგდება
- მენიუ
while True+ ფუნქციები: 4 არითმეტიკული ოპერაცია, 0 — გასვლა. ნულზე გაყოფა უნდა შემოწმდეს
- Digit analysisFor an integer, find: the sum of its digits, how many there are, the largest digit and the number reversed. Using only
%and// - An
is_prime(n)functionWrite it, then use it to print every prime from 1 to 100 - Text statisticsCounts of characters, words and vowels; the longest word; the text reversed
- A savings calculatorTake an amount and an annual rate; print the balance each year until it doubles
- A menu
while True+ functions: four arithmetic operations, 0 to exit. Division by zero must be handled
რჩევაამოცანა ჯერ ქაღალდზე დაშალე ნაბიჯებად, მერე დაწერე. გამოცდაზე ეს დროს დაზოგავს, არა დაგახარჯვინებს.
AdviceBreak the problem into steps on paper first, then write it. In an exam that saves time rather than costing it.
ყურადღებაWatch out
ტიპური შეცდომები გამოცდაზეTypical exam mistakes
| შეცდომაMistake | სწორადCorrect |
|---|---|
age = input("ასაკი: ") და შემდეგ age + 1
age = input("age: ") then age + 1 |
age = int(input(...)) |
range(1, 5) როცა 5 გჭირდებაrange(1, 5) when you need 5 |
range(1, 6) |
if x = 5: | if x == 5: |
s.upper() შედეგის შენახვის გარეშეs.upper() without storing the result |
s = s.upper() |
while-ში მთვლელი არ იზრდებაThe counter never grows in a while |
count += 1 სხეულშიcount += 1 in the body |
ფუნქციაში print return-ის ნაცვლადprint instead of return in a function |
return result |
| დამგროვებელი ცვლადი ციკლის შიგნით ინიციალიზდებაThe accumulator is initialised inside the loop | გაიტანე ციკლის წინMove it before the loop |
| ორწერტილი ან წანაცვლება დაგავიწყდაForgot the colon or the indentation | if x > 0: + 4 ჰარეif x > 0: + 4 spaces |
ეს ცხრილი შუალედურის შემდეგ IX კვირაზეც გამოგადგება შედეგების განხილვისას.
This table is useful again in week IX when going over the results.
ტაქტიკაTactics
გამოცდის დღესOn exam day
- ჯერ გადაათვალიერე ყველა კითხვადაიწყე იმით, რაც ზუსტად იცი — ქულა უკვე ჯიბეშია
- ღია კითხვაზე ტერმინოლოგია მნიშვნელოვანია2 ქულა იწერება, როცა პასუხი სრული და ტერმინოლოგიურად გამართულია
- კოდის ამოცანაზე ჯერ ალგორითმი დაწერენაწილობრივ სწორი გზაც ფასდება — ცარიელი ფურცელი არა
- შეამოწმე კიდურა შემთხვევები0, უარყოფითი, ცარიელი ტექსტი — სწორედ იქ იმალება შეცდომა
- წანაცვლება და ორწერტილიქაღალდზეც კი — ეს შეფასების კრიტერიუმია
- Skim every question firstStart with what you know for certain — those points are already banked
- Terminology matters on open questionsFull marks go to answers that are complete and use the right terms
- Write the algorithm before the codeA partially correct approach still earns marks — a blank page does not
- Check the edge cases0, negatives, empty text — that is exactly where the bug hides
- Indentation and colonsEven on paper — they are part of the marking criteria
შემდეგი — ლექცია 09შუალედურის შედეგების განხილვა + ფაილებთან მუშაობა.
Next — lecture 09Going over the midterm results + working with files.