Python Numbers
Numbers (int, float)
ဒါက Python မှာ အသုံးအများဆုံး Data Type တွေထဲက တစ်ခု။ ကိန်းဂဏန်းတွေနဲ့ တွက်ချက်မှုတွေ လုပ်ရင် ဒါတွေကို သေချာ သိထားရမယ်။
1. Integer (int) - ကိန်းပြည့် ပေါ့ကွာ
ဒါကြီးက?
ဒသမ မပါ တဲ့ ကိန်းဂဏန်း အကုန် int ပဲ။ အပေါင်း၊ အနှုတ်၊ သုည။ ရှင်းရှင်းလေး။
integers.py
# Integer ဥပမာတွေ
positive_number = 10
negative_number = -55
zero = 0
large_number = 1000000
print(positive_number) # 10
print(negative_number) # -55
print(zero) # 0
# Type စစ်ကြည့်မယ်
print(type(positive_number)) # <class 'int'>
Pros:
Python မှာ integer တွေက size limit မရှိဘူး။ ဘယ်လောက်ကြီးကြီး ရေးချင်ရေး၊ memory က ခွင့်ပြုသမျှ သုံးလို့ရတယ်။
2. Float - ဒသမကိန်း
ဒါဘာလဲ?
ဒါက ဒသမ ပါတဲ့ ကိန်းတွေပေါ့။
floats.py
# Float ဥပမာတွေ
pi = 3.14
price = 1.25
temperature = -9.8
small_decimal = 0.001
print(pi) # 3.14
print(temperature) # -9.8
# Type စစ်ကြည့်မယ်
print(type(pi)) # <class 'float'>
Gotcha:
Bro 10 လို့ ရေးရင် int။ bro 10.0 လို့ .0 (ဒသမ) ထည့်ရေးလိုက်တာနဲ့ သူက float ဖြစ်သွားပြီ။ ကွဲ တယ်နော်။ တန်ဖိုးက တူတူ ပဲ၊ ဒါပေမယ့် type မတူဘူး။
int_vs_float.py
# အရေးကြီးတဲ့ ကွာခြားချက်
number1 = 10 # ဒါက int
number2 = 10.0 # ဒါက float
print(type(number1)) # <class 'int'>
print(type(number2)) # <class 'float'>
# တန်ဖိုးက တူပေမယ့် type ကွဲတယ်
print(number1 == number2) # True (တန်ဖိုး တူတယ်)
print(type(number1) == type(number2)) # False (type မတူဘူး)3. တွက်ချက်မှုတွေ - Action အပိုင်း
ကဲ Action လာပြီ။ + (ပေါင်း)၊ - (နှုတ်)၊ * (မြှောက်) ကတော့ ရိုးရိုး ပဲ။ ကလေး ကစားတာ။
အခြေခံ တွက်ချက်မှုတွေ:
basic_operations.py
# ပေါင်း (+)
print(5 + 3) # 8
# နှုတ် (-)
print(10 - 4) # 6
# မြှောက် (*)
print(7 * 3) # 21
# စား (/) - အသက် က ဒီမှာ လာပြီ!
print(10 / 2) # 5.0 (float ထွက်တယ်!)
print(10 / 5) # 2.0 (float ပဲ!)
အရေးကြီးတာက / (စားတာ):
ဘရို၊ ဒါ လုံးဝ မမေ့နဲ့။ Python မှာ ရိုးရိုးစားရင် အဖြေက float အမြဲထွက်တယ်။ 10 / 2 ဆိုရင် အဖြေက 5 မဟုတ်ဘူး။ 5.0 (float) ထွက်တယ်။
အထူး Operators တွေ:
special_operations.py
# Floor Division (//) - ဒါက အတိ စားတာ။ ဒသမ ဖြုတ် တာ
print(11 // 3) # 3 (3.666... ထဲက 3 ကိုပဲ ဆွဲ ယူတာ)
print(10 // 3) # 3
print(20 // 6) # 3
# Modulus (%) - ဒါက အကြွင်း
print(11 % 3) # 2 (၁၁ ကို ၃ နဲ့ စားရင် ၃ ခါဝင်ပြီး ၂ ကြွင်းတယ်လေ)
print(10 % 3) # 1
print(20 % 6) # 2
# ထပ်ကိန်း (**) - ဒါက 2³ တို့ တွက်တာ
print(2 ** 3) # 8 (၂ သုံးလုံးမြှောက်)
print(5 ** 2) # 25
print(10 ** 3) # 1000Practical Use Case - စုံ/မ စစ်တာ (Even/Odd):
even_odd_check.py
# Modulus (%) သုံးပြီး စုံ-မ စစ်မယ်
number = 10
if number % 2 == 0:
print(f"{number} က စုံကိန်း") # 10 က စုံကိန်း
else:
print(f"{number} က မကိန်း")
# နောက်ထပ် ဥပမာ
for i in range(1, 6):
if i % 2 == 0:
print(f"{i} = စုံ")
else:
print(f"{i} = မ")
Suggestion:
စုံ-မ စစ်တာတို့၊ အကြိမ်ရေတစ်ခုတိုင်းမှာ တစ်ခါ ဘာလုပ်ချင်တာတို့၊ ပုံမှန် interval တွေ စစ်ချင်တာတို့မှာ အသုံးများတယ်။
Floor Division နဲ့ Modulus ပေါင်း သုံးတာ:
division_examples.py
# ၁၇ ကို ၅ နဲ့ စားမယ်
total = 17
divisor = 5
quotient = total // divisor # ရလဒ်
remainder = total % divisor # အကြွင်း
print(f"{total} ကို {divisor} နဲ့စားရင်:")
print(f"ရလဒ်: {quotient}") # 3
print(f"အကြွင်း: {remainder}") # 2
# Real-world example: ငွေ ခွဲမယ်
money = 100
people = 3
per_person = money // people
leftover = money % people
print(f"လူတစ်ယောက် {per_person} ကျပ်စီ ရမယ်") # 33
print(f"ကျန် {leftover} ကျပ်") # 14. Type ပြောင်းတာ (Casting)
တစ်ခါတစ်လေ float ကို int ပြောင်းချင်တယ်၊ int ကို float ပြောင်းချင်တယ်။
float() - int ကို float ပြောင်းမယ်:
int_to_float.py
# int ကနေ float ပြောင်းတာ လွယ်လွယ်လေး
number = 5
converted = float(number)
print(number) # 5
print(converted) # 5.0
print(type(converted)) # <class 'float'>
# နောက်ထပ် ဥပမာ
print(float(10)) # 10.0
print(float(-7)) # -7.0
print(float(0)) # 0.0int() - float ကို int ပြောင်းမယ်:
float_to_int.py
# float ကနေ int ပြောင်းတာ - သတိထားရမယ်!
number1 = 5.8
converted1 = int(number1)
print(number1) # 5.8
print(converted1) # 5 (ဒသမကို ဖြတ် ပစ်တယ်!)
# နောက်ထပ် ဥပမာတွေ - အားလုံး ဖြတ် ပစ်တယ်
print(int(5.1)) # 5
print(int(5.5)) # 5
print(int(5.9)) # 5
print(int(5.999)) # 5 (၆ မဖြစ်ဘူး!)
# အနုတ် နဲ့လည်း အတူတူပဲ
print(int(-3.7)) # -3 (not -4!)
print(int(-3.2)) # -3
int():
ဘရို၊ int() က round-up မလုပ်ဘူးနော်။ 5.8 က 6 မဖြစ်ဘူး။ သူက ဒသမ နောက်က အကုန်ဖြတ်ပစ်တာ။ အဖြေက 5။ int(5.1) လည်း 5 ပဲ။ int(5.999) လည်း 5 ပဲ။ ဒါ အရေးကြီးတယ်။
Type Conversion Gotchas နဲ့ Tips:
conversion_gotchas.py
# Gotcha 1: int() က truncate လုပ်တာ၊ round မလုပ်ဘူး
print(int(9.9)) # 9 (10 မဟုတ်ဘူး!)
# Round လုပ်ချင်ရင် round() သုံးရမယ်
print(round(9.9)) # 10
print(round(5.5)) # 6
print(round(5.4)) # 5
# Gotcha 2: float/int mixing
result1 = 10 + 5.5 # int + float = float
print(result1) # 15.5
print(type(result1)) # <class 'float'>
result2 = 10 * 2.0 # int * float = float
print(result2) # 20.0 (not 20)
print(type(result2)) # <class 'float'>
# Gotcha 3: Division အမြဲ float ထွက်တယ်
result3 = 10 / 2
print(result3) # 5.0 (not 5)
print(type(result3)) # <class 'float'>
Be careful:
- int() က ဖြတ်တာ၊ round() ကတန်ဖိုးတိုးယူ - မရောနဲ့
- int နဲ့ float တွဲသုံးရင် အဖြေက float ဖြစ်သွားတယ်
- Division (/) က အမြဲ float ပြန်လာတယ်၊ int လိုချင်ရင် // သုံးရမယ်
- ဒသမ တိကျမှု (precision) လိုချင်ရင် float သုံး၊ မလိုဘူးဆိုရင် int သုံး
Real-World Examples:
real_world_examples.py
# Example 1: ကုန်ဈေး တွက်တာ
price = 99.99
quantity = 3
total = price * quantity
print(f"စုစုပေါင်း: {total} ကျပ်") # 299.97 ကျပ်
# Example 2: ပျှမ်းမျှ တွက်တာ
scores = [85, 90, 78, 92]
average = sum(scores) / len(scores)
print(f"ပျှမ်းမျှ: {average}") # 86.25
# Example 3: အခွန် ထည့်တာ
item_price = 1000
tax_rate = 0.05 # 5%
tax = item_price * tax_rate
final_price = item_price + tax
print(f"အခွန်ပါ စုစုပေါင်း: {final_price} ကျပ်") # 1050.0 ကျပ်
အကြံပြု:
Python Numbers ရဲ့ အခြေခံတွေ (int, float, operators, type conversion) ကို သေချာ နားလည်ပြီဆိုရင် bro တွက်ချက်မှုတွေ လုပ်တဲ့အခါ ပြဿနာ မတက်တော့ဘူး။ အထူးသဖြင့် / နဲ့ // ရဲ့ ကွာခြားချက်၊ int() ရဲ့ truncation behavior တွေကို မမေ့နဲ့!