Skip to content

Chapter 1 Introduction To Python

CHAPTER 1

Introduction To Python

Keynote

1.1 What is Python? (Python ဆိုတာ ဘာလဲ)

  • Python ဆိုတာက Guido Van Rossum ဖန်တီးခဲ့တဲ့ High-level programming language တစ်ခု ဖြစ်ပါတယ်။ ဖန်တီးရှင် Guido ကိုတော့ ချစ်စနိုးနဲ့ Benevolent Dictator For Life လို့ တင်စားခေါ်ဝေါ်ကြပါတယ်။
  • Python ကို ၁၉၉၁ ခုနှစ်မှာ စတင်မိတ်ဆက်ခဲ့တာပါ။ အခုဆိုရင် Windows နဲ့ Linux အပါအဝင် Operating Systems တော်တော်များများမှာ Python interpreters တွေကို အသုံးပြုနိုင်နေပါပြီ။
  • Python ရေးတဲ့ Programmers တွေကိုတော့ Pythonists (သို့မဟုတ်) Pythonistas လို့ ခေါ်လေ့ရှိကြပါတယ်။

1.2 Reasons for Popularity (ဘာလို့ ဒီလောက် ခေတ်စားလာတာလဲ)

Python ဒီလောက်ထိ လူကြိုက်များပြီး ခေတ်စားလာရတဲ့ အကြောင်းရင်းတွေ အများကြီးရှိပါတယ်။ တချို့အချက်တွေကတော့ -

(a) Free (အခမဲ့ရတယ်):

  • Python က သုံးရတာ Free ဖြစ်သလို ဖြန့်ဝေရတာလည်း လွတ်လပ်တယ်။ ပြီးတော့ Community support လည်း အရမ်းကောင်းတယ်။
  • Python interpreter ကို အဓိက Platform တိုင်းအတွက် ရနိုင်ပါတယ်။

(b) Software quality (Software အရည်အသွေး):

  • ရိုးရိုး Scripting languages တွေနဲ့ တခြားရိုးရာ Languages တွေထက် ပိုကောင်းတယ်။
  • Code က ဖတ်ရလွယ်တဲ့အတွက် ပြန်သုံးရတာ (Reusable)၊ ပြင်ဆင်ထိန်းသိမ်းရတာ (Maintainable) အရမ်းအဆင်ပြေတယ်။
  • အဆင့်မြင့်တဲ့ Reuse mechanisms တွေကိုလည်း Support လုပ်ပေးတယ်။

© Developer productivity (ရေးသားသူအတွက် တွင်ကျယ်မှု):

  • Statically typed languages တွေထက်စာရင် အများကြီး ပိုပြီး အလုပ်တွင်တယ်။
  • Code ရေးရတာ အများကြီး ပိုတိုတယ်။
  • စာရိုက်ရတာ သက်သာမယ်၊ Debug လုပ်ရတာနဲ့ Maintain လုပ်ရတာ ပိုလွယ်တယ်။
  • Compile လုပ်တာတို့၊ Link ချိတ်တာတို့လို ကြာတဲ့အဆင့်တွေ လုပ်နေစရာ မလိုတော့ဘူး။

(d) Program portability (နေရာစုံမှာ အလုပ်လုပ်နိုင်မှု):

  • Python Programs တွေက Platform တော်တော်များများမှာ ဘာမှ ပြင်စရာမလိုဘဲ ဒီတိုင်း Run လို့ရတယ်။
  • လက်ရှိ လူသုံးများတဲ့ Platform တိုင်းမှာ Python က အလုပ်လုပ်ပါတယ်။
  • ကိုယ်ရေးထားတဲ့ Program ကို Platform အသစ်တစ်ခုဆီ ပြောင်းတင်ချင်ရင် (Porting) ဟိုဘက်ဒီဘက် Cut and paste လုပ်လိုက်ရုံပါပဲ။ ဒါက GUI၊ DB access၊ Web programming၊ OS interfacing နဲ့ Directory access စတာတွေအထိ အကျုံးဝင်ပါတယ်။

(e) Support libraries (Library အထောက်အပံ့များ):

  • Text pattern matching ကနေ Networking အပိုင်းထိ Library support အရမ်းတောင့်တင်းတယ်။
  • Third party libraries တွေလည်း တော်တော်များများ စုဆောင်းထားပြီးသား ရှိတယ်။
  • Web site တည်ဆောက်တာ၊ Numeric programming၊ Game development နဲ့ Machine Learning တွေအတွက်ပါ Libraries တွေ အစုံရှိတယ်။

(f) Component integration (တခြားအရာများနှင့် ချိတ်ဆက်နိုင်မှု):

  • C၊ C++ libraries တွေနဲ့ Java components တွေကို လှမ်းခေါ်သုံးလို့ရတယ်။
  • COM၊ .NET လို Frameworks တွေနဲ့လည်း ချိတ်ဆက်လုပ်ဆောင်နိုင်တယ်။
  • SOAP၊ XML-RPC၊ CORBA လို Interfaces တွေကနေတစ်ဆင့် Network ပေါ်မှာ ချိတ်ဆက်ဆောင်ရွက်နိုင်တယ်။
  • လိုအပ်တဲ့ Glue code လေးတွေ သုံးလိုက်မယ်ဆိုရင် Python ကနေ C++၊ Java၊ C# classes တွေကို Subclass လုပ်ပြီး ကိုယ့် Program ရဲ့ လုပ်ဆောင်နိုင်စွမ်းကို ချဲ့ထွင်လို့ရတယ်။
  • Product တစ်ခုကို Customization လုပ်တာတို့၊ Extension လုပ်တာတို့မှာ လူသုံးများကြတယ်။

(g) Enjoyment (သုံးရတာ အရသာရှိခြင်း):

  • သုံးရတာ လွယ်ကူရိုးရှင်းတယ်။
  • Built-in toolset တွေ ပါတယ်။
  • အလုပ်လုပ်ရတာမျိုး မဟုတ်ဘဲ Programming ရေးရတာကိုက ပျော်စရာကောင်းနေစေတယ်။

1.3 What sets Python apart? (Python ကို ဘာက ထူးခြားစေတာလဲ)

(a) Powerful (စွမ်းရည်မြင့်မားခြင်း):

  • Dynamic typing ဖြစ်တယ်။
  • Variable declaration ကြေညာပေးစရာ မလိုဘူး။
  • Automatic allocation နဲ့ Garbage Collection ပါပြီးသား။
  • Classes၊ Modules နဲ့ Exceptions တွေကို Support လုပ်တယ်။
  • Componentization နဲ့ Reuse လုပ်တာတွေကို ခွင့်ပြုပေးတယ်။
  • Lists၊ Dictionaries၊ Tuples စတဲ့ Containers တွေက အရမ်း Powerful ဖြစ်တယ်။

(b) Ready-made stuff (အဆင်သင့်သုံးနိုင်သော အရာများ):

  • Joining၊ Slicing၊ Sorting၊ Mapping စတဲ့ Operations တွေအတွက် Support ပါတယ်။
  • Library ပိုင်း တောင့်တင်းတယ်။
  • Third-party utilities တွေလည်း အများကြီးရှိတယ်။

© Ease of use (အသုံးပြုရ လွယ်ကူခြင်း):

  • ရိုက်ပြီး တန်း Run လိုက်ရုံပဲ (Type and run)။
  • Compile နဲ့ Link အဆင့်တွေ မလိုဘူး။
  • Interactive programming experience ကို ရစေတယ်။
  • အလုပ်ပြီးမြောက်မှု မြန်ဆန်တယ် (Rapid turnaround)။
  • Programs တွေက ရိုးရှင်းတယ်၊ ပိုပြီး Compact ဖြစ်သလို ပိုပြီး Flexible ဖြစ်တယ်။

1.4 Where is Python used? (Python ကို ဘယ်နေရာတွေမှာ သုံးကြလဲ)

Python ကို နေရာစုံ၊ ကဏ္ဍစုံမှာ အသုံးပြုကြပါတယ်။ အဓိက အသုံးပြုကြတာတွေကတော့

  • System programming
  • GUI applications တွေ တည်ဆောက်တဲ့နေရာ
  • Internet scripting
  • Component integration (တခြား Component တွေနဲ့ ချိတ်ဆက်ခြင်း)
  • Database programming
  • Rapid prototyping (နမူနာပုံစံ အမြန်ချရေးခြင်း)
  • Numeric နဲ့ Scientific programming
  • Game programming
  • Robotics programming

1.5 Who uses Python today? (လက်ရှိ Python ကို ဘယ်သူတွေ သုံးနေကြလဲ)

နာမည်ကြီး Organizations တော်တော်များများက Python ကို ရည်ရွယ်ချက်အမျိုးမျိုးနဲ့ သုံးနေကြပါတယ်။ ဥပမာအားဖြင့်

  • Google - သူတို့ရဲ့ Web search system မှာ သုံးပါတယ်။
  • YouTube - Video Sharing service အပိုင်းမှာ သုံးပါတယ်။
  • Bit-torrent - Peer to Peer file sharing system မှာ သုံးပါတယ်။
  • Intel, HP, Seagate, IBM, Qualcomm - Hardware testing ပြုလုပ်တဲ့အပိုင်းမှာ သုံးပါတယ်။
  • Pixar, Industrial Light and Magic - Movie animation ဖန်တီးတဲ့နေရာမှာ သုံးပါတယ်။
  • JP Morgan, Chase, UBS - Financial market forecasting (ငွေကြေးဈေးကွက် ခန့်မှန်းတွက်ချက်ခြင်း) အတွက် သုံးပါတယ်။
  • NASA, FermiLab - Scientific programming အတွက် သုံးပါတယ်။
  • iRobot - စီးပွားဖြစ်ရောင်းချတဲ့ Robot vacuum cleaners တွေမှာ သုံးပါတယ်။
  • NSA - Cryptographic နဲ့ Intelligence analysis ပြုလုပ်ရာမှာ သုံးပါတယ်။
  • IronPort - Email Servers တွေမှာ သုံးပါတယ်။

1.6 Programming Paradigms

  • Paradigm ဆိုတာက Organization principle တစ်ခုပါ၊ Model လို့လည်း ခေါ်ကြပါတယ်။
  • Programming paradigm/model ဆိုတာ Computer programs တွေရဲ့ Structure နဲ့ Elements တွေကို ဘယ်လိုတည်ဆောက်မလဲဆိုတဲ့ ပုံစံ (Style) တစ်ခုပါပဲ။
  • Functional၊ Procedural၊ Object-oriented၊ Event-driven စသဖြင့် Programming models တွေ အများကြီး ရှိပါတယ်။
  • Languages တော်တော်များများက Paradigms တစ်ခုမက Support ပေးထားကြပါတယ်။ ဥပမာ - Python ဆိုရင် Functional၊ Procedural၊ Object-oriented နဲ့ Event-driven programming models အားလုံးကို Support လုပ်ပါတယ်။
  • တချို့အခြေအနေတွေမှာ Functional programming က ရွေးချယ်စရာ ဖြစ်နေပေမယ့်၊ တခြားအခြေအနေတွေမှာတော့ Procedural programming က ပိုပြီး အဆင်ပြေတာမျိုး ရှိပါတယ်။
  • Paradigms တွေဆိုတာ တစ်ခုနဲ့တစ်ခု သီးသန့်ခွဲခြားထားရမယ့်အရာတွေ မဟုတ်ပါဘူး။ Program တစ်ပုဒ်တည်းမှာတင် Paradigms အများကြီးကို ရောသုံးလို့ ရပါတယ်။

1.6.1 Functional Programming Model

  • Functional programming က ပြဿနာတစ်ခုကို Functions အသေးလေးတွေအဖြစ် ခွဲခြမ်းစိတ်ဖြာလိုက်တာပါ။ အဲဒီ Functions တွေက Program ရဲ့ အဓိက Logic တွေ ဖြစ်လာပါတယ်။
  • Functions တွေက Input parameters တွေကို လက်ခံပြီး Outputs တွေကို ထုတ်ပေးပါတယ်။ Python မှာ lambda၊ map၊ reduce နဲ့ filter လိုမျိုး Functional programming techniques တွေ ပါဝင်ပါတယ်။ ဒါတွေကို Chapter 15 ရောက်ရင် အသေးစိတ် ဆွေးနွေးပါမယ်။
  • ဒီ Model မှာ Computation (တွက်ချက်ခြင်း) ကို Mathematical functions တွေ ဖြေရှင်းတွက်ချက်သလိုမျိုး သဘောထားပါတယ်။ ဥပမာ - ကိန်းဂဏန်းတစ်ခုရဲ့ Factorial value ရှာတာ၊ သို့မဟုတ် ဘယ်နှခုမြောက် Fibonacci number လဲဆိုတာ ရှာချင်ရင် အောက်က Functions တွေကို သုံးနိုင်ပါတယ် -
Factorial and Fibonacci
factorial(n) = 1 if n == 0
= n * factorial(n - 1) if n > 0

fibo(n) = 0 if n = 0
= 1 if n = 1
= fibo(n - 2) + fibo(n - 1) if n > 1
  • Function တစ်ခုရဲ့ Output value က သူ့ရဲ့ Arguments တွေအပေါ်မှာပဲ မူတည်ပါတယ်။ ဒါကြောင့် Function တစ်ခုကို တူညီတဲ့ Value နဲ့ ခေါ်လိုက်တိုင်း အမြဲတမ်း တူညီတဲ့ Result ကိုပဲ ရရှိစေပါတယ်။ ဒီအချက်ကြောင့် Parallel execution လုပ်ဖို့အတွက် အထူးသင့်တော်ပါတယ်။
  • ဘယ် Function ကမှ တခြား Variables တွေအပေါ် Side effects မဖြစ်စေပါဘူး (State က မပြောင်းလဲဘဲ ကျန်ရှိနေပါတယ်)။
  • Functional programming model ကို Declarative programming paradigm လို့လည်း ခေါ်လေ့ရှိကြပါတယ်။ ဘာကြောင့်လဲဆိုတော့ Statements တွေအစား Expressions (သို့မဟုတ်) Declarations တွေနဲ့ Programming ရေးသားလို့ ဖြစ်ပါတယ်။

1.6.2 Procedural Programming Model

  • Procedural programming က ပြဿနာတစ်ခုကို Procedures အစုအဝေးတွေအဖြစ် ခွဲခြားလိုက်တာပါ။ ဒီ Procedures တွေက Program ရဲ့ အဓိက Logic တွေ ဖြစ်လာပါတယ်။
  • Procedures တွေက Input parameters တွေကို လက်ခံပြီး Outputs တွေကို ထုတ်ပေးပါတယ်။ Python မှာ Functions၊ Classes၊ Modules စတဲ့ Procedural programming techniques တွေ ပါဝင်ပါတယ်။ ဒါတွေကို Chapter 15 မှာ ဆွေးနွေးသွားပါမယ်။
  • ဒီ Model မှာ Computation (တွက်ချက်ခြင်း) ကို Procedures တွေရဲ့ Evaluation လုပ်ဆောင်ချက်အဖြစ် သတ်မှတ်ပါတယ်။ ဥပမာ - ကိန်းဂဏန်းတစ်ခုရဲ့ Factorial value ရှာတာ၊ သို့မဟုတ် ဘယ်နှခုမြောက် Fibonacci number လဲဆိုတာ ရှာချင်ရင် အောက်က Procedures တွေကို သုံးနိုင်ပါတယ် -
Factorial and Fibonacci
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

def fibo(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibo(n - 2) + fibo(n - 1)
  • Procedure တစ်ခုရဲ့ Output value က သူ့ရဲ့ Arguments တွေအပေါ်မှာပဲ မူတည်ပါတယ်။ ဒါကြောင့် Procedure တစ်ခုကို တူညီတဲ့ Value နဲ့ ခေါ်လိုက်တိုင်း အမြဲတမ်း တူညီတဲ့ Result ကိုပဲ ရရှိစေပါတယ်။ ဒီအချက်ကြောင့် Parallel execution လုပ်ဖို့အတွက် အထူးသင့်တော်ပါတယ်။
  • ဘယ် Procedure ကမှ တခြား Variables တွေအပေါ် Side effects မဖြစ်စေပါဘူး (State က မပြောင်းလဲဘဲ ကျန်ရှိနေပါတယ်)။
  • Procedural programming model ကို Imperative programming paradigm လို့လည်း ခေါ်ကြပါတယ်။ ဘာကြောင့်လဲဆိုတော့ Expressions (သို့မဟုတ်) Declarations တွေအစား Statements (သို့မဟုတ်) Instructions တွေနဲ့ Programming လုပ်လို့ ဖြစ်ပါတယ်။
  • Procedural programming က Statement တစ်ကြောင်းချင်းစီ (Procedure တစ်ခုချင်းစီ) အလုပ်လုပ်ပြီး ပြဿနာကို ဖြေရှင်းပါတယ်။ ဒါကြောင့် သူ့မှာ သတ်မှတ်ထားတဲ့ Order အတိုင်း Execute လုပ်မယ့် အဆင့်ဆင့် ပါဝင်ပါတယ်။

  • သူကလည်း Functions တွေကို သုံးပေမယ့် Functional programming မှာ သုံးတဲ့ Mathematical functions တွေနဲ့တော့ မတူပါဘူး။ Functional programming က Expressions တွေကို အဓိကထားပြီး၊ Procedural programming ကတော့ Statements တွေကို အဓိကထားပါတယ်။

  • Statements တွေမှာ Values မရှိပါဘူး၊ အဲဒီအစား သူတို့က Conceptual machine ရဲ့ State တစ်ခုခုကို ပြောင်းလဲပေးလိုက်ပါတယ်။
  • တူညီတဲ့ Language expression ဖြစ်ပေမယ့် Run နေတဲ့ Program ရဲ့ Global state အပေါ်မူတည်ပြီး အချိန်မတူရင် Values ကွဲပြားသွားနိုင်ပါတယ်။ နောက်ပြီး Functions တွေက Program ရဲ့ State ကို ပြောင်းလဲနိုင်ပါတယ်။
  • Explicit sequence of statements တွေနဲ့ State ကို ပြောင်းလဲတဲ့အတွက် Procedural programming model ကို Imperative programming လို့ ခေါ်လေ့ရှိကြတာပါ။

1.6.3 Object-oriented Programming Model

  • ဒီ Model ကတော့ ကွန်ပျူတာထဲမှာ Objects လေးတွေနဲ့ တည်ဆောက်ထားတဲ့ ကမ္ဘာငယ်လေးတစ်ခု ဖန်တီးပြီး Real world (အပြင်လောကကြီး) ကို တုပထားတာ ဖြစ်ပါတယ်။
  • တက္ကသိုလ်တစ်ခုရဲ့ System မှာဆိုရင် VC (ပါမောက္ခချုပ်)၊ Professors၊ Non-teaching staff၊ Students၊ Courses၊ Semesters၊ Examinations စတာတွေ အားလုံးက Objects တွေ ဖြစ်နိုင်ပါတယ်။
  • Object တိုင်းမှာ State (values) နဲ့ Behavior (interface/methods) ရှိကြပါတယ်။ Objects တွေက သူတို့ကို ဖန်တီးလိုက်တဲ့ Class အပေါ်မူတည်ပြီး State နဲ့ Behavior တွေကို ရရှိပါတယ်။
  • Objects တွေက တစ်ခုနဲ့တစ်ခု Interface methods တွေကို လှမ်းခေါ်ပြီး Messages တွေ ပေးပို့ကာ အပြန်အလှန် ဆက်သွယ်လုပ်ဆောင်ကြပါတယ်။

1.6.4 Event-driven Programming Model

  • ဒီ Model ကိုတော့ Windows၊ Check boxes၊ Buttons၊ Combo-boxes၊ Scroll bars၊ Menus စတာတွေပါတဲ့ GUI applications တွေ ရေးသားရာမှာ အများဆုံး သုံးကြပါတယ်။
  • ကျွန်တော်တို့က ဒီ Elements တွေနဲ့ ထိတွေ့ဆက်ဆံလိုက်တဲ့အခါ (ဥပမာ - Button နှိပ်တာ၊ Scrollbar ဆွဲတာ၊ Menu item ရွေးတာမျိုး)၊ Events တွေ ဖြစ်ပေါ်လာပြီး ဒီ Elements တွေက Messages တွေကို ထုတ်လွှတ်ပေးပါတယ်။ ဒီ GUI elements တွေမှာ Register လုပ်ထားတဲ့ Listener methods တွေက အဲဒီ Events တွေကို တုံ့ပြန်ဆောင်ရွက်ပါတယ်။
  • ကျွန်တော်တို့ GUI elements တွေကို သုံးတဲ့ပုံစံအပေါ် မူတည်ပြီး Events တွေက ဘယ်အစဉ်လိုက် ဖြစ်မယ်ဆိုတာ အသေအချာ ပြောလို့မရတဲ့အတွက် Listeners တွေက Events တွေကို Asynchronous manner (အစဉ်လိုက်မဟုတ်သော ပုံစံ) နဲ့ Handle လုပ်နိုင်စွမ်း ရှိရပါမယ်။

Exercises

[A] Answer the following (အောက်ပါတို့ကို ဖြေဆိုပါ):

  • (a) Python ကို လူသုံးများတဲ့ နယ်ပယ် (၅) ခုကို ဖော်ပြပါ။
  • (b) Event-driven programming ကို ဘယ်နေရာမှာ အဓိက အသုံးပြုသလဲ။
  • © ဘာကြောင့် Python ကို Portable language လို့ ခေါ်တာလဲ။
  • (d) ဒီ Chapter မှာ ဆွေးနွေးခဲ့တဲ့ Programming models အမျိုးမျိုးရဲ့ အရေးအကြီးဆုံး Feature တစ်ခုစီက ဘာလဲ။
  • (e) အောက်ပါအချက်များထဲမှ ဘယ်အရာက Python ရဲ့ Feature မဟုတ်သလဲ။
  • Static typing
  • Dynamic typing
  • Error numbers ဖြင့် Run-time error handling လုပ်ခြင်း
  • Lists၊ Dictionaries၊ Tuples ကဲ့သို့သော Containers များအတွက် Library support ပါဝင်ခြင်း
  • (f) အောက်ပါ Programming models တစ်ခုစီအတွက် ဥပမာ Application တစ်ခုစီ ပေးပါ။
  • Functional model
  • Procedural model
  • Object-oriented model
  • Event-driven model

[B] State whether the following statements are True or False (အောက်ပါအဆိုများသည် မှန်/မှား ဖော်ပြပါ):

  • (a) Python သည် အခမဲ့အသုံးပြုနိုင်ပြီး ဖြန့်ဝေနိုင်သည်။
  • (b) Python Program တစ်ပုဒ်တည်းကို မတူညီတဲ့ OS - Microprocessor တွဲဖက်တွေမှာ အသုံးပြုနိုင်သည်။
  • © Python Program ထဲတွင် C++ သို့မဟုတ် Java libraries များကို အသုံးပြုနိုင်သည်။
  • (d) Python တွင် Variable ၏ Type ကို ၎င်း၏ အသုံးပြုပုံအပေါ် မူတည်ပြီး ဆုံးဖြတ်သည်။
  • (e) Python ကို GUI applications များ တည်ဆောက်ရန် အသုံးမပြုနိုင်ပါ။
  • (f) Python သည် Functional၊ Procedural၊ Object-oriented နှင့် Event-driven programming models များကို Support လုပ်သည်။
  • (g) GUI applications များသည် Event-driven programming model ကို အခြေခံသည်။
  • (h) Functional programming model သည် Objects အများအပြား ချိတ်ဆက်လုပ်ဆောင်ခြင်း ဖြစ်သည်။

[C] Match the following pairs (အောက်ပါတို့ကို တွဲဖက်ပါ):

  • (a) Functional programming -> 1. GUI element based interaction
  • (b) Event-driven programming -> 2. Interaction of objects
  • © Procedural programming -> 3. Statements
  • (d) OOP -> 4. Maths-like functions

[D] Fill in the blanks (ကွက်လပ်ဖြည့်ပါ):

  • (a) Functional programming paradigm ကို ________ programming model ဟုလည်း ခေါ်သည်။
  • (b) Procedural programming paradigm ကို ________ programming model ဟုလည်း ခေါ်သည်။
  • © Python ကို _________ က ဖန်တီးခဲ့သည်။
  • (d) Python Programmers များကို _________ ဟု ခေါ်လေ့ရှိသည်။

Comments