ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Python ကို Guido van Rossum က ဖန်တီးခဲ့ပြီး ပထမဆုံး version ကို 1991 မှာ ထုတ်ခဲ့ပါတယ်။ သူ့ရည်မှန်းချက်က code ကို ရေးရတာထက် ဖတ်ရတာ ပိုလွယ်အောင် လုပ်ဖို့ပါ — programmer တွေဟာ code ကို ရေးတာထက် ဖတ်တာ အများကြီး ပိုလုပ်ရလို့ပါ။
"High-level" ဆိုတာ memory ကို ကိုယ်တိုင် စီမံတာ၊ CPU instruction တွေကို စဉ်းစားတာ မလိုဘဲ ပြဿနာကိုယ်တိုင်ကိုပဲ အာရုံစိုက်လို့ရတာကို ဆိုလိုပါတယ်။ ဥပမာ C မှာ စာကြောင်းတစ်ကြောင်း ဖတ်ဖို့ buffer အရွယ်အစား စဉ်းစားရပေမယ့် Python မှာတော့ line = input() လို့ ရေးရုံပါပဲ။
Problem → Why → Solution
ပြဿနာ: 3.10 က 3.1 ဖြစ်သွားတယ်
Python version နံပါတ်ကို report တစ်ခုမှာ ပြချင်လို့ version = 3.10 လို့ ရေးလိုက်ပါတယ်။ Run လိုက်တော့ "Python 3.1" လို့ ထွက်လာပါတယ် — Python 3.1 ဆိုတာ 2009 ကထွက်ခဲ့တဲ့ version ဟောင်းကြီးပါ။ Error တစ်ခုမှ မတက်ဘဲ data မှားသွားတာမို့ ဒီလို bug က ရှာရခက်ပါတယ်။
version = 3.10 # looks like a version, but it is a float
print(f"Running on Python {version}") # Running on Python 3.1ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Quote မပါတဲ့ 3.10 ကို Python က ဒသမကိန်း (float) အဖြစ် ဖတ်ပါတယ်၊ သင်္ချာမှာ 3.10 နဲ့ 3.1 က တန်ဖိုး အတူတူမို့ နောက်ဆုံးက သုည ပျောက်သွားပါတယ်။ Python မှာ variable နာမည်က type မသတ်မှတ်ပါဘူး — ရေးလိုက်တဲ့ value ကပဲ type ကို ဆုံးဖြတ်ပါတယ်။ Version လို စာသားအတိုင်း ထိန်းထားရမယ့် data ကို "3.10" လို string အဖြစ် သိမ်းရပါမယ်။
Core concept: Python program တစ်ခု run ပုံ
python hello.py လို့ ရိုက်လိုက်တဲ့အခါ အလုပ်နှစ်ဆင့် ဖြစ်ပါတယ်။ ပထမဆင့်မှာ interpreter က file တစ်ခုလုံးကို ဖတ်ပြီး bytecode လို့ခေါ်တဲ့ ရိုးရှင်းတဲ့ instruction တွေအဖြစ် ပြောင်းပါတယ်။ ဒီအဆင့်မှာ grammar မှားတာ (ကွင်းပိတ် ကျန်တာ၊ colon ကျန်တာ) ကို SyntaxError အဖြစ် ဖမ်းမိလို့ line တစ်ကြောင်းမှ မ run ရသေးပါဘူး။ ဒုတိယဆင့်မှာ Python virtual machine က bytecode ကို တစ်ခုချင်း run ပါတယ်။ မရှိတဲ့ နာမည်သုံးတာလို ပြဿနာတွေကတော့ ဒီ run နေတဲ့အချိန်ကျမှ ပေါ်ပါတယ်။
အသုံးအများဆုံး interpreter က python.org က ထုတ်တဲ့ CPython ပါ။ Import လုပ်တဲ့ module တွေရဲ့ bytecode ကို __pycache__ folder ထဲမှာ .pyc file အဖြစ် သိမ်းထားလို့ နောက်တစ်ခါ မြန်မြန် load လုပ်နိုင်ပါတယ်။ ဒီ folder ကို ကိုယ်တိုင် ပြင်စရာ မလိုသလို git ထဲလည်း မထည့်ရပါဘူး။
Python က dynamically typed language ပါ။ age = 25 ဆိုရင် age ဆိုတဲ့ နာမည်က int object တစ်ခုကို ညွှန်ပြနေတာပါ၊ နောက်မှ age = "twenty" လို့ ပြန်ရေးရင် အဲဒီ နာမည်က str object ကို ညွှန်သွားပါတယ်။ Type က value မှာ ကပ်ပါတယ်၊ နာမည်မှာ မကပ်ပါဘူး။ ဒါပေမယ့် type မတူတဲ့ value တွေကို ရောသုံးတာကိုတော့ Python က ခွင့်မပြုပါဘူး — "Age: " + 25 က TypeError ပေးပါတယ်၊ JavaScript လို အလိုအလျောက် string ပြောင်းမပေးပါဘူး။ ဒါကြောင့် Python ကို dynamic ဖြစ်ပေမယ့် strongly typed လို့ ပြောကြပါတယ်။
Block တွေကို {} မသုံးဘဲ indentation (ရှေ့က space) နဲ့ ခွဲတာက Python ရဲ့ ထူးခြားချက်ပါ။ Standard library ကြီးကြီး ပါလာတာကိုလည်း "batteries included" လို့ ခေါ်ကြပါတယ်။ Python 3.0 ကို 2008 မှာ ထုတ်ခဲ့ပြီး Python 2 နဲ့ ပြည့်ပြည့်ဝဝ ကိုက်ညီမှု မရှိပါဘူး၊ Python 2 ကိုတော့ 2020 ကစပြီး support ရပ်လိုက်ပါပြီ။
ဒီလို ပြောင်းလွယ်တာရဲ့ ဈေးက အမြန်နှုန်းပါ။ Instruction တစ်ခုချင်းကို virtual machine က run ရလို့ ဂဏန်း သန်းချီ loop ပတ်တဲ့ pure Python code က C ထက် အများကြီး နှေးပါတယ်။ လက်တွေ့မှာတော့ NumPy လို library တွေက အလေးဆုံး တွက်ချက်မှုတွေကို compile လုပ်ထားတဲ့ code ထဲမှာ လုပ်ပေးလို့ Python က program ကို ညွှန်ကြားတဲ့ အလွှာအဖြစ်ပဲ ကျန်ပြီး အမြန်နှုန်း ပြဿနာ သိပ်မရှိတော့ပါဘူး။
- hello.py (source code)
- Bytecode ပြောင်း: SyntaxError စစ်
- Virtual machine က run
- Output သို့မဟုတ် runtime error
SyntaxError ဆို ဘာ output မှ မထွက်ပါဘူး၊ NameError နဲ့ TypeError ကတော့ အဲဒီ line ကို ရောက်မှ တက်ပါတယ်။
အထွေထွေ ပုံစံ: value, type နဲ့ block
name = "text" # str
count = 10 # int
ratio = 0.5 # float
flag = True # bool
for item in collection:
print(item) # indented = inside the loopVariable ကို ကြေညာစရာ မလိုဘဲ = နဲ့ value ပေးလိုက်တာနဲ့ ဖြစ်ပါတယ်၊ type ကို value ကနေ သိပါတယ်။ Colon (:) နဲ့ ဆုံးတဲ့ line နောက်က indent လုပ်ထားတဲ့ line တွေက block တစ်ခုပါ။
Example: Python ရဲ့ ထူးခြားချက်တွေကို run ကြည့်ခြင်း
ဒီ program က intro.py အဖြစ် save ပြီး run လို့ရပါတယ်။ Version ကို string အဖြစ် သိမ်းထားတာ၊ float ဖြစ်သွားရင် ဘာဖြစ်လဲ နှိုင်းယှဉ်ပြထားတာ၊ type() နဲ့ value တစ်ခုစီရဲ့ type ကို မေးကြည့်တာနဲ့ indentation နဲ့ ခွဲထားတဲ့ for loop တစ်ခုကို သတိထားကြည့်ပါ။
print("Hello, World!")
name = "Python"
version = "3.13" # a string keeps the text exactly as written
is_readable = True
print(f"{name} {version} is readable: {is_readable}")
print(3.10, "vs", "3.10") # the float loses its trailing zero
print(type(version).__name__, type(3.10).__name__)
features = ["Easy to read", "Runs on Windows, macOS, Linux", "Large standard library"]
for feature in features:
print(f"- {feature}")Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 4
version = "3.13" # a string keeps the text exactly as writtenQuote ထဲမှာ ရေးထားလို့ str ဖြစ်ပြီး ရေးထားတဲ့ စာလုံးတွေ အတိုင်း ထိန်းထားပါတယ်။ Quote မပါဘဲ version = 3.11 လို့ ရေးရင် float ဖြစ်သွားပြီး 3.10 လို version က 3.1 ဖြစ်သွားပါလိမ့်မယ်။
- Line 7
print(f"{name} {version} is readable: {is_readable}")f-string က {} ထဲက variable တွေကို value နဲ့ အစားထိုးပါတယ်။ True က bool value ဖြစ်ပေမယ့် f-string ထဲမှာ စာသား True အဖြစ် ထွက်ပါတယ်။
- Line 8
print(3.10, "vs", "3.10") # the float loses its trailing zeroFloat 3.10 က 3.1 အဖြစ် ထွက်ပြီး string "3.10" ကတော့ မပြောင်းပါဘူး။ ပြဿနာ section က bug ကို တိုက်ရိုက် မြင်ရပါတယ်။
- Line 9
print(type(version).__name__, type(3.10).__name__)type() က value ရဲ့ type ကို ပြန်ပေးပြီး .__name__ က အဲဒီ type ရဲ့ နာမည်တိုကို ယူပါတယ်၊ str နဲ့ float ထွက်ပါတယ်။ type(version) ကို တိုက်ရိုက် print ရင်တော့ <class 'str'> လို့ ထွက်ပါတယ်။
- Lines 12-13
for feature in features:for loop က list ထဲက item တစ်ခုချင်းကို feature ထဲ ထည့်ပြီး indent လုပ်ထားတဲ့ print() ကို item တစ်ခုစီအတွက် တစ်ကြိမ် run ပါတယ်။
Hello, World!
Python 3.13 is readable: True
3.1 vs 3.10
str float
- Easy to read
- Runs on Windows, macOS, Linux
- Large standard libraryDynamic ဖြစ်ပေမယ့် strongly typed
အောက်က code မှာ score ဆိုတဲ့ နာမည်တစ်ခုတည်းက int ကနေ str ကို ပြောင်းသွားတာ ခွင့်ပြုပါတယ်။ ဒါပေမယ့် str နဲ့ int ကို + နဲ့ ပေါင်းဖို့ ကြိုးစားတော့ TypeError ရပါတယ်။ str() နဲ့ ပြောင်းပေးတာ ဒါမှမဟုတ် f-string သုံးတာက မှန်ကန်တဲ့ နည်းပါ။
score = 90
print(type(score).__name__) # int
score = "ninety"
print(type(score).__name__) # str
age = 25
try:
print("Age: " + age)
except TypeError as err:
print("TypeError:", err)
print("Age: " + str(age))
print(f"Age: {age}")Python 2 code ကို ဘယ်လို မှတ်မိမလဲ
Stack Overflow အဖြေဟောင်းတွေ၊ စာအုပ်ဟောင်းတွေမှာ Python 2 code တွေ အများကြီး ကျန်နေသေးပါတယ်။ Python 3 မှာ copy ကူးပြီး run ရင် error တက်တာ ဒါမှမဟုတ် ပိုဆိုးတာက အဖြေမှား ထွက်တာ ဖြစ်နိုင်ပါတယ်။ အောက်က ဇယားမှာ အတွေ့ရအများဆုံး ကွာခြားချက်တွေကို ပြထားပါတယ်၊ ဘယ်ဘက် ကော်လံက ပုံစံကို တွေ့ရင် အဲဒီ code က ခေတ်မမီတော့ဘူးလို့ မှတ်ပါ။
| Python 2 | Python 3 |
|---|---|
| print "hi" (statement) | print("hi") (function) |
| 7 / 2 က 3 ရတယ် | 7 / 2 က 3.5 ရပြီး 7 // 2 က 3 ရတယ် |
| raw_input() နဲ့ စာဖတ်တယ် | input() နဲ့ စာဖတ်တယ် |
| Unicode စာအတွက် u"..." လိုတယ် | String တိုင်း Unicode ဖြစ်ပြီးသား |
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Team တွေက Python ကို ရွေးကြတဲ့ အဓိက အကြောင်းရင်းက ဖတ်ရလွယ်လို့ပါ။ Code review လုပ်တဲ့အခါ၊ developer အသစ် ဝင်လာတဲ့အခါ code ကို မြန်မြန် နားလည်နိုင်ပါတယ်။ Standard library ကလည်း file, JSON, date, CSV, HTTP, SQLite တွေအတွက် module တွေ ပါပြီးသားမို့ automation script တစ်ခုကို package တစ်ခုမှ install မလုပ်ဘဲ ရေးနိုင်တာ မကြာခဏ ဖြစ်ပါတယ်။
Dynamic typing ရဲ့ ဈေးကတော့ type မှားတာကို run မှ သိရတာပါ။ Project ကြီးတွေမှာ ဒါကို type hint (def total(price: float) -> float) နဲ့ mypy လို checker tool တွေ၊ unit test တွေနဲ့ ဖြည့်ကြပါတယ်။ Server တွေ deploy လုပ်တဲ့အခါလည်း development machine နဲ့ production မှာ Python version တူအောင် ထားပါတယ်၊ version ကို python --version နဲ့ စစ်ပြီး project document ထဲမှာ မှတ်ထားတာ ကောင်းပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Python ကို "type မရှိဘူး" လို့ ထင်တာ
Type ကို ကြေညာစရာ မလိုတာက type မရှိတာ မဟုတ်ပါဘူး။ "5" + 5 က TypeError ပေးပြီး "5" * 3 က "555" ပြန်ပေးပါတယ်။ Operator တစ်ခုရဲ့ အလုပ်က value ရဲ့ type ပေါ် မူတည်လို့ type() နဲ့ မသေချာတဲ့ value တွေကို စစ်ကြည့်ပါ။
2. Python language နဲ့ library ကို ရောတာ
pandas.read_csv() ဒါမှမဟုတ် Django ရဲ့ models.Model က Python ရဲ့ အစိတ်အပိုင်း မဟုတ်ပါဘူး၊ သီးသန့် install လုပ်ပြီး version သီးသန့်ရှိတဲ့ package တွေပါ။ Error တက်ရင် documentation ရှာတဲ့အခါ package နာမည်နဲ့ version ကို ထည့်ရှာပါ။
3. Version ကို float အဖြစ် သိမ်းတာ
3.10 က 3.1 ဖြစ်သွားပြီး 3.9 < 3.10 ကို float နဲ့ နှိုင်းယှဉ်ရင် False ရပါတယ်။ Version ကို "3.10" လို string သို့မဟုတ် (3, 10) လို tuple နဲ့ သိမ်းပါ၊ running Python ရဲ့ version ကိုတော့ sys.version_info ကနေ ယူပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Tutorial တစ်ခုကို မလိုက်ခင် Python 3 အတွက် ရေးထားလား (print() ကို ကွင်းနဲ့ ရေးထားလား) အရင် စစ်ပါ။
- Value တစ်ခုရဲ့ type ကို မသေချာရင် ခန့်မှန်းမနေဘဲ print(type(value)) နဲ့ မေးကြည့်ပါ။
- စာသားနဲ့ ဂဏန်း ရောပြီး print ချင်ရင် + အစား f-string ကို သုံးပါ၊ type ပြောင်းစရာ မလိုတော့ပါဘူး။
- Project တိုင်းမှာ သုံးထားတဲ့ Python version ကို README ထဲမှာ ရေးမှတ်ထားပါ။
Mini challenge
Mini challenge: language fact card
Python အကြောင်း fact card တစ်ခု print ထုတ်တဲ့ program ရေးပါ။ Value တွေကို မှန်ကန်တဲ့ type နဲ့ သိမ်းပြီး တွက်ချက်မှု တစ်ခု ထည့်ပါ။
- Language နာမည် (str)၊ ပထမ release နှစ် 1991 (int) နဲ့ ယခုနှစ် (int) ကို သိမ်းရမယ်
- Version "3.13" ကို string အဖြစ် သိမ်းရမယ်
- Python ရှိခဲ့တဲ့ နှစ်အရေအတွက်ကို တွက်ရမယ်
- Use case သုံးခုရှိတဲ့ list ကို for loop နဲ့ print ထုတ်ရမယ်
language = "Python"
# TODO: first_release (int) and this_year (int)
# TODO: version as a string, so "3.10"-style values stay intact
# TODO: a list with three things Python is used for
# TODO: print "<language> <version>"
# TODO: print how many years have passed since the first release
# TODO: loop over the list and print each use case with "- " in frontရလဒ်: this_year = 2026 ဆိုရင်: Python 3.13 First released 35 years ago, in 1991. Used for: - web back ends - data analysis - automation scripts
အရိပ်အမြွက် ကြည့်ရန်
နှစ်အရေအတွက်က this_year - first_release ပါ။ Loop body ကို space လေးခု indent လုပ်ဖို့ မမေ့ပါနဲ့။
အဖြေ ကြည့်ရန်
language = "Python"
first_release = 1991
this_year = 2026
version = "3.13"
uses = ["web back ends", "data analysis", "automation scripts"]
print(f"{language} {version}")
age = this_year - first_release
print(f"First released {age} years ago, in {first_release}.")
print("Used for:")
for use in uses:
print(f"- {use}")Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Python ကို Guido van Rossum က 1991 မှာ ထုတ်ခဲ့ပြီး ဒီနေ့ Python 3 ကိုပဲ သုံးပါတယ်။
- Interpreter က source ကို bytecode ပြောင်းပြီး virtual machine က run ပါတယ် — SyntaxError က run မစခင်၊ NameError/TypeError က run နေစဉ် တက်ပါတယ်။
- Type က value မှာ ကပ်ပြီး နာမည်မှာ မကပ်ပါဘူး၊ ဒါပေမယ့် type မတူတာတွေကို အလိုအလျောက် မရောပါဘူး။
- Version လို စာသားအတိုင်း ထိန်းရမယ့် data ကို float မဟုတ်ဘဲ string နဲ့ သိမ်းပါ။
The Python Tutorial — Python Software Foundation