Python2 Lesson 7
Python သင္ခန္းစာမ်ားကို အသစ္ထည့္ထားေပးေသာ menu box ကေလးမွတစ္ဆင့္ Python for Hacker ကို ေရြးခ်ယ္ျပီး ၾကည့္ႏိုင္ပါတယ္။
အပိုင္း 4 ထိ သင္ခန္းစာေတြကို မၾကည့္ရေသးသူမ်ား ေအာက္ပါ link ကေနလည္း သြားေရာက္ ၾကည့္ရႈႏိုင္မွာျဖစ္ပါတယ္ခင္ဗ်ာ
Lesson 1 ၾကည့္ရန္ link >> Click Here
Lesson 2 ၾကည့္ရန္ link >> Click Here
Lesson 3 ၾကည့္ရန္ link >> Click Here
Lesson 4 ၾကည့္ရန္ link >> Click Here
Lesson 5 ၾကည့္ရန္ link >> Click Here
Lesson 6 ၾကည့္ရန္ link >> Click Here
........................................
အဆက္
Python Programming
..
ဒီေန့ေတာ့ မေန့က ေမးခဲ့တဲ့ lesson ေလးကို ျပန္ဆက္ပါမယ္။
မေန့က problem ေလးနဲ့ ပတ္သက္ျပီး ျပန္လည္ ရွင္းျပခ်င္ပါတယ္။ ကြ်န္ေတာ္တို့ leafpad မွာ ေရးခဲ့တာေတာ့ ေအာက္ပါအတိုင္း ျဖစ္ပါတယ္။
...
khitminnyo = ()
if not khitminnyo:
print "\n There is nothing in khitminnyo."
raw_input("\n Press enter to continue......")
khitminnyo = ( "Laptop1", "Books" , "1Android" , "1000Ks7" )
print "\n Khit Minnyo has the items:", khitminnyo
raw_input("\n Press enter to exit.")
RUN တဲ့အခါ ဘာလို့ error တက္ရသလဲ ဆိုတာကို ခု ျပန္ရွင္းေပးသြားမွာ ျဖစ္ပါတယ္။
Terminal ကေန python လို့ ရိုက္ေခၚလိုက္ပါ။
>>> khitminnyo = ()
>>> if not khitminnyo:
... print "\n There is nothing in khitminnyo."
File "
print "\n There is nothing in khitminnyo."
^
IndentationError: expected an indented block
print ျပီး enter လိုက္တာနဲ့ အထက္ပါအတိုင္း error ေတြ့ျခင္း ျဖစ္ပါတယ္။
ခု ipython ကို ဖြင့္ျပီး စမ္းျကည့္ရေအာင္ပါ။
(ipython install ထားသူမ်ားသာ)
In [1]: khitminnyo = ()
In [2]: if not khitminnyo:
...: print "\n There is nothing in khitminnyo."
...:
There is nothing in khitminnyo.
ipython မွာလည္း ဒီအတိုင္းပဲ ရိုက္တာပါပဲ ဘာလို့ အေျဖထြက္လာလဲ??????
အဲဒါေလးကို ရွင္းျပရမယ္ဆိုရင္ေတာ့ else သင္ခဲ့စဥ္တုန္းက သင္ခန္းစာေလးကိုပဲ ျပန္ကိုးကားရပါလိမ့္မယ္။
if ေရးျပီးလို႔ print ေတာ့့မယ့္အခါ print မေရးခင္ tab တစ္ခ်က္ ခုန္ထားပါမယ္။
if .................:
print .........
ပံုစံပါ။ သေဘာကေတာ့ print မယ့္ လုပ္ေဆာင္ခ်က္သည္ if ထဲမွာ ပါဝင္ေနတယ္ ဆိုတာပါပဲ။ ဒါေၾကာင့္ အတြင္းသြင္းျပီးေရးတယ္လို႔ မွတ္ထားေပါ့။
terminal က python ဆီမွာ ျပန္ေရးၾကည့္ၾကစို႔
.
>>> khitminnyo = ()
>>> if not khitminnyo:
... print "\n There is nothing in khitminnyo."
...
There is nothing in khitminnyo.
>>>
python မွာ ကြ်န္ေတာ္တို့ လိုခ်င္တဲ့ အေျဖ ရပါျပီ။ ဒါဆို မေန့က Save ခဲ့တဲ့ ဖိုင္ဆီ ျပန္သြားရေအာင္။
R-click နွိပ္ျပီး text editor နဲ့ ဖြင့္ဖြင့္ Double click နဲ့ပဲ ဖြင့္ဖြင့္ပါ။
.ျပီးရင္ လိုင္း3 မွာရွိတဲ့ print ေရွ့မွာ ကာဆာ ခ်ျပီး space bar ၄ခ်က္နွိပ္ျပီး ေနာက္ပို့လိုက္ပါ။
သို႔မဟုတ္ tab key ကို တစ္ခ်က္ ခုန္လိုက္ပါ။ (1 tab = 4 space)
ေအာက္ပါအတိုင္း ျဖစ္သြားပါျပီ။
khitminnyo = ()
if not khitminnyo:
print "\n There is nothing in khitminnyo."
raw_input("\n Press enter to continue......")
khitminnyo = ( "Laptop1", "Books" , "1Android" , "1000Ks7" )
print "\n Khit Minnyo has the items:", khitminnyo
raw_input("\n Press enter to exit.")
....
Save ျပီး ျပန္ RUN ျကည့္ပါဦး။
အဆင္ေျပသြားျပီေနာ္။
.....
.....
အထက္ပါ ပံုစံမွာပဲ ေနာက္ထပ္ အနည္းငယ္ ထပ္ျဖည့္ျကည့္ရေအာင္။ ေအာက္ဆံုး တစ္တန္း ခ်န္ျပီး ျကားမွာ ျဖည့္မယ္ေနာ္။
khitminnyo = ()
if not khitminnyo:
print "\n There is nothing in khitminnyo."
raw_input("\n Press enter to continue......")
khitminnyo = ( "Laptop1", "Books" , "1Android" , "1000Ks7" )
print "\n Khit Minnyo has the items:", khitminnyo
for item in khitminnyo:
print item
raw_input("\n Press enter to exit.")
...
ဘာမွ အေထြအထူးေတာ့ မေျပာင္းလဲေသးပါဘူး။
for looping ေလးနဲ့ အစဥ္လိုက္ေလး ထုတ္ေပးလိုက္တာ တစ္ခုပါပဲ။
Save ျပီး RUN လို့ ရျပီလား။ error တက္ေသးလား။ :)
ဒီေနရာမွာေတာ့ for loop ေအာက္က print ဆိုတဲ့စာလံုးကို tab တစ္ခ်က္ ေနာက္ပို့ျပီးမွ ေရးမယ္လို့ ယူဆပါတယ္။
error တက္စရာ မရွိေတာ့ပါဘူး :)
..........
ေနာက္ထပ္ တစ္ေျကာင္း ထပ္ျဖည့္ပါဦးမယ္။
...........
khitminnyo = ()
if not khitminnyo:
print "\n There is nothing in khitminnyo."
raw_input("\n Press enter to continue......")
khitminnyo = ( "Laptop1", "Books" , "1Android" , "1000Ks7" )
print "\n Khit Minnyo has the items:", khitminnyo
for item in khitminnyo:
print item
print "\n Khit Minnyo has", len(khitminnyo), "item"
raw_input("\n Press enter to exit.")
ထပ္ျဖည့္တဲ့ print ကို space နဲ့ ပို့စရာ မလိုေတာ့ပါဘူးေနာ္ :)
........
root@kmn:~# cd Desktop
root@kmn:~/Desktop# python lesson6.py
There is nothing in khitminnyo.
Press enter to continue......
Khit Minnyo has the items: ('Laptop1', 'Books', '1Android', '1000Ks7')
Laptop1
Books
1Android
1000Ks7
Khit Minnyo has 4 item
Press enter to exit.
root@kmn:~/Desktop#
Terminal ကေန ဖြင့္ျပီး ျပန္ RUN ျကည့္တဲ့အခါ ခုလို ျမင္ရပါျပီ။ အဆင္ေျပရဲ့လားခင္ဗ်။
.........
အဲသည္ ဖိုင္မွာပဲ ေနာက္ထပ္ ထပ္ျဖည့္ပါဦးမယ္။
khitminnyo = ()
if not khitminnyo:
print "\n There is nothing in khitminnyo."
raw_input("\n Press enter to continue......")
khitminnyo = ( "Laptop1", "Books" , "1Android" , "1000Ks7" )
print "\n Khit Minnyo has the items:", khitminnyo
for item in khitminnyo:
print item
for item in khitminnyo:
print "This is ", item
print "\n Khit Minnyo has", len(khitminnyo), "item"
raw_input("\n Press enter to exit.")
.............
အထက္ပါအတိုင္း ထပ္ျဖည့္ စမ္းျကည့္ပါ။ ဘာထူးျခားတာ ေတြ့ပါသလဲ။
နည္းနည္း ပိုတက္လာျပီေနာ္။
.................
ေနာက္ဆံုးအေနနဲ့ raw_input ေတြကို ထပ္ထည့္ျဖည့္သံုးပါမယ္။ ေအာက္ပါအတိုင္း ျဖစ္သြားပါျပီ။
လုပ္ျကည့္ပါဦး။
khitminnyo = ()
if not khitminnyo:
print "\n There is nothing in khitminnyo."
raw_input("\n Press enter to continue......")
khitminnyo = ( "Laptop1", "Books" , "1Android" , "1000Ks7" )
print "\n Khit Minnyo has the items:", khitminnyo
raw_input("\n Press enter to continue......")
for item in khitminnyo:
print item
raw_input("\n Press enter to continue......")
for item in khitminnyo:
print "This is ", item
raw_input("\n Press enter to continue......")
print "\n Khit Minnyo has", len(khitminnyo), "item"
raw_input("\n Press enter to exit.")
..............................................
...............................................
အေပၚက သင္ခန္းစာကို ပထမပိုင္းအျဖစ္ သတ္မွတ္လိုက္ရေအာင္
အဲသည္အပိုင္းကို ရျပီ နားလည္ျပီဆိုမွ ဒုတိယအပိုင္းကို ဆက္ျပီး ၾကည့္ေပးပါဗ်
..
.............
............
Python's String Function
Programming Language ေတြတိုင္းမွာ String ေလးေတြ ရွိျကပါတယ္။ ခုေတာ့ Python ရဲ့ String ေလးေတြကို ဆက္ျပီး ေျပာျပသြားမွာပါ။
String ဆိုတာ စာသားတန္ဖိုးေတြကို variable လိုပဲ သိုေလွာင္ပါတယ္။ ကိန္းေတြကို စာသားတန္ဖိုး ေျပာင္းလဲတာေတြကိုလည္း လုပ္ေဆာင္ေပးတဲ့ functionေပါ့။
အျကမ္းဖ်င္းေလး ရွင္းျပရင္း ဥပမာေလး ေပးပါမယ္။
python or ipython နွစ္သက္ရာမွာ ေရးျပီး စမ္းနိုင္ပါတယ္။
ကြ်န္ေတာ္ကေတာ့ စာနည္းတဲ့ python မွာပဲ စမ္းျပထားပါတယ္။ ျပန္ကူးျပတဲ့အခါ ipython က စာပိုေဖာင္းပြလို့ပါ။
>>> x = 50
>>> y = "I like the number,"
>>> x+y
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
အထက္ပါ problem အရ x ကို variable သတ္မွတ္စဥ္တုန္းက ကိန္းဂဏန္းတန္ဖိုး 50 နဲ့ သတ္မွတ္ခဲ့ပါတယ္။ y ကိုေတာ့ စာသားတန္ဖိုးအေနနဲ့ပဲ မွတ္သားခဲ့တာပါ။
Data Type မတူညီတဲ့အတြက္ သူတို့ ၂ခုကို ေပါင္းလို့ မရပါဘူး။ ဒီအခါမွာ string function လိုအပ္လာပါျပီ။
ျကားျဖတ္ ေျပာစရာတစ္ခုေတာ့ ရွိပါတယ္။ ၂ခုလံုးကို text အေနနဲ့ပဲ မွတ္လိုက္ရင္ ရတာေပါ့လို့။
>>> x = "50"
>>> y = "I like this number,"
>>> y + x
'I like this number,50'
>>>
အထက္ပါအတိုင္း ျကည့္ရင္ ရတာပါပဲ။ string ကို ဘာလို့ သံုးဖို့လိုေသးလဲ ေမးစရာျဖစ္လာနိုင္ပါတယ္။
ဆက္ျကည့္ျကပါစို့။
>>> x = "50"
>>> y = "100"
>>> x+y
'50100'
>>>
x တစ္ခုတည္း မဟုတ္ဘဲ အျခား ကိန္းေတြပါ ပါလာတဲ့အခါ text ေပါင္းသလို ေပါင္းလို့ မရေတာ့ပါဘူး။
သင္ခန္းစာရဲ့ ေရွ့ပိုင္းမွာလည္း ရွင္းျပခဲ့ျပီးပါျပီ။
ဒါဆိုရင္ string လိုအပ္လာျပီေပါ့။ ရွင္းပါတယ္ေနာ္။
အထက္ပါ ဥပမာကိုပဲ ျပန္ဆက္ရေအာင္။
အသစ္ ျပန္ေရးျကည့္မယ္ေနာ္။
>>> a = 50
>>> b = "I like the number, "
>>> a = str (a)
>>> b + a
'I like the number, 50'
>>>
တတိယ လိုင္း မွာျကည့္ရင္ မူလ a တန္ဖိုးကို ကိန္းဂဏန္းအေနနဲ့ မွတ္ထားရာကေန text အျဖစ္ ျပန္ေျပာင္းလိုက္ပါတယ္။ ဒါေျကာင့္ မတူညီတဲ့ data type ကေန တူညီတဲ့ data tpye ျဖစ္သြားပါတယ္။
ေပါင္းစပ္လို့ ရျပီေပါ့။
ေနာက္ ဥပမာ တစ္ခုေပးမယ္။
x က 50, y က 100, အဲ ၂ခု ေပါင္းလဒ္ကို စာနဲ့ ေဖာ္ျပခ်င္တယ္ဆိုပါစို့။
၂ခုေပါင္း ရလဒ္က w လို့ ထားလိုက္။ ေက်ာင္း သင္ခန္းစာ တြက္နည္းအတိုင္းပဲ ေျပာျပေနတာေနာ္။
ဒါဆို ျကည့္ရေအာင္။
>>> x = 50
>>> y = 100
>>> w = x + y
>>> z = "The result of x + y is, "
>>> w = str (w)
>>> z + w
'The result of x + y is, 150'
ျကည့္ျပီး လုပ္ျကည့္ရံုနဲ့ ရွင္းမယ္ ထင္ပါတယ္။ မရွင္းတာရွိရင္ comment ကေန လာေမးနိုင္ပါတယ္ခင္ဗ်ာ။
............
............
Integer Function
Function ေတြ ေတာ္ေတာ္မ်ားမ်ား စံုလာျပီေနာ္။ ဒီတစ္ခါေတာ့ Integer Function အေျကာင္းေလး ေျပာခ်င္ပါတယ္။
Integer Function ဆိုတာ Variable တည္ေဆာက္ထားတဲ့ စာသားနဲ့ ကိန္း အေရာေတြကေန ကိန္းဂဏန္းေတြကို ေရြးထုတ္ေပးနိုင္တဲ့ လုပ္ေဆာင္ခ်က္ကို ေဆာင္ရြက္လိုလ်င္ အသံုးျပုရေသာ function ျဖစ္ပါတယ္။
ေအာက္ပါ လုပ္ေဆာင္ခ်က္ကေလးကို ျကည့္ရေအာင္။ တစ္ေျကာင္းစီကို အရင္ ရွင္းျပပါမယ္။ ျပီးမွ အားလံုးစုျပီး ျပန္ေရးေပးမယ္။
............
present = ( 'Book' , 'Laptop' , 'Phone' , 'Shirt' )
ပထမဆံုးအေနနဲ့ လက္ေဆာင္ စာရင္းေလးကို variable လုပ္လိုက္ပါတယ္။
for item in present:
လက္ေဆာင္မွာ ပါဝင္တဲ့ item ေတြကို ကြန္ပ်ူတာသိေအာင္ ျကိုေျပာထားတဲ့သေဘာေပါ့
print item
item ေတြကို ထုတ္ခ်လိုက္ပါျပီ။ ထံုးစံအတိုင္း print အတြက္ Tab 1ခ်က္ ျခားဖို့ လိုတယ္ေနာ္။
raw_input("\n Press Enter to know each item...")
ျကားမွာ တစ္ဆင့္ျခားခ်င္လို့ ထည့္ထားတာ။ မျခားခ်င္ရင္မထည့္နဲ့ ရတယ္။
for item in present:
print "This item is ", item
This item is ဆိုျပီး တစ္ခုစီကို ေနာက္တစ္ေခါက္ ထပ္ေဖာ္ျပတာပါ။
ခုေနရာအထိ ဒီေန့အတြက္ lesson 1 မွာ ေျပာျပျပီးျပီေနာ္။ မွတ္မိေသးတယ္မလား။
index = int (raw_input("\n Type number you want ....."))
integer function စျပီဗ်။ list ထဲကေန လိုခ်င္တဲ့ present ကို နံပါတ္ ရိုက္ပါလို့ေျပာတာ။
သတိထားရမွာက ေရတြက္တဲ့အခါ သုညကေန စတယ္ေနာ္။
print "\n The present you want is ", present [index]
ေရွ့ပိုင္းမွာ ပံုမွန္အတိုင္းပဲ ေဖာ္ျပခ်င္တဲ့စာကို print လိုက္တာ။
မတူတာက အထက္မွာ ေရြးခိုင္းလို့ ေရြးတဲ့နံပါတ္ ရိုက္လိုက္တဲ့အခါထို အမွတ္စဥ္ပါတဲ့ စာသားကို ထုတ္ျပဖို့ ေနာက္မွာ index ထပ္ထည့္ထားတာပဲ။
raw_input("\n Press Enter to exit")
ဒါကေတာ့ ရွင္းပါတယ္။
..................
အထက္ပါ စာေျကာင္းေတြကို ျပန္ျကည့္ရင္ ဒီလို ရပါမယ္။
present = ( 'Book' , 'Laptop' , 'Phone' , 'Shirt' )
for item in present:
print item
raw_input("\n Press Enter to know each item...")
for item in present:
print "This item is ", item
index = int (raw_input("\n Type you want ....."))
print "\n The present you want is ", present [index]
raw_input("\n Press Enter to exit")
.......
Terminal ကေန RUN ျကည့္မယ္ဆိုရင္ေတာ့ ဒီလို ေတြ့ရပါလိမ့္မယ္။
root@kmn:~/Desktop# python lesson7a.py
Book
Laptop
Phone
Shirt
Press Enter to know each item...
This item is Book
This item is Laptop
This item is Phone
This item is Shirt
Type you want .....0 ဒီေနရာမွာ ေရြးတဲ့နံပါတ္ ရိုက္ထည့္ရမယ္။
The present you want is Book
Press Enter to exit
root@kmn:~/Desktop#
............
............
lesson ၂ခု ဆက္စပ္မွု ရွိတယ္ေနာ္။
သတိေလးထားျပီး ဆက္လက္ ေလ့က်င့္ေစလိုပါတယ္။
ဒီေနရာမွာ ထြင္တတ္ရင္ ထြင္တတ္သေလာက္ စဥ္းစားတတ္ရင္ စဥ္းစားတတ္သေလာက္ ေရးလို့ရတာေတြ အရမ္း မ်ားသြားပါျပီ။
လာေရာက္လည္ပတ္သူအေပါင္း ေပ်ာ္ရႊင္သာယာၾကပါေစ
ကြ်န္ေတာ့္ရဲ႕ Website ကေလးမွာ ေဖာ္ျပထားတဲ့ Article ေလးေတြကို က႑အလိုက္ ေရြးခ်ယ္ဖတ္ရႈ လိုပါက ဒီစာေၾကာင္းေလးကို ႏွိပ္ျပီး ေရြးခ်ယ္ႏိုင္သလို Menu box ကေနလည္း ေရြးခ်ယ္ဖတ္ရႈႏိုင္ပါတယ္ခင္ဗ်ာ
Thanks
Khit Minnyo
No comments