Adsense

OUO Banner

Latest Posts

Python2 to Python3


Python Programming
ဒီေန႔မွာေတာ့ idle နဲ႔ idle3 နဲ႔ကို တြဲဖက္ ေလ့လာသြားမွာျဖစ္ပါတယ္။
idle3 မသြင္းရေသးသူမ်ားရွိရင္ apt-get install idle3 ထည့္သြင္းထားလိုက္ပါဦး။
Kali Linux User ေတြအတြက္ကေတာ့
apt install idle
apt install idle3 ဆိုျပီး သြင္းထားဖို႔ လိုအပ္ပါမယ္
.
Windows User ေတြအတြက္ကေတာ့ Python 3 ကို ေဒါင္းယူျပီး install ရမွာျဖစ္ပါတယ္
Python for windows Download Link >> https://www.python.org/downloads/
.
Python2 မွာတုန္းက python သင္သာသင္လာတယ္ မရွင္းျပခဲ့တာေတြ ရွိပါတယ္။ အဲဒါေတြကေတာ့ python အေၾကာင္း ရွင္းမျပရေသးလို႔ပါပဲ။
..
Python ဆိုတာ Powerful Programming Language တစ္မ်ိဳးဆိုတာ အားလံုးသိၾကပါတယ္။
ခုရွင္းျပလိုတာက python version ပါ။  Terminal ကို ဖြင့္ျပီး ေအာက္ပါအတိုင္း ရိုက္ၾကည့္ပါ။
..
root@kmn:~# python
Python 2.7.14 (default, Sep 17 2017, 18:50:44)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
root@kmn:~#

..... ျပီးရင္ ဆက္ရိုက္ပါ .....

root@kmn:~# python3
Python 3.5.4 (default, Sep  5 2017, 18:32:10)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()

ဘာေတြ႔ပါသလဲ??????
Python Version ၂မ်ိဳးကို ေတြ႔ျမင္ရပါလိမ့္မယ္။
ဗားရွင္း ၂မ်ိဳးက  old version နဲ႔ new version ပဲကြာတာမလား။ လုပ္ေဆာင္ခ်က္ တူတူပဲလား။ ေမးစရာရွိပါတယ္။

အထက္ပါ terminal က ေဖာ္ျပခ်က္အရ ကြ်န္ေတာ္တို႔ စက္ထဲမွာ Python 2.7.14  နဲ႔
Python 3.5.4 ဆိုျပီး ၂မ်ိဳးလံုး ရွိေနတာ ေတြ႕ရျပန္ပါတယ္။ ထုတ္ခဲ့တဲ့ Date ေတြကို ျပန္ၾကည့္ရေအာင္။ အတူတူပဲ ေတြ႕ရပါမယ္။ ဒါဆိုရင္ ဗားရွင္း အသစ္မွာ အေဟာင္းထက္ ပိုမိုတဲ့အခ်က္ ရွိႏိုင္မယ္လို႔ ေတြးမိေလာက္ပါျပီ။
.....
ကဲ Python 2.7.13 ဆိုတဲ့ version 2 နဲ႔
Python 3.5.3 version 3 ရဲ႕ ကြာျခားခ်က္ကေလးေတြကို သိရဖို႔ IDLE နဲ႔ IDLE 3 ကိုယွဥ္ျပီး ဖြင့္လိုက္ရေအာင္။
py2&3 ဆိုတဲ့ ပူးတြဲပါ movie ထဲမွာလည္း ၾကည့္ႏိုင္ပါတယ္။
ပိုျပီး ျမင္သာေအာင္ ကြ်န္ေတာ္ story telling method နဲ႔ စမ္းျပပါ့မယ္။
.................
#!/usr/bin/python

'''
Hello
how are you
my name is Ag
'''
storyFormat = '''
Once upon a time, deep in an ancient jungle, there lived a {animal}.
This {animal} liked to eat {food}, but the jungle had very little {food}
to offer. One day, an explorer found the {animal} and discovered it liked {food}.
The explorer took the {animal} back to {city}, whrer it could eat as much {food}
as it wanted. However, the {animal} became homesick, so the explorer brought it back
to the jungle, leaving a large supply of {food}.

The End
'''

def tellstory():
    userPicks = dict()
    addPick('animal', userPicks)
    addPick('food', userPicks)
    addPick('city', userPicks)
    story = storyFormat.format(**userPicks)
    print (story)

def addPick(cue, dictionary):
    ''' Prompt for a user response using the cue string,
        and place the cue-response pair in the dictionary.'''
    prompt = 'Enter an example for ' + cue + ': '
    response = input(prompt)
    dictionary[cue] = response

tellstory()
input('Press Enter to end the program.')
.........
အထက္ပါ ကုဒ္လိုင္းေလးေတြကို idle2 ေရာ ၃ ေရာက new file ေတြမွာ ေရးျပီး စမ္းၾကည့္ရေအာင္ပါ။
story-telling movie ဖိုင္ေလးကိုလည္း ဖြင့္ၾကည့္ႏိုင္ပါတယ္။


.............
ကြ်န္ေတာ္ ေရးျပခဲ့တဲ့ Story Telling program ေလးကို ျပန္ရွင္းျပခ်င္ပါတယ္။
.......
#!/usr/bin/python
ဒါေလးကေတာ့ python ကို အသံုးျပဳ ေရးသားထားတယ္ဆိုတာကို သိေအာင္ ျဖစ္ပါတယ္။
# ခံထားလို႔ program run တဲ့အခါ ျမင္ရမွာမဟုတ္ပါဘူး

'''
Hello
how are you
my name is Ag
'''
ဒီေနရာကေတာ့ About info ေပးတာမ်ိဳးပါ။
သတိျပဳရမွာက triple code '''...''' ပံုစံ ေရးရပါတယ္။
...............................
storyFormat = '''
Once upon a time, deep in an ancient jungle, there lived a {animal}.
This {animal} liked to eat {food}, but the jungle had very little {food}
to offer. One day, an explorer found the {animal} and discovered it liked {food}.
The explorer took the {animal} back to {city}, whrer it could eat as much {food}
as it wanted. However, the {animal} became homesick, so the explorer brought it back
to the jungle, leaving a large supply of {food}.

The End
'''
ခုအပိုင္းကေတာ့ story ေဖာ္ျပတဲ့အပိုင္းပါ။ ကိုယ္ႏွစ္သက္ရာ ပံုျပင္ကို ထည့္သြင္းႏိုင္ပါတယ္။ ကြ်န္ေတာ္ကေတာ့ ပ်င္းလို႔ အတိုဆံုး ပံုျပင္ကို အသံုးျပဳထားပါတယ္။
ဒီပံုျပင္အရ ဇာတ္ေကာင္က အရွင္ျဖစ္ျပီး ဇာတ္လမ္းက အေသျဖစ္ပါတယ္။ ဂိမ္းေတြလိုပါပဲ။
RUN ၾကည့္လိုက္လို႔ animal ေနရာ dog ျဖည့္ရင္ ဒီဇာတ္လမ္းမွာ dog က ဇာတ္ေကာင္ျဖစ္သြားမွာျဖစ္ျပီး tiger ျဖည့္ရင္လည္း tiger က  ဇာတ္ေကာင္ ျဖစ္သြားမွာပါ။
city, food စတဲ့ေနရာေတြလည္း အဲလိုပါပဲ။ သတိထားၾကည့္ရင္ အစားသြင္းမယ့္ စာလံုးေတြကို {} တြန္႔ကြင္း အသံုးျပဳ ေရးထားတာေတြ႕ရမွာျဖစ္ျပီး ဇာတ္လမ္းတစ္ခုလံုးကို
'''...''' ပံုစံနဲ႔ ေရးထားတာ ေတြ႕ရပါမယ္။


...................................
def tellstory():
    userPicks = dict()
    addPick('animal', userPicks)
    addPick('food', userPicks)
    addPick('city', userPicks)
    story = storyFormat.format(**userPicks)
    print (story)
ဒီအပိုင္းကေတာ့ tellstory ကို def ဖြင့္တာပါ။ သင္ျပီးသားေတြဆိုေတာ့ သိမယ္ထင္ပါတယ္။ addPick  ဆိုတာကေတာ့ ကြ်န္ေတာ္တို႔ အစားသြင္းမယ့္စာလံုး {} နဲ႔ ေရးထားတဲ့စာေတြကို ဆိုလိုပါတယ္။
userPicks ကေတာ့ ဇာတ္ေကာင္ေတြကို user က သတ္မွတ္လို႔ ရေစတာပါ။
...............................
def addPick(cue, dictionary):
    ''' Prompt for a user response using the cue string,
        and place the cue-response pair in the dictionary.'''
    prompt = 'Enter an example for ' + cue + ': '
    response = input(prompt)
    dictionary[cue] = response
ဒီအပိုင္းကေတာ့ ကြ်န္ေတာ္တို႔ ျဖည့္သြင္းဖို႔ ပံုစံထုတ္ေပးတဲ့အပိုင္းဆိုတာ ၾကည့္လိုက္တာနဲ႔ သိမွာျဖစ္ပါတယ္။
....................
tellstory()
input('Press Enter to end the program.')
ဒီအပိုင္းကိုေတာ့ ရွင္းျပစရာ မလိုေတာ့ဘူးထင္ပါတယ္
.............
ေသခ်ာစမ္းၾကည့္ပါ။
IDLE နဲ႔ IDLE3 မွာ IDLE 3 မွာပဲ RUN လို႔ရတာကို ေတြ႔ရပါမယ္။  ဘာလို႔လဲဆိုရင္ေတာ့
ကြ်န္ေတာ္က python3 ကို အသံုးျပဳ ေရးသားထားလို႔ပါပဲ။


.............
Python 2 Vs Python 3

Python 2 အတြက္ Story Telling Program Code
.......................................................................
#!/usr/bin/python

'''
Hello
how are you
my name is Ag
'''
storyFormat = '''
Once upon a time, deep in an ancient jungle, there lived a {animal}.
This {animal} liked to eat {food}, but the jungle had very little {food}
to offer. One day, an explorer found the {animal} and discovered it liked {food}.
The explorer took the {animal} back to {city}, whrer it could eat as much {food}
as it wanted. However, the {animal} became homesick, so the explorer brought it back
to the jungle, leaving a large supply of {food}.

The End
'''

def tellstory():
    userPicks = dict()
    addPick('animal', userPicks)
    addPick('food', userPicks)
    addPick('city', userPicks)
    story = storyFormat.format(**userPicks)
    print (story)

def addPick(cue, dictionary):
    ''' Prompt for a user response using the cue string,
        and place the cue-response pair in the dictionary.'''
    prompt = 'Enter an example for ' + cue + ': '
    response = input(prompt)
    dictionary[cue] = response

tellstory()
input('Press Enter to end the program.')
........................................................................

Python 3 အတြက္ Story Telling Program Code
........................................................................

#!/usr/bin/python

'''
Hello
how are you
my name is Ag
'''
storyFormat = '''
Once upon a time, deep in an ancient jungle, there lived a {animal}.
This {animal} liked to eat {food}, but the jungle had very little {food}
to offer. One day, an explorer found the {animal} and discovered it liked {food}.
The explorer took the {animal} back to {city}, whrer it could eat as much {food}
as it wanted. However, the {animal} became homesick, so the explorer brought it back
to the jungle, leaving a large supply of {food}.

The End
'''

def tellstory():
    userPicks = dict()
    addPick('animal', userPicks)
    addPick('food', userPicks)
    addPick('city', userPicks)
    story = storyFormat.format(**userPicks)
    print (story)

def addPick(cue, dictionary):
    ''' Prompt for a user response using the cue string,
        and place the cue-response pair in the dictionary.'''
    prompt = 'Enter an example for ' + cue + ': '
    response = input(prompt)
    dictionary[cue] = response

tellstory()
input('Press Enter to end the program.')
.............................................................................

Test Movie


Thanks


Khit Minnyo



No comments