ლექცია 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”.
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.
ტიპები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" + 5 → TypeError. როცა გაუგებრობაა — დაბეჭდე type().
Why it mattersMost beginner errors come from confusing types: "5" + 5 → TypeError. 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
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.
დეტალი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().
ოპერაციებიOperations
არითმეტიკული ოპერატორებიArithmetic operators
| ოპ.Op. | დასახელებაName | მაგალითიExample | შედეგიResult |
|---|---|---|---|
+ | შეკრებაAddition | 7 + 3 | 10 |
- | გამოკლებაSubtraction | 7 - 3 | 4 |
* | გამრავლებაMultiplication | 7 * 3 | 21 |
/ | გაყოფაDivision | 7 / 3 |
2.333… — ყოველთვის floatalways a float |
// | მთელი გაყოფაFloor division | 7 // 3 | 2 |
% | ნაშთი (modulo)Remainder (modulo) | 7 % 3 | 1 |
** | ახარისხებაExponentiation | 7 ** 3 | 343 |
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
( )— ფრჩხილები**— ახარისხება (მარჯვნიდან მარცხნივ)-x— უნარული მინუსი* / // %— მარცხნიდან მარჯვნივ+ -— მარცხნიდან მარჯვნივ
( )— parentheses**— exponentiation (right to left)-x— unary minus* / // %— left to right+ -— 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
მოკლე ჩანაწერი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, "სმ")
გამოტანა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} ₾")
შემაჯამებელიWrap-up
რა უნდა დაგამახსოვრდესWhat to remember
- ცვლადს ტიპს არ ვუთითებთ — Python მას მინიჭებისას განსაზღვრავს
- საბაზისო ტიპები:
int · float · str · bool · None, შემოწმება —type() /ყოველთვისfloat-ს აბრუნებს; მთელი გაყოფა —//, ნაშთი —%input()ყოველთვისstr-ს აბრუნებს → შემოახვიეint()ანfloat()- გამოტანისთვის —
f"..."და{value:.2f}
- You never declare a type — Python decides it at assignment
- Basic types:
int · float · str · bool · None; check withtype() /always returns afloat; floor division is//, remainder is%input()always returnsstr→ wrap it inint()orfloat()- For output —
f"..."and{value:.2f}
// და %-ით). 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.
if / elif / else + პრაქტიკული დავალება #1.
Next — lecture 03Comparison and logical operators, if / elif / else + practical assignment #1.