ლექცია 03 · III კვირაLecture 03 · Week III
პირობები და
ლოგიკაConditions and
logic
შედარების ოპერატორები · ლოგიკური ოპერატორები · if / elif / else
Comparison operators · logical operators · if / elif / else
დღეს ასევე: პრაქტიკული დავალება #1 Also today: practical assignment #1
გამეორება · ლექციები 01–02Review · lectures 01–02
შემოწმება — 8 კითხვაCheck yourself — 8 questions
იფიქრე, მერე გახსენი პასუხი.Think first, then open the answer.
1. რა ეტაპებს გადის .py ფაილი შესრულებამდე?
1. What stages does a .py file go through before it runs?
.py → თარგმნა ბაიტკოდში (.pyc, __pycache__) → შესრულება PVM-ის (Python-ის ვირტუალური მანქანის) მიერ.
Source .py → compilation to bytecode (.pyc in __pycache__) → execution by the PVM (Python Virtual Machine).
2. რით ქმნის Python კოდის ბლოკს, თუ ფიგურული ფრჩხილები არ აქვს? 2. How does Python create a code block without curly braces?
:.
By indentation — the standard is 4 spaces. A colon : opens the block.
3. რას დაბეჭდავს print(7 // 2, 7 % 2, 7 / 2)?
3. What does print(7 // 2, 7 % 2, 7 / 2) print?
3 1 3.5 — მთელი გაყოფა, ნაშთი და ჩვეულებრივი გაყოფა (ყოველთვის float).
3 1 3.5 — floor division, remainder, and true division (always a float).
4. რა ტიპს აბრუნებს input()?
4. What type does input() return?
str-ს. რიცხვად გამოსაყენებლად საჭიროა int(input(...)) ან float(input(...)).
Always str. To use it as a number you need int(input(...)) or float(input(...)).
5. რას დაბეჭდავს print(2 + 3 * 4) და რატომ?
5. What does print(2 + 3 * 4) print, and why?
14. გამრავლებას შეკრებაზე მაღალი პრიორიტეტი აქვს. 20-ის მისაღებად საჭიროა (2 + 3) * 4.
14. Multiplication binds tighter than addition. To get 20 you need (2 + 3) * 4.
6. რომელი სამი სახელია დაუშვებელი ცვლადისთვის: 2name, _x, my name, class, Age?
6. Which three of these are invalid variable names: 2name, _x, my name, class, Age?
2name (ციფრით იწყება), my name (ჰარე), class (რეზერვირებული სიტყვა). _x და Age დასაშვებია.
2name (starts with a digit), my name (contains a space), class (reserved word). _x and Age are fine.
7. რას აბრუნებს int(9.99) და რით განსხვავდება round(9.99)-ისგან?
7. What does int(9.99) return, and how does it differ from round(9.99)?
int(9.99) → 9 (წილად ნაწილს კვეცავს), round(9.99) → 10 (ამრგვალებს).
int(9.99) → 9 (truncates the fraction), round(9.99) → 10 (rounds).
8. რატომ არის 0.1 + 0.2 == 0.3 მცდარი?
8. Why is 0.1 + 0.2 == 0.3 false?
0.30000000000000004. ეს ყველა ენას ეხება, არა მხოლოდ Python-ს.
0.1 and 0.2 cannot be represented exactly in binary, so the sum comes out as 0.30000000000000004. This affects every language, not just Python.
ოპერატორებიOperators
შედარების ოპერატორებიComparison operators
შედეგი ყოველთვის bool-ია: True ან False.
The result is always a bool: True or False.
| ოპ.Op. | მნიშვნელობაMeaning | მაგალითიExample | შედეგიResult |
|---|---|---|---|
== | ტოლიაEqual to | 5 == 5 | True |
!= | არ არის ტოლიNot equal to | 5 != 3 | True |
> | მეტიაGreater than | 5 > 10 | False |
< | ნაკლებიაLess than | "a" < "b" |
True (ანბანურად)(alphabetically) |
>= | მეტია ან ტოლიGreater or equal | 5 >= 5 | True |
<= | ნაკლებია ან ტოლიLess or equal | 4 <= 3 | False |
= არის მინიჭება, == — შედარება. if x = 5: სინტაქსური შეცდომაა.
The most common mistake= is assignment, == is comparison. if x = 5: is a syntax error.
ოპერატორებიOperators
ლოგიკური ოპერატორებიLogical operators
and
ორივე უნდა იყოს ჭეშმარიტი
Both must be true
age = 20
has_id = True
print(age >= 18 and has_id) # True
or
ერთი მაინც ჭეშმარიტი
At least one must be true
is_weekend = False
is_holiday = True
print(is_weekend or is_holiday) # True
not
უარყოფა — ცვლის საპირისპიროდ
Negation — flips the value
is_open = False
print(not is_open) # True
and = „და“, or = „ან“, not = „არა“. კოდი ხმამაღლა წაიკითხე — თუ წინადადებად აზრი აქვს, სავარაუდოდ სწორია.
Read it out loudThese operators are plain English words on purpose. If the line makes sense as a sentence, it is probably correct.
თეორიაTheory
ჭეშმარიტების ცხრილიTruth table
| A | B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
| A | not A |
|---|---|
| True | False |
| False | True |
პრიორიტეტიPrecedence
not— ყველაზე მაღალიandor— ყველაზე დაბალი
not— highestandor— lowest
print(True or False and False)
# True — "and" is evaluated first
print((True or False) and False)
# False
თავისებურებაA key trait
„დამოკლებული“ გამოთვლა (short-circuit)Short-circuit evaluation
Python ლოგიკურ გამოსახულებას ბოლომდე არ ითვლის, თუ პასუხი უკვე ცნობილია.
Python stops evaluating a logical expression as soon as the answer is known.
x = 0
# and: if the first part is False, the second is never checked
print(x != 0 and 10 / x > 1) # False — no division happened
# or: if the first part is True, the second is never checked
print(x == 0 or 10 / x > 1) # True
10 / x != 0 and x != 0 (ავარია), მერე შეაბრუნე თანმიმდევრობა.
A good spot to crash it live: write 10 / x != 0 and x != 0 first (it crashes), then reverse the order.
კონსტრუქციაConstruct
if — უმარტივესი პირობაif — the simplest condition
if condition:
statement1
statement2
temperature = 31
if temperature > 30:
print("ძალიან ცხელა")
print("წყალი დალიე")
print("პროგრამა დასრულდა")
- ორწერტილი სავალდებულოა
- ბლოკი წანაცვლებულია 4 ჰარით
- ბლოკის გარეთ დაწერილი კოდი ყოველთვის სრულდება
- ფრჩხილები პირობის გარშემო საჭირო არაა (C/Java-სგან განსხვავებით)
- The colon is mandatory
- The block is indented by 4 spaces
- Code outside the block always runs
- Parentheses around the condition are not needed (unlike C/Java)
SyntaxError
ბლოკი არ წაანაცვლე → IndentationError
Two classic mistakes
Forgot the colon → SyntaxError
Did not indent the block → IndentationError
კონსტრუქციაConstruct
if / else დაand elif
age = int(input("ასაკი: "))
if age >= 18:
print("სრულწლოვანი")
else:
print("არასრულწლოვანი")
score = int(input("ქულა: "))
if score >= 91:
grade = "A"
elif score >= 81:
grade = "B"
elif score >= 71:
grade = "C"
elif score >= 61:
grade = "D"
elif score >= 51:
grade = "E"
else:
grade = "F"
print("შეფასება:", grade)
score >= 51 დაწერე, ყველა ქულა „E“ გახდება.
The key rulePython checks conditions top to bottom and stops at the first true one. Order therefore matters — put score >= 51 at the top and every score becomes “E”.
კონსტრუქციაConstruct
ჩადგმული პირობებიNested conditions
age = 20
has_ticket = True
if age >= 18:
if has_ticket:
print("შედი")
else:
print("იყიდე ბილეთი")
else:
print("ასაკი არ გაძლევს უფლებას")
if age >= 18 and has_ticket:
print("შედი")
elif age >= 18:
print("იყიდე ბილეთი")
else:
print("ასაკი არ გაძლევს უფლებას")
სცადე REPL-ში: import this — Python-ის ფილოსოფიის 19 პრინციპი.
Try it in the REPL: import this — the 19 principles of Python’s philosophy.
მოხერხებულობაConvenience
ჯაჭვური შედარება და inChained comparison and in
ჯაჭვიChaining
x = 15
# in other languages:
if x > 10 and x < 20:
print("დიაპაზონში")
# in Python, naturally:
if 10 < x < 20:
print("დიაპაზონში")
if 0 <= score <= 100:
print("ქულა კორექტულია")
in — „ეკუთვნის?““is it in there?”
letter = "ა"
print(letter in "გამარჯობა") # True
day = "შაბათი"
if day in ("შაბათი", "კვირა"):
print("დასვენება")
print("x" not in "python") # True
დეტალურად in-ს სიებთან (X კვირა) და ლექსიკონებთან (XI კვირა) გავიხსენებთ.
We return to in with lists (week X) and dictionaries (week XI).
დამატებითExtras
ტერნარული ოპერატორი და isThe ternary operator and is
პირობა ერთ ხაზშიA condition on one line
age = 20
# the usual way
if age >= 18:
status = "სრულწლოვანი"
else:
status = "არასრულწლოვანი"
# on one line
status = "სრულწლოვანი" if age >= 18 else "არასრულწლოვანი"
გამოიყენე მხოლოდ მაშინ, როცა მართლა კითხვადია. Use it only when it genuinely reads better.
== თუor is?
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True — equal values
print(a is b) # False — different objects
x = None
print(x is None) # correct style
print(x == None) # works, but poor style
is მხოლოდ None, True, False-სთან. დანარჩენ შემთხვევაში — ==.
RuleUse is only with None, True, False. Everywhere else — ==.
სრული მაგალითიFull example
წელიწადის დღეების კალკულატორიLeap-year calculator
# ნაკიანი წელი: იყოფა 4-ზე, მაგრამ არა 100-ზე,
# გარდა იმ შემთხვევისა, როცა 400-ზეც იყოფა
# a leap year is divisible by 4, but not by 100,
# unless it is also divisible by 400
year = int(input("წელი: "))
if year % 400 == 0:
is_leap = True
elif year % 100 == 0:
is_leap = False
elif year % 4 == 0:
is_leap = True
else:
is_leap = False
if is_leap:
print(f"{year} ნაკიანია — 366 დღე")
else:
print(f"{year} ჩვეულებრივია — 365 დღე")
# the same as one logical expression:
is_leap = year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
შეფასებაAssessment
პრაქტიკული დავალება #1Practical assignment #1
8 ქულა · სრულდება აუდიტორიაში, დამოუკიდებლად 8 points · done in class, independently
- ლუწი/კენტი + ნიშანიმომხმარებელი შეიყვანს მთელ რიცხვს. დაბეჭდე, ლუწია თუ კენტი და დადებითია, უარყოფითი თუ ნული
- უდიდესი სამიდანსამი რიცხვიდან იპოვე უდიდესი (ჩაშენებული
max()-ის გარეშე) - შეფასების კონვერტორი0–100 ქულა → A/B/C/D/E/F ასოითი სკალით. არაკორექტულ ქულაზე დაბეჭდე შეტყობინება
- ბილეთის ფასიასაკი < 6 — უფასო; 6–17 — 50% ფასდაკლება; 18–64 — სრული; ≥ 65 — 30% ფასდაკლება
- Even/odd + signThe user enters an integer. Print whether it is even or odd, and positive, negative or zero
- Largest of threeFind the largest of three numbers — without the built-in
max() - Grade converterA score of 0–100 → an A/B/C/D/E/F letter grade. Print a message for an invalid score
- Ticket priceAge < 6 — free; 6–17 — 50% off; 18–64 — full price; ≥ 65 — 30% off
შემაჯამებელიWrap-up
რა უნდა დაგამახსოვრდესWhat to remember
- შედარება აბრუნებს
bool-ს;=ანიჭებს,==ადარებს and·or·not— პრიორიტეტი სწორედ ამ თანმიმდევრობით ეცემაif / elif / else: მოწმდება ზემოდან ქვემოთ, ჩერდება პირველივე ჭეშმარიტზე- ორწერტილი + წანაცვლება — სავალდებულო
- ჯაჭვი
10 < x < 20დაinკოდს კითხვადს ხდის isმხოლოდNone/True/False-სთვის
- Comparison returns a
bool;=assigns,==compares not·and·or— precedence falls in exactly that orderif / elif / else: checked top to bottom, stops at the first true branch- Colon + indentation — both mandatory
- Chaining
10 < x < 20andinmake code readable isonly forNone/True/False
for, while, range(), break და continue.
Next — lecture 04Loops: for, while, range(), break and continue.