ლექცია 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?
წანაცვლებით (indentation) — სტანდარტი 4 ჰარეა. ბლოკს ხსნის ორწერტილი :. 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.1 და 0.2 ზუსტად ვერ ჩაიწერება, ამიტომ ჯამი გამოდის 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 to5 == 5True
!=არ არის ტოლიNot equal to5 != 3True
>მეტიაGreater than5 > 10False
<ნაკლებიაLess than"a" < "b" True (ანბანურად)(alphabetically)
>=მეტია ან ტოლიGreater or equal5 >= 5True
<=ნაკლებია ან ტოლიLess or equal4 <= 3False
ყველაზე ხშირი შეცდომა= არის მინიჭება, ==შედარება. 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

ABA and BA or B
TrueTrueTrueTrue
TrueFalseFalseTrue
FalseTrueFalseTrue
FalseFalseFalseFalse
Anot A
TrueFalse
FalseTrue

პრიორიტეტიPrecedence

  1. not — ყველაზე მაღალი
  2. and
  3. or — ყველაზე დაბალი
  1. not — highest
  2. and
  3. or — lowest
print(True or False and False)
# True  — "and" is evaluated first

print((True or False) and False)
# False
ისევფრჩხილები აზრს ცხადს ხდის. დაწერე ისინი. AgainParentheses make the intent explicit. Write them.

თავისებურება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
პრაქტიკული სარგებელიესაა სტანდარტული ხერხი ნულზე გაყოფის ან ცარიელი მონაცემის „ავარიის“ ასაცილებლად — ჯერ ამოწმებ საშიშ პირობას, მერე იყენებ. Why it is usefulThis is the standard way to avoid dividing by zero or touching empty data — you check the dangerous condition first, then use the value.
კარგი ადგილია ავარიის ცოცხლად საჩვენებლად: ჯერ დაწერე 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)
მთავარი წესიPython პირობებს ამოწმებს ზემოდან ქვემოთ და ჩერდება პირველივე ჭეშმარიტზე. ამიტომ თანმიმდევრობა მნიშვნელოვანია — თუ ზემოთ 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”.
ეს ECTS-ის ასოითი სკალაა — სტუდენტებს ხიბლავს, რომ საკუთარ ქულას ითვლიან. საჭიროების შემთხვევაში ზღვრები შენი კურსის მიხედვით შეცვალე. This is the ECTS letter scale — students enjoy computing their own grade. Adjust the thresholds to your own course if needed.

კონსტრუქცია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("ასაკი არ გაძლევს უფლებას")
PEP 20 (Zen of Python)„Flat is better than nested“ — ბრტყელი სჯობს ჩადგმულს. სამზე მეტი დონე უკვე გადასაწერი ნიშანია. PEP 20 (Zen of Python)“Flat is better than nested.” More than three levels is a sign to rewrite.

სცადე 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)
კარგი დისკუსია: რომელი ვარიანტი სჯობს? ხუთხაზიანი if-ჯაჭვი უფრო წაკითხვადია, ერთხაზიანი — უფრო კომპაქტური. სწორი პასუხი კონტექსტზეა დამოკიდებული. A good discussion: which version is better? The if-chain reads more clearly, the one-liner is more compact. The right answer depends on context.

შეფასებაAssessment

პრაქტიკული დავალება #1Practical assignment #1

8 ქულა · სრულდება აუდიტორიაში, დამოუკიდებლად 8 points · done in class, independently

  1. ლუწი/კენტი + ნიშანიმომხმარებელი შეიყვანს მთელ რიცხვს. დაბეჭდე, ლუწია თუ კენტი და დადებითია, უარყოფითი თუ ნული
  2. უდიდესი სამიდანსამი რიცხვიდან იპოვე უდიდესი (ჩაშენებული max()-ის გარეშე)
  3. შეფასების კონვერტორი0–100 ქულა → A/B/C/D/E/F ასოითი სკალით. არაკორექტულ ქულაზე დაბეჭდე შეტყობინება
  4. ბილეთის ფასიასაკი < 6 — უფასო; 6–17 — 50% ფასდაკლება; 18–64 — სრული; ≥ 65 — 30% ფასდაკლება
  1. Even/odd + signThe user enters an integer. Print whether it is even or odd, and positive, negative or zero
  2. Largest of threeFind the largest of three numbers — without the built-in max()
  3. Grade converterA score of 0–100 → an A/B/C/D/E/F letter grade. Print a message for an invalid score
  4. Ticket priceAge < 6 — free; 6–17 — 50% off; 18–64 — full price; ≥ 65 — 30% off
შეფასების კრიტერიუმი3 ქ. — სრულად შესრულებული · 2 ქ. — დამოუკიდებლად, დროში · 3 ქ. — სწორი გზა, მექანიკური შეცდომების გარეშე. Marking criteria3 pts — completed in full · 2 pts — done independently, on time · 3 pts — correct approach, no mechanical errors.

შემაჯამებელიWrap-up

რა უნდა დაგამახსოვრდესWhat to remember

შემდეგი — ლექცია 04ციკლები: for, while, range(), break და continue. Next — lecture 04Loops: for, while, range(), break and continue.