ლექცია 02 · II კვირაLecture 02 · Week II

ცვლადები და
საბაზისო ტიპები
Variables and
basic types

ცვლადების გამოცხადება · მონაცემთა ტიპები · არითმეტიკული ოპერაციები · input() და print() Declaring variables · data types · arithmetic operators · input() and print()

საბაზისოFundamentals

რა არის ცვლადიWhat is a variable

ცვლადი — სახელი, რომელიც მეხსიერებაში არსებულ მნიშვნელობაზე მიუთითებს.

A variable is a name that points to a value held in memory.

age = 20
name = "ნინო"
price = 19.99
is_student = True

ნიშანი = არაა „ტოლობა“ — ესაა მინიჭება: „მარჯვნივ გამოთვლილი მნიშვნელობა მიაბი მარცხენა სახელს“. The = sign is not “equals” — it is assignment: “bind the value computed on the right to the name on the left”.

წარმოიდგინე ასემნიშვნელობა ყუთშია, ცვლადი კი — ყუთზე გაკრული სტიკერი. ერთ ყუთს რამდენიმე სტიკერიც შეიძლება ჰქონდეს. Picture it this wayThe value sits in a box; the variable is a sticker on that box. One box can carry several stickers.
x = 5
y = x      # ორივე ერთსა და იმავეზე / both point to the same value
x = 7      # x-ს ახალი ყუთი მიება / x is bound to a new box
print(x, y)

7 5

თავისებურებაA key trait

Python დინამიკურად ტიპიზებულიაPython is dynamically typed

ტიპს არ ვწერთ — Python მას თავად ცნობს მინიჭებული მნიშვნელობიდან.

We never write the type — Python infers it from the value you assign.

x = 10          # int
x = "ტექსტი"    # ახლა str / now str
x = 3.5         # ახლა float / now float

ერთი და იმავე ცვლადის ტიპი პროგრამის მსვლელობისას იცვლება. The type of one variable can change as the program runs.

int x = 10;
x = "ტექსტი";   // შეცდომა! / error!

იქ ტიპი ერთხელ ცხადდება და აღარ იცვლება. There the type is declared once and never changes.

თავისუფლების ფასიმოქნილობა მოსახერხებელია, მაგრამ შეცდომა მხოლოდ გაშვების დროს ჩნდება. ამიტომაა Pylance სასარგებლო — ის ბევრ ასეთ პრობლემას წერისასვე ხედავს. The price of freedomFlexibility is convenient, but mistakes only surface at run time. That is why Pylance helps — it catches many of them as you type.
კარგი კითხვაა აუდიტორიისთვის: „რა უპირატესობა და რა საფრთხე აქვს ამას?“ — მსჯელობა 3–4 წუთი. A good question for the room: “what does this buy us, and what does it risk?” — 3–4 minutes of discussion.

ტიპებიTypes

საბაზისო ტიპებიThe basic types

ტიპიType რა არისWhat it is მაგალითიExample შენიშვნაNote
intმთელი რიცხვიWhole number 0, -14, 1_000_000 ზომაზე შეზღუდვა არ აქვსNo size limit
floatწილადიFractional number 3.14, -0.5, 2e3 ათწილადის გამყოფი — წერტილიDecimal separator is a dot
strტექსტიText "გამარჯობა", 'ა' ბრჭყალები ორივე გამოდგებაEither quote style works
boolლოგიკურიBoolean True, False დიდი ასოებით!Capitalised!
NoneType„არაფერი““Nothing” None „მნიშვნელობა ჯერ არაა““No value yet”
complexკომპლექსურიComplex number 2 + 3j იშვიათი, მაგრამ ჩაშენებულიRare, but built in
შემდეგშიკონტეინერული ტიპები — list, tuple (X კვირა), set, dict (XI კვირა). Coming laterContainer types — list, tuple (week X), set, dict (week XI).

ინსტრუმენტიTool

type() — რა ტიპისაა?type() — what type is it?

a = 5
b = 5.0
c = "5"
d = True
e = None

print(type(a))    # <class 'int'>
print(type(b))    # <class 'float'>
print(type(c))    # <class 'str'>
print(type(d))    # <class 'bool'>
print(type(e))    # <class 'NoneType'>

print(a == b)             # True  — same value
print(type(a) == type(b)) # False — different type
რატომაა მნიშვნელოვანი?უმეტესი შეცდომა დამწყებთან ტიპების არევიდან იბადება: "5" + 5TypeError. როცა გაუგებრობაა — დაბეჭდე type(). Why it mattersMost beginner errors come from confusing types: "5" + 5TypeError. When something is unclear, print type().

დეტალიDetail

int დაand float

int — შეზღუდვის გარეშეint — unbounded

big = 2 ** 100
print(big)
# 1267650600228229401496703205376

million = 1_000_000   # ქვედა ტირე / underscore separator
print(million)        # 1000000

float — სიზუსტის ზღვარიfloat — limited precision

print(0.1 + 0.2)
# 0.30000000000000004

print(0.1 + 0.2 == 0.3)   # False
print(round(0.1 + 0.2, 2) == 0.3)  # True
ეს არაა Python-ის ბაგიორობით სისტემაში 0.1 ზუსტად ვერ ჩაიწერება — ისევე, როგორც ათობითში 1/3. ეს ყველა ენას ეხება. ფულთან მუშაობისას იყენებენ decimal მოდულს ან მთელ თეთრებს. This is not a Python bug0.1 cannot be written exactly in binary — just as 1/3 cannot in decimal. Every language has this. For money, use the decimal module or count in whole cents.
ეს დემონსტრაცია REPL-ში აუცილებლად აჩვენე — ყოველთვის აოცებს და კარგად ამახსოვრდებათ. Definitely demo this in the REPL — it always surprises them, and it sticks.

დეტალიDetail

bool — მხოლოდ ორი მნიშვნელობაbool — only two values

is_ready = True
has_error = False

print(True + True)      # 2   — bool is a number!
print(int(False))       # 0

# რომელი მნიშვნელობებია "მცდარი" / which values are falsy?
print(bool(0))          # False
print(bool(""))         # False
print(bool([]))         # False
print(bool(None))       # False
print(bool(0.0))        # False

print(bool(-5))         # True — any non-zero number
print(bool("False"))    # True — a non-empty string!
კლასიკური ხაფანგიbool("False") არის True, რადგან ეს არაცარიელი ტექსტია, არა ლოგიკური მნიშვნელობა. A classic trapbool("False") is True, because that is a non-empty string, not a boolean.

კონვერტაციაConversion

ტიპის შეცვლა (casting)Changing type (casting)

print(int("42") + 8)        # 50
print(float("3.5") * 2)     # 7.0
print(str(100) + " ლარი")   # 100 ლარი

print(int(9.99))            # 9  — truncates, does not round!
print(round(9.99))          # 10 — this rounds

print(int("42abc"))
# ValueError: invalid literal for int() with base 10: '42abc'
int() კვეცავსint(9.99) → 9, int(-9.99) → -9. დამრგვალებისთვის round()-ია. int() truncatesint(9.99) → 9, int(-9.99) → -9. For rounding, use round().
ValueErrorჩნდება, როცა ტექსტი რიცხვად არ იკითხება. დამუშავებას XII კვირაზე ვისწავლით. ValueErrorRaised when the text cannot be read as a number. Handling it comes in week XII.

ოპერაციებიOperations

არითმეტიკული ოპერატორებიArithmetic operators

ოპ.Op. დასახელებაName მაგალითიExample შედეგიResult
+შეკრებაAddition7 + 310
-გამოკლებაSubtraction7 - 34
*გამრავლებაMultiplication7 * 321
/გაყოფაDivision7 / 3 2.333…ყოველთვის floatalways a float
//მთელი გაყოფაFloor division7 // 32
%ნაშთი (modulo)Remainder (modulo)7 % 31
**ახარისხებაExponentiation7 ** 3343
ყურადღება10 / 2 აბრუნებს 5.0-ს (float), არა 5-ს. მთელი შედეგისთვის — 10 // 2. Watch out10 / 2 returns 5.0 (a float), not 5. For a whole result use 10 // 2.

პრაქტიკაPractice

// და % — რისთვის გამოგადგებაWhat // and % are good for

წამების გარდაქმნაConverting seconds

total = 3725   # წამი / seconds

hours = total // 3600
rest = total % 3600
minutes = rest // 60
seconds = rest % 60

print(hours, "სთ", minutes, "წთ", seconds, "წმ")

1 სთ 2 წთ 5 წმ

ლუწი / კენტიEven or odd

n = 14
print(n % 2 == 0)   # True → even

ბოლო ციფრიLast digit

print(1234 % 10)    # 4
print(1234 // 10)   # 123
ეს ორი ოპერატორიგამოცდაზე თითქმის ყოველთვის ხვდება. % — „გაყოფის ნაშთი“, // — „მთელი ნაწილი“. These two operatorsShow up in almost every exam. % is “the remainder”, // is “the whole part”.

წესიRule

ოპერაციების პრიორიტეტიOperator precedence

  1. ( ) — ფრჩხილები
  2. ** — ახარისხება (მარჯვნიდან მარცხნივ)
  3. -x — უნარული მინუსი
  4. * / // % — მარცხნიდან მარჯვნივ
  5. + - — მარცხნიდან მარჯვნივ
  1. ( ) — parentheses
  2. ** — exponentiation (right to left)
  3. -x — unary minus
  4. * / // % — left to right
  5. + - — left to right
print(2 + 3 * 4)        # 14, not 20
print((2 + 3) * 4)      # 20
print(2 ** 3 ** 2)      # 512  (2 ** 9), not 64
print(-2 ** 2)          # -4   (2**2 first, then the minus)
print((-2) ** 2)        # 4
რჩევაეჭვის შემთხვევაში დასვი ფრჩხილები. ისინი უფასოა და კოდს კითხვადს ხდის. AdviceWhen in doubt, add parentheses. They cost nothing and make the code readable.

მოკლე ჩანაწერიShorthand

შემოკლებული მინიჭებაAugmented assignment

count = 10

count = count + 5   # ჩვეულებრივი / the long way
count += 5          # იგივე, მოკლედ / the same, shorter

count -= 3          # count = count - 3
count *= 2          # count = count * 2
count /= 4          # count = count / 4
count //= 2
count %= 7
count **= 2

მრავლობითი მინიჭებაMultiple assignment

x = y = z = 0

a, b = 5, 10
print(a, b)     # 5 10

a, b = b, a     # swap!
print(a, b)     # 10 5
ეს ჩანაწერიa, b = b, a — Python-ის ერთ-ერთი ყველაზე ლამაზი თვისებაა. სხვა ენებში დროებითი ცვლადი დაგჭირდებოდა. This one linea, b = b, a is one of Python’s nicest touches. Other languages need a temporary variable.

შეტანაInput

input() — მონაცემები კლავიატურიდანinput() — data from the keyboard

name = input("სახელი: ")
print("გამარჯობა,", name)

age = input("ასაკი: ")
print(type(age))          # <class 'str'> — always!

print(age + 1)
# TypeError: can only concatenate str (not "int") to str
წესი, რომელიც ერთხელ და სამუდამოდ უნდა დაიმახსოვროinput() ყოველთვის ტექსტს (str) აბრუნებს — მაშინაც კი, როცა მომხმარებელი რიცხვს კრეფს. The one rule to memorise for goodinput() always returns text (str) — even when the user types a number.
age = int(input("ასაკი: "))
print(age + 1)            # now it works

height = float(input("სიმაღლე (მ): "))
print(height * 100, "სმ")
ეს სლაიდი ყველაზე მნიშვნელოვანია ამ ლექციაში. თითქმის ყველა პრაქტიკული დავალება input-ით იწყება და სწორედ აქ იჭედება ნახევარი ჯგუფი. This is the most important slide in the lecture. Nearly every assignment starts with input, and this is exactly where half the group gets stuck.

გამოტანაOutput

print() — უფრო მეტი, ვიდრე გგონიაprint() — more than you think

print("ა", "ბ", "გ")              # ა ბ გ
print("ა", "ბ", sep="-")          # ა-ბ
print("ა", "ბ", sep="")           # აბ

print("ერთი", end=" ")            # no line break
print("ხაზი")                     # ერთი ხაზი

print()                            # empty line
print("-" * 20)                    # --------------------

sep

რა ჩაიდოს არგუმენტებს შორის. ნაგულისხმევი — ერთი ჰარე.

What goes between the arguments. Default: a single space.

end

რით დამთავრდეს. ნაგულისხმევი — ხაზის გადატანა \n.

What it ends with. Default: a newline \n.

გამოტანაOutput

f-სტრიქონი — თანამედროვე ფორმატირებაf-strings — modern formatting

name = "ნინო"
score = 87.6543

# ძველი გზები / the older ways
print("სტუდენტი " + name + ", ქულა " + str(score))
print("სტუდენტი {}, ქულა {}".format(name, score))

# f-სტრიქონი — ყველაზე მოხერხებული / the handiest
print(f"სტუდენტი {name}, ქულა {score}")

# ფორმატირება პირდაპირ შიგნით / formatting inline
print(f"ქულა: {score:.2f}")        # 87.65
print(f"გამოთვლა: {2 + 3 * 4}")    # 14
print(f"{name!r}")                  # 'ნინო'
დაიმახსოვრეf ბრჭყალის წინ, ცვლადი — ფიგურულ ფრჩხილებში. დეტალურად ფორმატირებას VII კვირაზე გავივლით. RememberThe f goes before the quote, the variable inside braces. Formatting in depth comes in week VII.

სრული მაგალითიFull example

ყველაფერი ერთადEverything together

# პროგრამა ითვლის შენაძენის ღირებულებას ფასდაკლებით
# computes the price of a purchase with a discount

print("=== კალკულატორი ===")

item = input("პროდუქტი: ")
price = float(input("ფასი (ლარი): "))
count = int(input("რაოდენობა: "))
discount = float(input("ფასდაკლება (%): "))

total = price * count
saved = total * discount / 100
final = total - saved

print("-" * 30)
print(f"პროდუქტი:    {item}")
print(f"ჯამი:        {total:.2f} ₾")
print(f"დაზოგე:      {saved:.2f} ₾")
print(f"გადასახდელი: {final:.2f} ₾")
სთხოვე სტუდენტებს ეს გადააკეთონ: დაამატონ დღგ 18%, ან ფასდაკლება მხოლოდ 3-ზე მეტი ერთეულის შემთხვევაში (ეს უკვე III კვირის მასალაა — კარგი „კაუჭია“). Ask them to extend it: add 18% VAT, or apply the discount only above 3 units (that is already week III material — a good hook).

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

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

სახლში1) წამების → სთ/წთ/წმ გადამყვანი. 2) წრეწირის სიგრძე და წრის ფართობი რადიუსით. 3) ორნიშნა რიცხვის ციფრების ჯამი (// და %-ით). 4) ტემპერატურა °C → °F. At home1) A seconds → h/m/s converter. 2) Circumference and area of a circle from its radius. 3) Sum of the digits of a two-digit number (with // and %). 4) Temperature °C → °F.
შემდეგი — ლექცია 03შედარებისა და ლოგიკური ოპერატორები, if / elif / else + პრაქტიკული დავალება #1. Next — lecture 03Comparison and logical operators, if / elif / else + practical assignment #1.