Adsense

OUO Banner

Latest Posts

Python3 Lesson 1




Python Programming
Lesson 1
ဒီေန႔မွာေတာ့ ကြ်န္ေတာ္တို႔အေနနဲ႔ Python နဲ႔ ပတ္သက္ျပီး အတြင္းက်က်အခ်က္ေတြကို ေလ့လာရေအာင္ပါ။ Python 3 ကို အဓိကထားအသံုးျပဳမွာျဖစ္ျပီး Python နဲ႔ပတ္သက္တဲ့ သိသင့္သိရာေတြကိုသာ ေဆြးေႏြးမွာျဖစ္ပါတယ္။

Python Strings
* String ေတြဟာဆိုရင္ Python မွာ popular အျဖစ္ဆံုး ပံုစံ ျဖစ္ပါတယ္။ ကြ်န္ေတာ္တို႔ အသံုးျပဳ ေရးသားမယ့္ python 3 ရဲ႕ idle3 မွာသာမက idle (Python 2) မွာပါ အစိမ္းေရာင္နဲ႔ ေဖာ္ျပပါတယ္။
*Quote (Single quote or Double quotes) ထဲမွာ ေရးရပါတယ္။
* Python မွာ String ေရးတဲ့အခါ Single quote ေရာ Double quotes ေရာကို အတူတူပဲ သတ္မွတ္ပါတယ္။
* String မွာေတာ့ မည္သည့္စာမဆို ျဖစ္ႏိုင္ပါတယ္။ အေရအတြက္လည္း ကန္႔သတ္ထားျခင္း မရွိပါဘူး။
* Empty String ဆိုတာကေတာ့ '' (single quote ၂ခ်က္) ျဖစ္ပါတယ္။ အတြင္းမွာ မည္သည့္စာမွ် မပါပါဘူး။
* String ထဲမွာေတာ့ quote ေတြဟာ ကိန္းလံုးေတြကိုေတာင္ စာသားတန္ဖိုးအျဖစ္ ထည့္သြင္းသတ္မွတ္ပါတယ္။ ဒါေၾကာင့္ သင္ဟာ ကိန္းဂဏန္းေတြကို အသံုးျပဳတြက္ခ်က္ရမယ္ဆိုရင္ String မွာ မထည့္ရပါဘူး။
* \ သေကၤတကိုေတာ့ quote ေတြကေန လြန္ေျမာက္ဖို႔ လိုအပ္ရင္ သံုးပါတယ္။ ဒီအခ်က္ကို ရွင္းျပပါမယ္။  idle3 မွာ ေအာက္ပါအတိုင္း ေရးၾကည့္ပါ။ ျပီးရင္ Enter ။
  print ("I don't have money.")
အထက္ပါ စာေၾကာင္းကို ၾကည့္ရင္ ရွင္းပါတယ္။ Double quote ကို အသံုးျပဳထားတဲ့အတြက္ စက္က Double quotes အတြင္းစာသားအားလံုးကို string အျဖစ္ သတ္မွတ္သြားလို႔ပါ။
  print ('I don't have money.')
ဒီစာေၾကာင္းကိုၾကည့္ပါ။ အေပၚစာေၾကာင္းနဲ႔ စာသားခ်င္း အားလံုး တူေပမယ့္ quote မွာေတာ့ single quote ကိုသာ အသံုးျပဳဳထားတာေၾကာင့္ စက္က ဘယ္ေနရာမွာဆံုးလဲမသိတဲ့အတြက္ အနီးဆံုးျဖစ္တဲ့  'I don' ထိပဲ သတ္မွတ္လိုက္မွာျဖစ္ပါတယ္။ ဒါေၾကာင့္ error ကိုသာ သင္ျမင္ရမွာပါ။ ေ(ရွ႕ပိုင္း second scheme မွာလည္း ရွင္းျပခဲ့ျပီးပါျပီ။) Double Quotes နဲ႔ ေရးရင္ ရေပမယ့္ single quote ထဲမွာေရာ ေရးလို႔ မရဘူးလား ေမးစရာရွိလာပါတယ္။ အေျဖက ရပါတယ္။ ဒီလိုေရးၾကည့္ရေအာင္ပါ။
  print ('I don\'t have money.')
\ ထည့္လိုက္တာေၾကာင့္ အဲသည္ေနာက္က ' (quote) ကို စာသားထဲ ပါတယ္လို႔ စက္က သတ္မွတ္လိုက္လို႔ပါပဲ။

* \n ကို ေနာက္တစ္ေၾကာင္း ဆင္းရန္အတြက္ အသံုးျပဳပါတယ္။ \a ကေတာ့ ဖုန္းပံုစံပါ။
ဒါေၾကာင့္လည္း အနည္းငယ္ အခက္အခဲ ျဖစ္တတ္ပါတယ္။ ေအာက္ပါအတိုင္း idle3 မွာ ရိုက္ၾကည့္လိုက္ပါ။
>>> print('c\app\new')
c pp
ew
ဆိုျပီး ေပၚလာမွာျဖစ္ပါတယ္။ \a ကို ဖုန္းပံုစံ သတ္မွတ္ျပီး \n ကို enter (ေနာက္တစ္ေၾကာင္း) လို႔ သတ္မွတ္လိုက္တာေၾကာင့္ပါ။
ဒါဆို ကြ်န္ေတာ္တို႔က c\app\new ဆိုတဲ့စာကို ဒီအတိုင္းပဲ မျဖစ္မေန ထုတ္ဖို႔လိုလာတဲ့အခါမွာ ဘယ္လိုလုပ္မလဲ။
လြယ္ပါတယ္။ ေရွ႕မွာ r ထည့္ေပးလိုက္ရံုပါပဲ။ idle3 မွာ ေအာက္ပါအတိုင္းဆက္ရိုက္ၾကည့္ပါဦး။
>>> print(r'c\app\new')
c\app\new ဆိုျပီး ကြ်န္ေတာ္တို႔ လိုခ်င္တဲ့အတိုင္း ေဖာ္ျပေပးသြားမွာျဖစ္ပါတယ္။

Strings  နဲ႔ ပတ္သက္ျပီး ေနာက္ထပ္ ျပန္ေႏႊးေပးခ်င္တာေလးေတြ ရွိပါေသးတယ္။
idle3 မွာပဲ file new မလုပ္ဘဲ တိုက္ရိုက္ စမ္းၾကည့္ရေအာင္ေနာ္။
>>> 'New ' + 'York'
'New York'
>>> 4*'New'
'NewNewNewNew'
>>> 4*' New' + 4*' York'
' New New New New York York York York'
>>> 3*'Um' + 'brella'
'UmUmUmbrella'
Second Scheme မွာလည္း သင္ခဲ့ျပီးသားမို႔ အေထြအထူးေတာ့ ရွင္းမျပေတာ့ပါ။ String သည္ သူ႔ဆီ ဝင္လာသမွ်ကို စာသားတန္ဖိုးအျဖစ္သာ သတ္မွတ္ပါတယ္။
ဒါေၾကာင့္
>>> '3' + '5'
'35'
string (အစိမ္းေရာင္စာလံုး) နဲ႔ ကိန္းေတြကို အေပါင္း အနုတ္ ျပဳလုပ္လို႔ကေတာ့ အေျဖမွန္ကို လံုးဝ ရမွာမဟုတ္တာ သတိထားေစလိုပါတယ္။
......................
String Formatting
String ကို superscripted လို႔ ဆိုႏိုင္ပါတယ္။ အညႊန္းကိန္း သုညနဲ႔ စျပီး စာလံုးေတြကို ေရတြက္ႏိုင္တဲ့အေၾကာင္း index မွာ သင္ခဲ့ပါတယ္။
ဒီေနရာမွာ တစ္ခါ ထပ္ေျပာပါမယ္။
>>> word = 'Python'
>>> word[3]
'h'
Python ဆိုတဲ့ စာလံုးထဲမွာ ေရၾကည့္ရင္ P=0, y=1, t=2, h=3. o=4, n=5 ျဖစ္ပါတယ္။
>>> x = 'I love Python'
>>> x[2]
'l'
>>> x[5]
'e'
>>> x[6]
' '
>>> x[7]
'P'
ဆိုျပီး ထုတ္ျပေပးသြားမွာျဖစ္ပါတယ္။ space ျခားထားတာကိုပါ ထည့္ေရတယ္ဆိုတာ ေျပာထားျပီးသားေနာ္။ x[6] မွာလည္း အေျဖပါပါတယ္။
....................
I love Python
0123456789...
....................
ေရတြက္ပံုကို မွတ္မိမယ္ထင္ပါတယ္။


Triple Quotes
Triple quotes ဆိုတာကေတာ့ single (or) double quotes ေတြကို သံုးႀကိမ္ဆက္ေရးထားျခင္းကို ဆိုလိုပါတယ္။ တစ္ေၾကာင္းထက္ ပိုတဲ့စာေတြကို ေဖာ္ျပခ်င္ရင္ သံုးတာပါ။ ဥပမာေျပာရရင္
idle 3 မွာ ေအာက္ပါအတိုင္းရိုက္ၾကည့္ပါ။
..
>>> print ('''\
I am going to teach Python. I love python.
Ch1. Python Introduction    see page 5
Ch2. Python Strings         see page 10
ကိုယ္ေပၚေစခ်င္တဲ့စာကိုေရးပါ။ အဆံုးသတ္ကိုလည္း triple quotes နဲ႔သာ အဆံုးသတ္ရပါမယ္
''')
...........
enter ဆက္ဆင္းလိုက္မယ္ဆိုရင္ေတာ့ ေအာက္ပါ အေျဖကို ရပါမယ္။
...........
I am going to teach Python. I love python.
Ch1. Python Introduction    see page 5
Ch2. Python Strings         see page 10
ကိုယ္ေပၚေစခ်င္တဲ့စာကိုေရးပါ။ အဆံုးသတ္ကိုလည္း triple quotes နဲ႔သာ အဆံုးသတ္ရပါမယ္
..............
........................................................
.
Python Programming
Lesson 2
Lesson 1 ကို ေသခ်ာ ဖတ္ျပီးမွ ဆက္ဖတ္ရန္
.....
Python Language
*Python is an interpretd, object-oriented, high-level programming language with dynamic semantics.
*Python allows for Rapid Application Development.
*Python can be used a scripting or glue language to connect existing components together.
*Python is simple, easy to learn syntax emphasizes readability and code reuse.
Python အေၾကာင္းေျပာမယ္ဆိုရင္ေတာ့ အမ်ားႀကီးေျပာရမွာပါ။ အလြယ္ဆံုးေျပာရရင္ေတာ့ high-level programming language တစ္ခုလို႔ ဆိုရပါမယ္။ High-level programming language ဆိုတာက စက္ေရာ လူေရာ နားလည္လြယ္တဲ့ ကုဒ္ေတြနဲ႔ ေရးရျခင္းကို ဆိုလိုတာျဖစ္ပါတယ္။ Python ရဲ႕ အားသာခ်က္က application ဖန္တီးရတာ ျမန္တယ္။ သင္ယူရတာ လြယ္တယ္။ ကုဒ္ေတြကို အႀကိမ္ႀကိမ္ ျပန္သံုးႏိုင္တယ္။ စသည္ စသည္ျဖင့္ အားသာခ်က္ေတြ အမ်ားႀကီးနဲ႔ လူသိမ်ားလာတဲ့ language ျဖစ္ပါတယ္။
Google ရဲ႕ နာမည္ေက်ာ္ Google Search  (ျပိဳင္ဘက္မရွိ Search Engine)ကိုလည္း Python နဲ႔ပဲ အဓိက ေရးခဲ့တာျဖစ္ျပီး Hacking Tools ေတြကိုလည္း Python နဲ႔ပဲ ဖန္တီးထားၾကပါတယ္။ ဒါေၾကာင့္ Python ဟာ အလြန္ အေရးပါတဲ့ Programming Language လို႔ေျပာရင္ မမွားပါဘူး။

Python Input and Output
Input မပါဘဲ ေရးဆြဲႏိုင္တဲ့ program ဆိုတာ မရွိသေလာက္ပါပဲ။ Input ဟာ နည္းလမ္းအမ်ိဳးမ်ိဳးနဲ႔ လာပါတယ္။ ဥပမာေျပာရရင္ Database ေတြဆီကျဖစ္ေစ၊ အျခား ကြန္ပ်ဴတာေတြကျဖစ္ေစ၊ muse click လုပ္ျပီးျဖစ္ေစ ဒါမွမဟုတ္ အင္တာနက္မွျဖစ္ေစ စသည္ျဖင့္ျဖစ္ပါတယ္။ Python မွာေတာ့ input() ဆိုတဲ့ function ကို အသံုးျပဳႏိုင္ပါတယ္။ input အတြက္ေတာ့ ေအာက္ပါ example ေလးမ်ားကို ကြ်န္ေတာ္တို႔ ေလ့လာခဲ့ျပီးပါျပီ။
 example ကို ေလ့လာရန္ idle 3 ကိုဖြင့္ျပီး file new လုပ္ပါ။ ေအာက္ပါကုဒ္လိုင္းေလးေတြ ေရးျပီး စမ္းၾကည့္ႏိုင္ပါတယ္။
.................
name = input("What is your name?  ")
print ("Nice to meet you, " + name + "")
age = input ("How old are you, " + name + "?")
print ("So, you are already " + age + " years old," + name + ".")
............

Reading and Writing Files Syntax
* ပထမဆံုး argument ကေတာ့ ဖိုင္နာမည္ ပါဝင္တဲ့ string ျဖစ္ပါတယ္။
* ဒုတိယ argument ကေတာ့ ကြ်န္ေတာ္တို႔ အသံုးျပဳမယ့္ ပံုစံကို ေဖာ္ျပမယ့္ ေနာက္ထပ္ string တစ္ခုျဖစ္ပါတယ္။
(ဒီတိုင္းရွင္းျပရတာက ရႈပ္ပါတယ္။ ဖိုင္ကို ဖတ္တဲ့ ေရးတဲ့ သင္ခန္းစာေတြကို ကြ်န္ေတာ္တို႔ သင္ခဲ့ျပီးေနာ္)
.......................
ျပန္ေႏႊးခ်င္ရင္ Desktop ေပၚမွာ folder တစ္ခုေဆာက္ပါ။ ျပီးရင္ idle3 ကိုဖြင့္ file new ယူ။ ေအာက္ပါ ကုဒ္လိုင္းေလးေတြ ေရးလိုက္ပါ။
......
file = open ("file.txt", "w")
file.write("Hello, I am Khit Minnyo. I'm studying python. I love it so much..")
file.close()
ျပီးရင္ ခုန folder အသစ္ထဲ save ပါ။ ျပီးရင္ idle ကေနပဲ RUN ၾကည့္လိုက္ပါ။ folder ထဲမွာ ေနာက္တစ္ဖိုင္ ေပၚလာပါမယ္။ ဒီသင္ခန္းစာကို လုပ္ျပီးသားမို႔ အေထြအထူး ထပ္မေျပာေတာ့ပါဘူး။ see picture
........

Python Expressions and Operators
* Expression က ကိန္းဂဏန္းေတြ, string ေတြ၊ ဒါမွမဟုတ္ class ေတြ စတဲ့ တစ္စံုတစ္ရာကို ကိုယ္စားျပဳ ေဖာ္ျပေပးပါတယ္။
* မည္သည့္တန္ဖိုးမဆို Expression ျဖစ္ႏိုင္ပါတယ္။ တစ္စံုတစ္ရာကို ျပဳလုပ္ဖို႔ statement ကိုလည္း Expression လို႔ ေခၚႏိုင္ပါတယ္။
* Variables ေတြ နဲ႔ Function ေတြကို Assignment ျပဳလုပ္ေပးတာဟာလည္း Statement ျဖစ္ပါတယ္။
ေဖာ္ျပခ်က္ေတြတိုင္းကို Expressions လို႔ သတ္မွတ္လိုက သတ္မွတ္ႏိုင္ပါတယ္။ ဒါကိုေတာ့ အက်ယ္ခ်ဲ႕မေနေတာ့ပါဘူး။


Operators
e.g.
howdy = "Hello" ဆိုတဲ့ variable သတ္မွတ္ခ်က္ကေလးကို ၾကည့္ရေအာင္။ howdy ကို Hello ျဖစ္ေအာင္ သတ္မွတ္ေပးလိုက္တာက Expression ျဖစ္ျပီး အဲဒီ Expression က လုပ္ေဆာင္သြားျခင္းကို statement လို႔ သတ္မွတ္ႏိုင္ပါတယ္။
hello ဆိုတာ ရိုးရွင္းတဲ့ ေဖာ္ျပခ်က္ (Simple Expression) သာျဖစ္ျပီး သူ႔မွာ operator ဘာမွ မပါဝင္ေသးပါဘူး။ howdy ဆိုတာကိုလည္း Hello လို႔သာ သတ္မွတ္လိုက္ရံုပဲ ရွိပါေသးတယ္။
e.g. >>> y = ("2" + "3")
Operator ဆိုတာကေတာ့ operation ေတြ ျဖစ္လာဖို႔အတြက္ ေပါင္းစပ္ရတဲ့ အေသးဆံုးအစိတ္အပိုင္းျဖစ္တဲ့ operand ေတြကို manipulate (Handle or Control) ဖို႔အတြက္ ေဆာက္လုပ္ထားတဲ့အရာလို႔ အၾကမ္းဖ်င္း  သတ္မွတ္လို႔ ရေပမယ့္ English လိုပဲ မွတ္ထားတာက ပိုရွင္းပါတယ္။
ရွင္းသြားေအာင္ ဥပမာေလးနဲ႔ ေျပာျပမယ္။ 4 + 5 ဆိုပါစို႔။ 4 နဲ႔ 5 ဟာ Operand ေလးေတြျဖစ္ပါတယ္။ သူတို႔ေတြ အလုပ္လုပ္ဖို႔အတြက္ ၾကားခံ ေပါင္းစပ္ေပးတဲ့ + ကေတာ့ operator ေပါ့။
ဒါဆိုရင္ေတာ့ သူ႔ရဲ႕ meaning ကို နားလည္မယ္ထင္ပါတယ္။
.......

Basic Operator Types
****** Python Language မွာ support လုပ္တဲ့ အဓိက operators ပံုစံ ၃ခု ရွိပါတယ္။
1. Arithmetic operators
2. Comparison (Relation) Operators
3. Assignment Operators ဆိုျပီး ျဖစ္ပါတယ္။
operator အကုန္ေျပာရင္ေတာ့
    Arithmetic Operators
    Comparison (Relational) Operators
    Assignment Operators
    Logical Operators
    Bitwise Operators
    Membership Operators
    Identity Operators
ဆိုျပီး အမ်ားႀကီး ရွိပါတယ္။ အဓိကကေတာ့ အေပၚမွာ ေဖာ္ျပခဲ့တဲ့ ၃မ်ိဳးပါ။ အေသးစိတ္ ေဖာ္ျပထားကို ၾကည့္ရန္ Screen အျပည့္ ဖြင့္ၾကည့္ပါ။


  1. Arithmetic operators

Operator Description   Example

+ Addition Adds values on either side of the operator a + b = 30

- Subtraction Subtracts right hand operand from                       a – b = -10
                                 left hand operand. 

* Multiplication         Multiplies values on either side of                        a * b = 200
                                the operator

/ Division          Divides left hand operand by right                      b / a = 2
                                  hand operand

% Modulus Divides left hand operand by right                       b % a = 0
                                  hand operand and returns remainder
** Exponent Performs exponential (power) calculation            a**b =10 to the
                                 on operators                                                              power 20


// Floor Division      The division of operands where the result
is the quotient in which the digits after 9//2 = 4 and 9.0//2.0 = 4.0,
the decimal point are removed. But if one of the operands is negative, the result is floored, -11//3 = -4, -11.0//3 = -4.0
i.e., rounded away from zero (towards negative infinity):

......................................................................................................................................................


                                                   2. Comparison (Relation) Operators

Operator Description Example

== If the values of two operands are equal, then the condition becomes true. (a == b) is not true.

!= If values of two operands are not equal, then condition becomes true.

> If the value of left operand is greater than the value of right operand,
then condition becomes true. (a > b) is not true.

< If the value of left operand is less than the value of right operand,
then condition becomes true. (a < b) is true.

>= If the value of left operand is greater than or equal to the value of right operand,
then condition becomes true. (a >= b) is not true.

<= If the value of left operand is less than or equal to the value of right operand,
then condition becomes true. (a <= b) is true.

...................................................................................................................................................

3. Assignment Operators

Operator Description Example
= Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c

+= Add AND It adds right operand to the left operand and assign the result
to left operand c += a is equivalent to c = c + a


-= Subtract AND It subtracts right operand from the left operand and assign
the result to left operand c -= a is equivalent to c = c - a


*= Multiply AND It multiplies right operand with the left operand and assign
the result to left operand c *= a is equivalent to c = c * a

/= Divide AND It divides left operand with the right operand and
assign the result to left operand c /= a is equivalent to c = c / a
c /= a is equivalent to c = c / a

%= Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a

**= Exponent AND Performs exponential (power) calculation on operators and
assign value to the left operand c **= a is equivalent to c = c ** a


//= Floor Division It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c //

................................................................................................................................................

Python Operators Precedence



Operator Description
** Exponentiation (raise to the power)

~ + - Complement, unary plus and minus (method names for the last two are +@ and -@)

* / % // Multiply, divide, modulo and floor division

+ - Addition and subtraction

>> << Right and left bitwise shift

& Bitwise 'AND'

^ | Bitwise exclusive `OR' and regular `OR'

<= < > >= Comparison operators

<> == != Equality operators

= %= /= //= -= += *= **= Assignment operators

is is not Identity operators

in not in Membership operators

not or and Logical operators

...................................................................................................................................................




သင္ျပီးသားေတြလည္းပါ။ ေရးထားတာေတြလည္း ရွင္းတာေၾကာင့္ ျမန္မာလို ျပန္မေျပာျပေတာ့ဘူးေနာ္။ မ်ားလိုက္တဲ့ operators ေတြပါလား ????????????
ဒါေတြ အလြတ္ ရရမယ္လို႔ မဆိုလိုပါဘူး။ ဘာက ဘာဆိုတာ သိဖို႔ေတာ့ လိုပါတယ္။ အဲသလိုပဲ လိုအပ္ရင္ ျပန္ၾကည့္ျပီး ေရးႏိုင္ဖို႔လည္း လိုပါတယ္။ ေခါင္းမူးေနေလာက္ျပီမို႔ ဒီေန႔ေတာ့ ဒီေလာက္ပါပဲ။ မနက္ျဖန္မွာ ဆက္ၾကဦးစို႔
..................
သင္ခန္းစာ ၂ ကို ဖတ္ရန္ ဒီစာေၾကာင္းကို ႏွိပ္ပါ


Thanks


Khit Minnyo











1 comment:

  1. So good sir!thank you so much for sharing your knowledges.....

    ReplyDelete