Thuta Learning
Python
BasicProgrammingအခြေခံ

Python Numbers

Pythonသင်ခန်းစာ 825 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • int, float, complex value တွေကို literal ကနေ ခွဲခြားပြီး type() နဲ့ စစ်နိုင်ဖို့
  • / (true division), // (floor division), % (remainder), ** (power) တို့ရဲ့ ရလဒ်ကို ကြိုခန့်မှန်းနိုင်ဖို့
  • 0.1 + 0.2 က 0.3 အတိအကျ မဖြစ်ရတဲ့ အကြောင်းရင်းကို ရှင်းပြပြီး math.isclose() နဲ့ နှိုင်းယှဉ်နိုင်ဖို့
  • ငွေကြေးတွက်ချက်မှုမှာ cents ကို int နဲ့ သိမ်းတာ ဒါမှမဟုတ် decimal.Decimal သုံးတာကို ရွေးချယ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Python မှာ ကိန်းဂဏန်းတွေကို အဓိက အမျိုးအစား သုံးမျိုးနဲ့ သိမ်းပါတယ်။ int က 10၊ -5၊ 1000 လို ကိန်းပြည့်တွေ၊ float က 3.14၊ -0.5 လို ဒသမပါတဲ့ ကိန်းတွေ၊ complex က 2 + 3j လို သင်္ချာနဲ့ engineering မှာ သုံးတဲ့ ကိန်းစုံတွေပါ။ Value ကို ရေးလိုက်တဲ့ ပုံစံကနေ Python က type ကို အလိုအလျောက် ဆုံးဖြတ်ပေးပါတယ် — ဒသမအမှတ် ပါရင် float၊ မပါရင် int ပါ။

Type က အရေးကြီးပါတယ်၊ ဘာလို့လဲဆိုတော့ operator တစ်ခုတည်းက type ပေါ်မူတည်ပြီး ရလဒ်မတူလို့ပါ။ 7 / 2 က 3.5 (float) ရပြီး 7 // 2 က 3 (int) ရပါတယ်။ ဒီ lesson မှာ ဘယ် operator က ဘာ type ပြန်ပေးလဲ၊ float တွေ ဘာကြောင့် အမြဲ တိကျမနေလဲဆိုတာ ကြည့်ပါမယ်။

Problem → Why → Solution

ပြဿနာ: စျေးနှုန်း ပေါင်းတာ မှန်ပေမယ့် == က False ပြန်တယ်

Shop တစ်ခုမှာ 1.10 နဲ့ 2.20 တန် ပစ္စည်းနှစ်ခု ဝယ်ရင် စုစုပေါင်း 3.30 ဖြစ်ရင် လက်ဆောင်ပေးမယ်ဆိုတဲ့ code ကို ရေးလိုက်ပါတယ်။ ဂဏန်းတွေက လက်နဲ့ တွက်ရင် မှန်ပါတယ်၊ ဒါပေမယ့် program က လက်ဆောင် မပေးပါဘူး။ Error message လည်း မရှိပါဘူး — if condition က တိတ်တိတ်လေး False ဖြစ်သွားတာပါ။ Print ထုတ်ကြည့်မှ total က 3.3000000000000003 ဖြစ်နေတာကို တွေ့ရပါတယ်။

gift.py
item_a = 1.10
item_b = 2.20
total = item_a + item_b

if total == 3.30:
    print("Free gift!")
else:
    print("No gift")      # this branch runs

print(total)              # 3.3000000000000003

ဘာကြောင့် ဖြစ်တာလဲ

float တွေကို computer က binary (base 2) နဲ့ သိမ်းပါတယ်။ 1/3 ကို decimal နဲ့ 0.333... လို့ အဆုံးမရှိ ရေးရသလိုပဲ 0.1၊ 1.1၊ 2.2 လို ကိန်းတွေကို binary နဲ့ အတိအကျ ရေးလို့ မရပါဘူး၊ အနီးစပ်ဆုံး value ကိုပဲ သိမ်းပါတယ်။ အဲဒီ အမှားလေးတွေ ပေါင်းလိုက်တော့ == က အတိအကျ တူမတူ စစ်တာမို့ False ပြန်ပါတယ်။ ဒါက Python bug မဟုတ်ပါဘူး၊ IEEE 754 float သုံးတဲ့ language တိုင်းမှာ ဖြစ်ပါတယ်။

Core concept: number type သုံးမျိုးနဲ့ arithmetic operator များ

Python ရဲ့ int မှာ အရွယ်အစား ကန့်သတ်ချက် မရှိပါဘူး။ 2 ** 100 လို ကြီးမားတဲ့ ကိန်းကိုလည်း ဂဏန်းတိုင်း အတိအကျ သိမ်းထားနိုင်ပြီး memory ရှိသလောက် ကြီးလာနိုင်ပါတယ်။ ကိန်းရှည်တွေကို ဖတ်ရလွယ်အောင် 1_000_000 လို underscore ခံပြီး ရေးလို့ရပါတယ်။ float ကတော့ 64-bit IEEE 754 format ကို သုံးလို့ ဂဏန်း ၁၅-၁၇ လုံးလောက်ပဲ တိကျပြီး 0.1 လို decimal value အများစုကို အနီးစပ်ဆုံး value အဖြစ်ပဲ သိမ်းပါတယ်။

Operatorအဓိပ္ပာယ်7 နဲ့ 2 ဆိုရင်
+ပေါင်း7 + 2 → 9
-နုတ်7 - 2 → 5
*မြှောက်7 * 2 → 14
/True division — အမြဲ float ပြန်7 / 2 → 3.5
//Floor division — အောက်ဘက်ကို ဆင်း7 // 2 → 3
%Remainder (modulus) — အကြွင်း7 % 2 → 1
**Power — ထပ်ကိန်း7 ** 2 → 49

/ က int နှစ်ခုကို စားရင်တောင် float ကို အမြဲ ပြန်ပေးပါတယ် — 10 / 2 က 5 မဟုတ်ဘဲ 5.0 ပါ။ ကိန်းပြည့် ရလဒ် လိုချင်ရင် // ကို သုံးရပါမယ်။ // က အနီးဆုံး ကိန်းပြည့်ကို round လုပ်တာ မဟုတ်ဘဲ အောက်ဘက် (floor) ကို ဆင်းတာမို့ -7 // 2 က -3 မဟုတ်ဘဲ -4 ပါ။ % ကလည်း အဲဒီ floor နဲ့ ကိုက်အောင် အလုပ်လုပ်ပြီး ရလဒ်က စားကိန်းရဲ့ sign အတိုင်း ဖြစ်ပါတယ် (-7 % 2 က 1)။ (a // b) * b + a % b က a ကို အမြဲ ပြန်ရပါတယ်။

Type မတူတာတွေ ရောတွက်ရင် Python က ပိုကျယ်တဲ့ type ကို ပြောင်းပေးပါတယ် — int နဲ့ float ပေါင်းရင် float၊ float နဲ့ complex ပေါင်းရင် complex ရပါတယ်။ abs()၊ pow()၊ round() တို့လို built-in function တွေကို import မလုပ်ဘဲ သုံးလို့ရပြီး math module မှာ sqrt()၊ floor()၊ ceil()၊ isclose() တို့ ထပ်ရှိပါတယ်။ ငွေကြေးလို decimal အတိအကျ လိုတဲ့ နေရာမှာ decimal.Decimal ကို၊ 1/3 လို အချိုး အတိအကျ လိုရင် fractions.Fraction ကို သုံးပါတယ်။

Value တစ်ခုရဲ့ type ကို သိချင်ရင် type(value) နဲ့ ကြည့်ပြီး code ထဲမှာ type ပေါ်မူတည်ပြီး ခွဲလုပ်ချင်ရင် isinstance(value, float) ကို သုံးပါတယ်။ ဂဏန်းကြီးတဲ့ float တွေကို 1e3 (1000.0) လို scientific notation နဲ့လည်း ရေးလို့ရပြီး e ပါတာနဲ့ float ဖြစ်သွားပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: operator က type ကို ဆုံးဖြတ်တယ်
  1. Literal ကနေ type ရ (10 → int)
  2. Type ရောရင် float ဘက် ကျယ်သွား
  3. / က float, // နဲ့ % က floor
  4. float ရလဒ်ကို == နဲ့ မစစ်ရ

int က အတိအကျ ဖြစ်ပြီး float က အနီးစပ်ဆုံး value ပါ။ ရလဒ် တိကျဖို့ အရေးကြီးရင် int ဒါမှမဟုတ် Decimal ကို ရွေးပါ။

အထွေထွေ ပုံစံ

python
whole = 42              # int
real = 3.5              # float
cplx = 2 + 3j           # complex

quotient = a / b        # always float
floored = a // b        # rounded down
remainder = a % b       # what is left over
power = base ** exponent

value = round(number, ndigits)

ဒသမ ပါမပါကနေ type ဆုံးဖြတ်ပြီး operator တစ်ခုစီက ရလဒ်မတူပါဘူး။ round() ရဲ့ ဒုတိယ argument က ဒသမနေရာ ဘယ်နှနေရာ ထားမလဲဆိုတာပါ၊ မပေးရင် int ပြန်ပါတယ်။

Example: number type, operator နဲ့ float precision

ဒီ example က type သုံးမျိုးကို ဖန်တီးပြီး type() နဲ့ ပြပါတယ်။ ပြီးရင် 7 နဲ့ 3 ကို operator ခုနစ်ခုလုံးနဲ့ တွက်ပြီး conversion နဲ့ built-in function တွေကို စမ်းပါတယ်။ နောက်ဆုံး အပိုင်းက problem section မှာ တွေ့ခဲ့တဲ့ float precision ကို ကိုယ်တိုင် ပြန်မြင်ရအောင် ထည့်ထားတာပါ။

numbers.py
# ===== 1. Number Types =====
x = 10        # int
y = 3.14      # float
z = 2 + 3j    # complex

print(f"Int: {x}, Type: {type(x)}")
print(f"Float: {y}, Type: {type(y)}")
print(f"Complex: {z}, Type: {type(z)}")

# ===== 2. Arithmetic Operations =====
a = 7
b = 3

print("\n===== Arithmetic Operators =====")
print(f"{a} + {b} = {a + b}")   # Addition
print(f"{a} - {b} = {a - b}")   # Subtraction
print(f"{a} * {b} = {a * b}")   # Multiplication
print(f"{a} / {b} = {a / b}")   # Division (always a float)
print(f"{a} // {b} = {a // b}") # Floor Division
print(f"{a} % {b} = {a % b}")   # Modulus (remainder)
print(f"{a} ** {b} = {a ** b}") # Exponentiation

# ===== 3. Type Conversion =====
print("\n===== Type Conversion =====")
num = 5
print(f"int to float: {float(num)}")   # 5.0
print(f"float to int: {int(3.99)}")     # 3 (cuts off, does not round)

# ===== 4. Useful Functions =====
print("\n===== Useful Functions =====")
print(f"abs(-10) = {abs(-10)}")                    # Absolute value
print(f"pow(2, 4) = {pow(2, 4)}")                  # Power
print(f"round(3.14159, 2) = {round(3.14159, 2)}")  # Rounding

# ===== 5. Float Precision =====
print("\n===== Float Precision =====")
print(f"0.1 + 0.2 = {0.1 + 0.2}")
print(f"0.1 + 0.2 == 0.3 -> {0.1 + 0.2 == 0.3}")

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 4z = 2 + 3j # complex

    Python မှာ imaginary part ကို j နဲ့ ရေးပါတယ် (သင်္ချာမှာ i သုံးသလိုပါ)။ type(z) က <class 'complex'> ပြပါတယ်။

  2. Line 18print(f"{a} / {b} = {a / b}") # Division (always a float)

    7 / 3 က 2.3333333333333335 ပါ — float ရဲ့ နောက်ဆုံး ဂဏန်းမှာ approximation ကြောင့် 5 ပေါ်လာတာကို သတိထားပါ။

  3. Lines 19-20print(f"{a} // {b} = {a // b}") # Floor Division

    7 // 3 က 2 ရပြီး 7 % 3 က 1 ရပါတယ်။ 3 * 2 + 1 က 7 ပြန်ဖြစ်လို့ // နဲ့ % က အတွဲလိုက် အလုပ်လုပ်တာပါ။

  4. Line 27print(f"float to int: {int(3.99)}") # 3 (cuts off, does not round)

    int() က ဒသမ နောက်က အပိုင်းကို ဖြတ်ချပစ်တာပါ၊ round မလုပ်ပါဘူး။ ဒါကြောင့် 3.99 က 4 မဟုတ်ဘဲ 3 ဖြစ်ပါတယ်။

  5. Line 38print(f"0.1 + 0.2 == 0.3 -> {0.1 + 0.2 == 0.3}")

    ပေါင်းလဒ်က 0.30000000000000004 ဖြစ်နေလို့ == က False ပြန်ပါတယ်။ float နှစ်ခုကို နှိုင်းယှဉ်ရင် math.isclose() ကို သုံးပါ။

You should see
Int: 10, Type: <class 'int'>
Float: 3.14, Type: <class 'float'>
Complex: (2+3j), Type: <class 'complex'>

===== Arithmetic Operators =====
7 + 3 = 10
7 - 3 = 4
7 * 3 = 21
7 / 3 = 2.3333333333333335
7 // 3 = 2
7 % 3 = 1
7 ** 3 = 343

===== Type Conversion =====
int to float: 5.0
float to int: 3

===== Useful Functions =====
abs(-10) = 10
pow(2, 4) = 16
round(3.14159, 2) = 3.14

===== Float Precision =====
0.1 + 0.2 = 0.30000000000000004
0.1 + 0.2 == 0.3 -> False

Rounding နဲ့ Decimal

round() က ကျောင်းမှာ သင်ခဲ့တဲ့ "5 ဆို အပေါ်တက်" စည်းကို မသုံးပါဘူး။ တစ်ဝက်တိတိ ဖြစ်နေရင် စုံကိန်းဘက်ကို round လုပ်ပါတယ် (banker's rounding) — round(2.5) က 2 ဖြစ်ပြီး round(3.5) က 4 ပါ။ ဒါ့အပြင် 2.675 လို float က binary မှာ 2.67499999... အဖြစ် သိမ်းထားလို့ round(2.675, 2) က 2.68 မဟုတ်ဘဲ 2.67 ရပါတယ်။

Decimal ကို string ကနေ ဖန်တီးရင် ရေးထားတဲ့ ဂဏန်းအတိုင်း အတိအကျ သိမ်းပြီး quantize() နဲ့ rounding rule ကို ကိုယ်တိုင် ရွေးလို့ ရပါတယ်။ Decimal(0.1) လို float ကနေ ဖန်တီးရင်တော့ float ရဲ့ အမှားပါ ပါလာလို့ string ကို အမြဲ သုံးပါ။

rounding.py
from decimal import Decimal, ROUND_HALF_UP

print(round(2.5), round(3.5))       # 2 4  (halves go to even)
print(round(2.675, 2))              # 2.67 (the float is slightly below 2.675)

price = Decimal("2.675")
print(price.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))  # 2.68
print(Decimal("0.1") + Decimal("0.2") == Decimal("0.3"))        # True

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

E-commerce နဲ့ banking system တွေမှာ ငွေပမာဏကို float နဲ့ မသိမ်းပါဘူး။ နည်းလမ်း နှစ်မျိုး သုံးကြပါတယ် — ပထမက 19.99 ဒေါ်လာကို 1999 cents အဖြစ် int နဲ့ သိမ်းပြီး ပြသတဲ့ အချိန်ကျမှ 100 နဲ့ စားတာ၊ ဒုတိယက Decimal နဲ့ တွက်ပြီး database မှာ DECIMAL/NUMERIC column နဲ့ သိမ်းတာပါ။ ငွေကို လူအများကြား ခွဲဝေတဲ့အခါ // နဲ့ တစ်ယောက်ချင်း ပမာဏ၊ % နဲ့ ကျန်တဲ့ cents ကို တွက်ပါတယ်။

% ကို နေ့စဉ် code မှာ မထင်မှတ်ဘဲ အသုံးများပါတယ် — n % 2 == 0 နဲ့ စုံကိန်း စစ်တာ၊ index % len(items) နဲ့ list ကို ဝိုင်းပတ်သုံးတာ၊ seconds // 60 နဲ့ seconds % 60 နဲ့ မိနစ်နဲ့ စက္ကန့် ခွဲတာတွေပါ။ Science နဲ့ data analysis မှာတော့ float က မြန်ပြီး လုံလောက်အောင် တိကျလို့ အဓိက သုံးပါတယ်၊ ရလဒ်တွေကို နှိုင်းယှဉ်တဲ့အခါ math.isclose() ဒါမှမဟုတ် tolerance တစ်ခုနဲ့ စစ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. float ရလဒ်ကို == နဲ့ စစ်တာ

if total == 3.30: လို condition က တွက်ပြီးသား float အတွက် မယုံရပါဘူး။ math.isclose(total, 3.30) ကို သုံးပါ၊ ဒါမှမဟုတ် ငွေဆိုရင် int cents နဲ့ တွက်ပြီး 330 == 330 လို int အချင်းချင်း နှိုင်းယှဉ်ပါ။

2. / က int ပြန်မယ်လို့ ထင်တာ

items[len(items) / 2] က "list indices must be integers or slices, not float" TypeError ပေးပါတယ်၊ / က 4 / 2 မှာတောင် 2.0 ပြန်လို့ပါ။ Index ဒါမှမဟုတ် အရေအတွက် လိုရင် // ကို သုံးပါ။

3. int() က round လုပ်မယ်လို့ ထင်တာ

int(2.9) က 3 မဟုတ်ဘဲ 2 ပါ၊ int(-2.9) က -2 ပါ — zero ဘက်ကို ဖြတ်ချတာပါ။ အနီးဆုံးကိန်း လိုရင် round() ကို၊ အောက်ဘက် အမြဲ လိုရင် math.floor() ကို သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • ငွေကြေးကို int cents ဒါမှမဟုတ် string ကနေ ဖန်တီးထားတဲ့ Decimal နဲ့ သိမ်းပါ။
  • float တွေကို နှိုင်းယှဉ်ရင် math.isclose() ကို သုံးပါ။
  • ပြသဖို့ပဲ round လုပ်ချင်ရင် f"{value:.2f}" format ကို သုံးပြီး တွက်ချက်မှုထဲက value ကို မပြောင်းပါနဲ့။
  • ကိန်းရှည်တွေကို 1_000_000 လို underscore နဲ့ ရေးပါ။

Mini challenge

Mini challenge: ငွေ ခွဲဝေတဲ့ helper

ငွေ 100.00 ကို လူ သုံးယောက် ခွဲပေးတဲ့ function နဲ့ ငွေကို ဒသမ နှစ်နေရာ round လုပ်တဲ့ function ရေးပါ။ float နဲ့ Decimal ပေါင်းလဒ် ကွာခြားချက်ကိုလည်း print ထုတ်ပြပါ။

  • split_bill(total_cents, people) က (တစ်ယောက်ချင်း cents, ကျန်တဲ့ cents) tuple ပြန်ရမယ်၊ // နဲ့ % သုံးရမယ်
  • to_money(amount) က string ကို Decimal ပြောင်းပြီး ROUND_HALF_UP နဲ့ ဒသမ နှစ်နေရာ round လုပ်ရမယ်
  • 0.10 ကို ဆယ်ကြိမ် float နဲ့ရော Decimal နဲ့ပါ ပေါင်းပြီး ရလဒ်နှစ်ခု print ထုတ်ရမယ်
  • float total ကို == 1.0 နဲ့ရော math.isclose() နဲ့ပါ စစ်ပြရမယ်
money.py
import math
from decimal import Decimal, ROUND_HALF_UP


def split_bill(total_cents: int, people: int) -> tuple[int, int]:
    # TODO: return (cents each person pays, cents left over)
    ...


def to_money(amount: str) -> Decimal:
    # TODO: Decimal(amount) rounded to 0.01 with ROUND_HALF_UP
    ...


# TODO: split 10_000 cents between 3 people and print the share
# TODO: add 0.10 ten times as float and as Decimal, print both totals
# TODO: compare the float total with == 1.0 and with math.isclose()
# TODO: print round(2.675, 2) next to to_money("2.675")

ရလဒ်: Program က အောက်ပါအတိုင်း print ထုတ်ရပါမယ်: Each pays 33.33, 1 cent(s) left over Float total: 0.9999999999999999 Decimal total: 1.00 Float total == 1.0 -> False isclose(float_total, 1.0) -> True round(2.675, 2) = 2.67 to_money('2.675') = 2.68

အရိပ်အမြွက် ကြည့်ရန်

Decimal(amount).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) က ဒသမ နှစ်နေရာ round လုပ်ပေးပါတယ်။ ပေါင်းတဲ့ loop မှာ Python 3.12 ကစပြီး sum() က float တွေကို ပိုတိကျအောင် ပေါင်းပေးလို့ += နဲ့ ကိုယ်တိုင် ပေါင်းမှ အမှားကို မြင်ရပါမယ်။

အဖြေ ကြည့်ရန်
money.py
import math
from decimal import Decimal, ROUND_HALF_UP


def split_bill(total_cents: int, people: int) -> tuple[int, int]:
    """Return (cents each person pays, cents left over)."""
    return total_cents // people, total_cents % people


def to_money(amount: str) -> Decimal:
    """Round a decimal string to 2 places; halves round up."""
    return Decimal(amount).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)


each, left = split_bill(10_000, 3)
print(f"Each pays {each / 100:.2f}, {left} cent(s) left over")

float_total = 0.0
decimal_total = Decimal("0")
for _ in range(10):
    float_total += 0.10
    decimal_total += Decimal("0.10")
print(f"Float total: {float_total}")
print(f"Decimal total: {decimal_total}")
print(f"Float total == 1.0 -> {float_total == 1.0}")
print(f"isclose(float_total, 1.0) -> {math.isclose(float_total, 1.0)}")

print(f"round(2.675, 2) = {round(2.675, 2)}")
print(f"to_money('2.675') = {to_money('2.675')}")

Quick Quiz 1 / 3

print(-7 // 2, -7 % 2) က ဘာ ထုတ်မလဲ။

Quick Quiz 2 / 3

ငွေ 19.99 ကို သိမ်းဖို့ ဘယ်နည်းက အသင့်တော်ဆုံးလဲ။

Quick Quiz 3 / 3

items = ["a", "b", "c", "d"] ဖြစ်ရင် items[len(items) / 2] မှာ ဘာဖြစ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • int က အရွယ်အစား ကန့်သတ်ချက်မရှိဘဲ အတိအကျ ဖြစ်ပြီး float က binary approximation ဖြစ်ပါတယ်။
  • / က float အမြဲ ပြန်ပြီး // နဲ့ % က floor အပေါ် အခြေခံပါတယ်၊ ** က ထပ်ကိန်းပါ။
  • float ရလဒ်တွေကို == နဲ့ မစစ်ဘဲ math.isclose() ကို သုံးပါ။
  • ငွေကြေးအတွက် int cents ဒါမှမဟုတ် string ကနေ ဖန်တီးတဲ့ Decimal ကို သုံးပါ။

The Python Tutorial — Python Software Foundation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Python Numbers | Thuta Learning