ლექცია 05 · V კვირაLecture 05 · Week V
ჩაშენებული
ფუნქციებიBuilt-in
functions
რიცხვებთან სამუშაო ფუნქციები და მეთოდები · მოდული math · შემთხვევითი რიცხვები
Functions for working with numbers · the math module · random numbers
გამეორება · ლექციები 03–04Review · lectures 03–04
შემოწმება — 8 კითხვაCheck yourself — 8 questions
1. რა განსხვავებაა =-სა და ==-ს შორის?
1. What is the difference between = and ==?
= — მინიჭება (მარჯვენა მნიშვნელობას მარცხენა სახელს აბამს), == — შედარება, აბრუნებს True/False.
= is assignment (binds the right-hand value to the left-hand name); == is comparison and returns True/False.
2. რას დაბეჭდავს? print(True or False and False)
2. What does print(True or False and False) print?
True. and-ის პრიორიტეტი or-ზე მაღალია, ამიტომ ჯერ False and False → False, შემდეგ True or False → True.
True. and binds tighter than or, so first False and False → False, then True or False → True.
3. რამდენ ბიჯს გაივლის for i in range(1, 10, 3) და რა მნიშვნელობებით?
3. How many steps does for i in range(1, 10, 3) take, and with what values?
1, 4, 7. 10 არ შედის.
3 steps: 1, 4, 7. 10 is excluded.
4. რა განსხვავებაა break-სა და continue-ს შორის?
4. What is the difference between break and continue?
break ციკლს მთლიანად წყვეტს; continue მხოლოდ მიმდინარე ბიჯს ტოვებს და შემდეგზე გადადის.
break ends the loop entirely; continue only skips the current iteration and moves to the next.
5. როდის სრულდება else, რომელიც for-ს მიება?
5. When does an else attached to a for run?
break-ის გარეშე დასრულდა ბოლომდე. თუ break გასრულდა — else გამოტოვდება.
When the loop ran to completion without hitting break. If break fired, the else is skipped.
6. რატომ არის ეს ციკლი უსასრულო?
i = 0
while i < 5: print(i)
6. Why is this loop infinite?
i = 0
while i < 5: print(i)
i += 1. პირობა i < 5 სამუდამოდ ჭეშმარიტი რჩება. გაჩერება — Ctrl+C.
The change is missing: i += 1. The condition i < 5 stays true forever. Stop it with Ctrl+C.
7. რას დაბეჭდავს? x = 15; print(10 < x < 20)
7. What does x = 15; print(10 < x < 20) print?
True. Python უშვებს ჯაჭვურ შედარებას — ტოლფასია 10 < x and x < 20-ისა.
True. Python allows chained comparisons — it is equivalent to 10 < x and x < 20.
8. რას აკეთებს n % 10 და n // 10 ერთად, ციკლში?
8. What do n % 10 and n // 10 do together, inside a loop?
n % 10 გამოყოფს ბოლო ციფრს, n // 10 კი აშორებს მას. ერთად — რიცხვის ციფრებზე გავლის სტანდარტული ხერხია.
n % 10 extracts the last digit, n // 10 removes it. Together they are the standard way to walk the digits of a number.
ცნებაConcept
ფუნქცია — „შავი ყუთი“A function is a black box
იღებს არგუმენტს, აბრუნებს შედეგს. როგორ აკეთებს — შენი საზრუნავი არაა.
It takes an argument and returns a result. How it does that is not your concern.
result = len("გამარჯობა")
# ↑ ↑
# function argument
print(result) # 9
ჩაშენებულიBuilt in
უკვე არსებობს — print, len, abs… სულ ~70. იმპორტი არ სჭირდება.
Already there — print, len, abs… about 70 of them. No import needed.
მოდულიდანFrom a module
საჭიროებს import-ს — math.sqrt, random.randint.
Requires an import — math.sqrt, random.randint.
შენს საკუთარ ფუნქციებს — VI კვირაზე. Your own functions come in week VI.
რიცხვებიNumbers
ჩაშენებული ფუნქციები რიცხვებისთვისBuilt-in functions for numbers
| ფუნქციაFunction | მაგალითიExample | შედეგიResult |
|---|---|---|
abs(x) | abs(-7.5) |
7.5 — მოდულიabsolute value |
round(x, n) | round(3.14159, 2) | 3.14 |
pow(x, y) | pow(2, 10) |
1024 — იგივეsame as 2 ** 10 |
divmod(a, b) | divmod(17, 5) |
(3, 2) — მთელი და ნაშთიquotient and remainder |
min() / max() | max(4, 9, 2) | 9 |
sum() | sum(range(1, 101)) | 5050 |
int() float() str() | int("42") | 42 |
bin() oct() hex() | bin(10) | '0b1010' |
round()-ის თავისებურებაround(0.5) → 0, round(1.5) → 2. ესაა „ბანკირის დამრგვალება“ — ლუწისკენ. სტანდარტია და შეცდომა არაა.
A quirk of round()round(0.5) → 0, round(1.5) → 2. This is banker’s rounding — to the nearest even. It is the standard, not a bug.
მოდულიModule
math — მათემატიკური ფუნქციებიmath — mathematical functions
import math
print(math.sqrt(16)) # 4.0 square root
print(math.ceil(4.1)) # 5 round up
print(math.floor(4.9)) # 4 round down
print(math.trunc(-4.9)) # -4 drop the fraction
print(math.factorial(5)) # 120 factorial
print(math.gcd(12, 18)) # 6 greatest common divisor
print(math.pi) # 3.141592653589793
print(math.e) # 2.718281828459045
print(math.log(100, 10)) # 2.0 logarithm
print(math.log10(1000)) # 3.0
print(math.exp(1)) # 2.718…
მოდულიModule
math — ტრიგონომეტრია და გრადუსებიmath — trigonometry and degrees
import math
# trigonometric functions work in radians!
angle_deg = 90
angle_rad = math.radians(angle_deg)
print(math.sin(angle_rad)) # 1.0
print(math.cos(0)) # 1.0
print(math.degrees(math.pi)) # 180.0
# circumference and area of a circle
r = 5
print(f"სიგრძე: {2 * math.pi * r:.2f}")
print(f"ფართობი: {math.pi * r ** 2:.2f}")
# distance between two points
print(math.hypot(3, 4)) # 5.0
print(math.dist((0, 0), (3, 4))) # 5.0
math.sin(90) არ იძლევა 1-ს — 90 რადიანად აღიქმება. ჯერ math.radians(90).
A typical mistakemath.sin(90) does not give 1 — the 90 is read as radians. Convert first with math.radians(90).
იმპორტიImports
იმპორტის სამი ფორმაThree forms of import
# 1. the whole module — the most common and the clearest
import math
print(math.sqrt(16))
# 2. specific names
from math import sqrt, pi
print(sqrt(16), pi)
# 3. a short alias
import math as m
print(m.sqrt(16))
from math import * — ყველა სახელს ერთბაშად შემოიტანს და შენს ცვლადებს „გადააფარებს“. PEP 8 ამას პირდაპირ კრძალავს.
What we never dofrom math import * — it dumps every name into your scope and can shadow your own variables. PEP 8 forbids it outright.
დეტალურად მოდულებსა და პაკეტებს — XV კვირაზე. Modules and packages in depth — week XV.
მოდულიModule
random — შემთხვევითი რიცხვებიrandom — random numbers
import random
print(random.random()) # 0.0 ≤ x < 1.0
print(random.randint(1, 6)) # 1..6 — both ends included!
print(random.randrange(0, 10, 2)) # 0,2,4,6,8
print(random.uniform(1.5, 3.5)) # a float in the range
colors = ["წითელი", "მწვანე", "ლურჯი"]
print(random.choice(colors)) # one element
print(random.sample(colors, 2)) # two, without repeats
random.shuffle(colors) # shuffle in place
print(colors)
randint vs rangerandom.randint(1, 6) — 6 შედის. range(1, 6) — 6 არ შედის. ეს განსხვავება ხშირად ავიწყდებათ.
randint vs rangerandom.randint(1, 6) includes 6. range(1, 6) excludes it. This difference is easy to forget.
თეორიაTheory
ფსევდო-შემთხვევითობაPseudo-randomness
კომპიუტერს „ნამდვილი“ შემთხვევითობა არ შეუძლია — ის ალგორითმით ითვლის მიმდევრობას, რომელიც შემთხვევითს ჰგავს.
A computer cannot produce “true” randomness — it computes, by algorithm, a sequence that merely looks random.
import random
random.seed(42)
print(random.randint(1, 100)) # 82
print(random.randint(1, 100)) # 15
random.seed(42) # back to the same point
print(random.randint(1, 100)) # 82 — identical!
print(random.randint(1, 100)) # 15
seed()-ით შედეგი განმეორებადია — ტესტირებისა და ლაბორატორიული სამუშაოსთვის აუცილებელია.
Why it is usefulWith seed() the result is reproducible — essential for testing and for lab work.
random არ ვარგა — მისთვის secrets მოდულია.
Securityrandom is not suitable for passwords or cryptographic keys — use the secrets module for those.
პრაქტიკაPractice
თამაში — გამოიცანი რიცხვიA game — guess the number
import random
secret = random.randint(1, 100)
attempts = 0
print("გამოიცანი რიცხვი 1-დან 100-მდე")
while True:
guess = int(input("შენი ვარიანტი: "))
attempts += 1
if guess < secret:
print("მეტია ↑")
elif guess > secret:
print("ნაკლებია ↓")
else:
print(f"სწორია! {attempts} ცდაში გამოიცანი")
break
ინსტრუმენტიTool
როგორ გავიგოთ, რა შეუძლია მოდულსHow to find out what a module can do
import math
print(dir(math)) # a list of every name
help(math.sqrt) # documentation for one function
print(math.sqrt.__doc__)
Help on built-in function sqrt in module math: sqrt(x, /) Return the square root of x.
docs.python.org/3/library/math.html — ყოველთვის უფრო სრულია, ვიდრე ნებისმიერი გაკვეთილი.
The official docsdocs.python.org/3/library/math.html — always more complete than any tutorial.
შემაჯამებელიWrap-up
რა უნდა დაგამახსოვრდესWhat to remember
- ჩაშენებული ფუნქციები იმპორტს არ საჭიროებს:
abs, round, pow, divmod, min, max, sum math—sqrt, ceil, floor, factorial, gcd, pi, log; ტრიგონომეტრია რადიანებშიrandom—random(), randint(a, b)(b შედის!),choice, shuffle, sampleseed()შედეგს განმეორებადს ხდის- იმპორტი:
import math·from math import sqrt·import math as m;import *— არასდროს - უცნობი ფუნქცია —
help()დაdir()
- Built-ins need no import:
abs, round, pow, divmod, min, max, sum math—sqrt, ceil, floor, factorial, gcd, pi, log; trigonometry works in radiansrandom—random(), randint(a, b)(b included!),choice, shuffle, sampleseed()makes results reproducible- Imports:
import math·from math import sqrt·import math as m; neverimport * - For an unfamiliar function —
help()anddir()
def, პარამეტრები, return, lambda + პრაქტიკული დავალება #2.
Next — lecture 06Your own functions: def, parameters, return, lambda + practical assignment #2.