Variables နှင့် Data Types
Variables ဆိုတာ ဘာလဲ?
Section titled “Variables ဆိုတာ ဘာလဲ?”Variable ဆိုတာ Data တွေကို သိမ်းထားပေးတဲ့ “သေတ္တာလေး” တွေနဲ့ တူပါတယ်။ သေတ္တာပေါ်မှာ နာမည်တပ်ထားပြီး အထဲမှာ ကိုယ်သိမ်းချင်တဲ့ တန်ဖိုးကို ထည့်ထားလို့ ရပါတယ်။
name = "Aung Aung" # name ဆိုတဲ့ သေတ္တာထဲ "Aung Aung" ထည့်age = 20 # age ဆိုတဲ့ သေတ္တာထဲ 20 ထည့်Variable နာမည်ပေးတဲ့ စည်းကမ်းချက်များ:
# ✅ မှန်ကန်သည်student_name = "Su Su"age2 = 25_private = True
# ❌ မမှန်ကန် (Error တက်မည်)2name = "Aung" # ဂဏန်းနဲ့ မစနိုင်my-name = "Aung" # (-) သင်္ကေတ မသုံးရclass = "first" # Python Keyword မသုံးရData Types (ဒေတာ အမျိုးအစားများ)
Section titled “Data Types (ဒေတာ အမျိုးအစားများ)”Python မှာ အဓိက Data Type ၅ မျိုး ရှိပါတယ်:
၁. String (စာသား)
Section titled “၁. String (စာသား)”စာသားတွေကို သိမ်းဖို့ သုံးပါတယ်။ "" (Double Quote) ဒါမှမဟုတ် '' (Single Quote) ထဲမှာ ရေးပေးရပါတယ်:
name = "Aung Aung"city = 'ရန်ကုန်'message = "မင်္ဂလာပါ! ဒါ Python ပါ။"
print(type(name)) # <class 'str'>print(len(name)) # 9 (စာလုံး အရေအတွက်)၂. Integer (ကိန်းပြည့်)
Section titled “၂. Integer (ကိန်းပြည့်)”ဒသမဂဏန်း မပါတဲ့ ကိန်းဂဏန်းတွေပါ:
age = 20year = 2024negative = -5
print(type(age)) # <class 'int'>၃. Float (ဒသမကိန်း)
Section titled “၃. Float (ဒသမကိန်း)”ဒသမဂဏန်း ပါတဲ့ ကိန်းဂဏန်းတွေပါ:
price = 99.99pi = 3.14159temperature = -2.5
print(type(price)) # <class 'float'>၄. Boolean
Section titled “၄. Boolean”True (မှန်တယ်) ဒါမှမဟုတ် False (မှားတယ်) ဆိုတဲ့ တန်ဖိုး နှစ်ခုပဲ ရှိပါတယ်:
is_student = Trueis_raining = False
print(type(is_student)) # <class 'bool'>၅. None
Section titled “၅. None”None ဆိုတာ “တန်ဖိုး မရှိသေးဘူး” လို့ အဓိပ္ပာယ်ရပါတယ်။ Variable တစ်ခုကို တည်ဆောက်ထားပြီး ဘာမှ မထည့်ဘဲ နောက်မှ ထည့်ဖို့ Plan ရှိတဲ့အခါမျိုးမှာ သုံးလို့ရပါတယ်။
result = None # မသိသေးဘူးuser_name = None # User က login မဝင်ရသေးဘူး
print(result) # Noneprint(type(result)) # <class 'NoneType'>
# None စစ်ဆေးနည်းif result is None: print("ရလဒ် မရသေးဘူး")type() — Data Type စစ်ဆေးနည်း
Section titled “type() — Data Type စစ်ဆေးနည်း”ဘယ် Variable ကိုမဆို type() Function နဲ့ စစ်ဆေးကြည့်လို့ ရပါတယ်:
name = "Aung Aung"age = 20price = 99.99active = Trueempty = None
print(type(name)) # <class 'str'>print(type(age)) # <class 'int'>print(type(price)) # <class 'float'>print(type(active)) # <class 'bool'>print(type(empty)) # <class 'NoneType'>Type Conversion — Type ပြောင်းလဲခြင်း
Section titled “Type Conversion — Type ပြောင်းလဲခြင်း”Type တစ်ခုကနေ တစ်ခုကို ပြောင်းလဲနိုင်ပါတယ်:
# String → Integerage_str = "20"age_int = int(age_str)print(age_int + 1) # 21 (String ဆိုရင် + သည် ပေါင်းလဒ် မဟုတ်ဘဲ ချိတ်ဆက်သွားပါမယ်)
# Integer → Stringscore = 95message = "Score: " + str(score)print(message) # Score: 95
# String → Floatprice_str = "99.99"price = float(price_str)print(price * 2) # 199.98
# Integer → Floatnum = 5print(float(num)) # 5.0
# Boolean conversionprint(bool(0)) # Falseprint(bool(1)) # Trueprint(bool("")) # False (Empty String)print(bool("hello")) # TrueOperators
Section titled “Operators”Arithmetic Operators (ပေါင်းနှုတ်မြှောက်စား)
Section titled “Arithmetic Operators (ပေါင်းနှုတ်မြှောက်စား)”a = 10b = 3
print(a + b) # 13 (ပေါင်း)print(a - b) # 7 (နှုတ်)print(a * b) # 30 (မြှောက်)print(a / b) # 3.333... (စား — Float ပြန်ပေးပါမယ်)print(a // b) # 3 (ကိန်းပြည့်ဖြင့် စား — ဒသမ ဖြတ်)print(a % b) # 1 (Mod — ကြွင်းရလဒ်)print(a ** b) # 1000 (a ကို b ထပ် — 10³)Comparison Operators (နှိုင်းယှဉ်စစ်ဆေးခြင်း)
Section titled “Comparison Operators (နှိုင်းယှဉ်စစ်ဆေးခြင်း)”နှိုင်းယှဉ်တဲ့ Operators တွေက စစ်ဆေးပြီး True ဒါမှမဟုတ် False ပြန်ပေးပါတယ်:
x = 10y = 5
print(x == y) # False (တူသလား?)print(x != y) # True (မတူဘူးလား?)print(x > y) # True (ကြီးသလား?)print(x < y) # False (ငယ်သလား?)print(x >= y) # True (ကြီးတယ် သို့မဟုတ် တူသလား?)print(x <= y) # False (ငယ်တယ် သို့မဟုတ် တူသလား?)f-strings — ရှင်းလင်းတဲ့ Text Formatting
Section titled “f-strings — ရှင်းလင်းတဲ့ Text Formatting”name = "Su Su"age = 22score = 95.678
# f"..." ပုံစံနဲ့ Variable ကို {} ထဲ ထည့်ပါprint(f"မင်္ဂလာပါ {name}! ခင်ဗျားအသက်က {age} နှစ် ရှိပါပြီ")
# Expression တွေကိုလည်း တိုက်ရိုက် ထည့်နိုင်ပါတယ်print(f"Score: {score:.2f}") # Score: 95.68 (ဒသမ ၂ ထပ်)print(f"Next year: {age + 1}") # Next year: 23
# ဒါကို သုံးနိုင်ပါပြီ — ဒီ Course ထဲမှာ f-string ကိုသာ အဓိက သုံးသွားပါမယ်Multiple Assignment
Section titled “Multiple Assignment”# Variable တစ်ချက်တည်း — ဂိမ်းအမှတ်တွေကို ကနဦး 0 သတ်မှတ်ခြင်းmorning = afternoon = evening = 0print(morning, afternoon, evening) # 0 0 0
# Tuple Unpackingname, age, city = "Aung", 20, "ရန်ကုန်"print(name) # Aungprint(age) # 20print(city) # ရန်ကုန်
# နှစ်ယောက်ရဲ့ Score ကို လဲလှယ်ခြင်းmg_mg_score = 85su_su_score = 92mg_mg_score, su_su_score = su_su_score, mg_mg_scoreprint(mg_mg_score, su_su_score) # 92 85ဖြစ်တတ်တဲ့ အမှားများ
Section titled “ဖြစ်တတ်တဲ့ အမှားများ”# ၁. Type မစစ်ဆေးဘဲ ပေါင်းခြင်းage = "20" # String!# print(age + 1) # TypeError!print(int(age) + 1) # ✅ 21
# ၂. None ကို Math တွက်ချက်ခြင်းresult = None# print(result + 1) # TypeError: unsupported operandif result is not None: print(result + 1)
# ၃. == နဲ့ = ကို ရောသုံးခြင်းx = 5 # ← Assign (တန်ဖိုးထည့်သည်)x == 5 # ← Compare (စစ်ဆေးသည်)