简明Python教程(重新排版打印版)


⿟ᑣDŽ ៥Ӏᇚⳟϔϟབԩ⫼ Python 㓪ݭ䖤㸠ϔϾӴ㒳ⱘĀHello Worldā⿟ᑣDŽ䗮䖛ᅗˈԴᇚᄺӮབԩ㓪ݭǃֱᄬ੠䖤㸠 Python 1. ㅔㅔҟ 14.1.1 GUIᎹ݋ὖᣀ ·························· 77 14.1 ೒ᔶ䕃ӊ········································· 76 14. ᥹ϟᴹᄺдҔМ˛················ 75 13.8 reprߑ᭄ ·········································· 75 13.7 assert䇁হ········································ 75 13.6 exec੠eval䇁হ ······························· 75 13.5.1 ᅗབԩᎹ԰ ····························· 74 13.5 lambdaᔶᓣ ····································· 74 㒘੠߫㸼·············· 73ܗ13.4 ೼ߑ᭄Ё᥹ᬊ 13.3.1 Փ⫼߫㸼㓐ড়·························· 73 13.3 ߫㸼㓐ড়········································· 73 13.2 ऩ䇁হഫ········································· 72 13.1 ⡍⅞ⱘᮍ⊩····································· 72 13. ᳈໮Pythonⱘݙᆍ················· 72 12.3 os῵ഫ············································· 71 12.2.2 ᳈໮sysⱘݙᆍ························· 71 12.2.1 ੑҸ㸠খ᭄ ····························· 69 12.2 sys῵ഫ············································ 69 12.1 ㅔҟ ················································ 69 ᑧ························· 69ޚ12. Pythonᷛ 11.4.1 Փ⫼finally······························· 68 11.4 try..finally ········································ 68 11.3.1 བԩᓩথᓖᐌ·························· 66 11.3 ᓩথᓖᐌ········································· 66 11.2.1 ໘⧚ᓖᐌ ································· 65 11.2 try..except ········································ 65 11.1 䫭䇃 ················································ 65 11. ᓖᐌ ······································· 64 ᄬ·························· 63ټᄬϢপټ 10.2.1 ᄬ఼············································· 63ټ 10.2 10.1.1 Փ⫼᭛ӊ ································· 62 10.1 ᭛ӊ ················································ 62 10. 䕧ܹ/䕧ߎ······························· 62 9.7.1 Փ⫼㒻ᡓ ································· 60 9.7 㒻ᡓ·················································59 9.6.1 Փ⫼㉏Ϣᇍ䈵ⱘব䞣···············57 9.6 ㉏Ϣᇍ䈵ⱘᮍ⊩······························56 9.5.1 Փ⫼__init__ᮍ⊩·····················55 9.5 __init__ᮍ⊩····································55 9.4.1 Փ⫼ᇍ䈵ⱘᮍ⊩······················55 9.4 ᇍ䈵ⱘᮍ⊩ ·····································55 9.3.1 ߯ᓎϔϾ㉏······························54 9.3 ㉏ ····················································54 9.2 self···················································54 9.1 ㅔҟ·················································53 9. 䴶৥ᇍ䈵ⱘ㓪⿟ ···················· 53 8.3 䕃ӊᓔথ䖛⿟··································53 8.2.5 䖯ϔℹӬ࣪······························52 8.2.4 ⠜ᴀಯ ·····································50 8.2.3 ⠜ᴀϝ ·····································49 8.2.2 ⠜ᴀѠ ·····································47 8.2.1 ⠜ᴀϔ ·····································45 ᮍḜ ·········································45އ8.2 㾷 8.1 䯂乬·················································44 8. ϔϾPythonᅲ՟····················· 44 7.7.1 ᄫヺІⱘᮍ⊩··························43 7.7 ᳈໮ᄫヺІⱘݙᆍ··························43 7.6.1 ᇍ䈵Ϣখ㗗······························42 7.6 খ㗗·················································42 7.5.1 Փ⫼ᑣ߫··································40 7.5 ᑣ߫·················································40 7.4.1 Փ⫼ᄫ݌··································38 7.4 ᄫ݌·················································38 㒘Ϣᠧॄ䇁হ······················37ܗ 7.3.2 㒘··································36ܗ⫼7.3.1 Փ 㒘·················································36ܗ 7.3 7.2.2 Փ⫼߫㸼··································34 7.2.1 ᇍ䈵Ϣ㉏ⱘᖿ䗳ܹ䮼···············34 7.2 ߫㸼·················································34 7.1 ㅔҟ·················································34 7. ᭄᥂㒧ᵘ································ 34 2 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 6.6.1 Փ⫼dirߑ᭄····························· 32 6.6 dir()ߑ᭄·········································· 32 6.5.2 from..import····························· 32 6.5.1 ߯ᓎԴ㞾Ꮕⱘ῵ഫ·················· 31 6.5 ࠊ䗴Դ㞾Ꮕⱘ῵ഫ·························· 31 6.4.1 Փ⫼῵ഫⱘ__name__·············· 30 6.4 ῵ഫⱘ__name__····························· 30 6.3 from..import䇁হ····························· 30 6.2 ᄫ㡖㓪䆥ⱘ.pyc᭛ӊ······················· 30 6.1.1 Փ⫼sys῵ഫ ···························· 29 6.1 ㅔҟ ················································ 28 6. ῵ഫ ······································· 28 5.7.2 ᅗབԩᎹ԰ ····························· 28 5.7.1 Փ⫼DocStrings························ 27 5.7 DocStrings······································· 27 5.6.2 ᅗབԩᎹ԰ ····························· 27 5.6.1 Փ⫼ᄫ䴶ᛣНϞⱘ䇁হ ·········· 26 5.6 return䇁হ ······································· 26 5.5.2 ᅗབԩᎹ԰ ····························· 26 5.5.1 Փ⫼݇䬂খ᭄·························· 26 5.5 ݇䬂খ᭄········································· 25 5.4.2 ᅗབԩᎹ԰ ····························· 25 5.4.1 Փ⫼咬䅸খ᭄ؐ······················ 25 5.4 咬䅸খ᭄ؐ····································· 24 5.3.3 Փ⫼global䇁হ························ 24 5.3.2 ᅗབԩᎹ԰ ····························· 23 5.3.1 Փ⫼ሔ䚼ব䞣·························· 23 5.3 ሔ䚼ব䞣········································· 23 5.2.2 ᅗབԩᎹ԰ ····························· 23 5.2.1 Փ⫼ߑ᭄ᔶখ·························· 22 5.2 ߑ᭄ᔶখ········································· 22 5.1.2 ᅗབԩᎹ԰ ····························· 22 5.1.1 ᅮНߑ᭄ ································· 21 5.1 ㅔҟ ················································ 21 5. ߑ᭄ ······································· 21 4.6.2 ᅗབԩᎹ԰ ····························· 21 4.6.1 Փ⫼continue䇁হ···················· 20 4.6 continue䇁হ ··································· 20 4.5.2 ᅗབԩᎹ԰ ····························· 20 4.5.1 Փ⫼break䇁হ ·························19 4.5 break䇁হ ········································19 4.4.2 ᅗབԩᎹ԰······························19 4.4.1 Փ⫼for䇁হ ·····························18 4.4 forᕾ⦃ ············································18 4.3.2 ᅗབԩᎹ԰······························18 4.3.1 Փ⫼while䇁হ ·························17 4.3 while䇁হ ········································17 4.2.2 ᅗབԩᎹ԰······························16 4.2.1 Փ⫼if䇁হ ·······························15 4.2 if䇁হ ··············································15 4.1 ㅔҟ·················································15 4. ᥻ࠊ⌕···································· 15 3.4 㸼䖒ᓣ·············································14 3.3.2 㒧ড়㾘ᕟ··································14 3.3.1 䅵ㅫ乎ᑣ··································14 㑻··································13ܜ3.3 䖤ㅫヺӬ 3.2 䖤ㅫヺ·············································11 3.1 ㅔҟ·················································11 3. 䖤ㅫヺϢ㸼䖒ᓣ ···················· 11 2.9 㓽䖯·················································11 2.8 䘏䕥㸠Ϣ⠽⧚㸠································9 2.7 ᇍ䈵···················································8 2.6 ᭄᥂㉏ൟ ···········································8 2.5 ᷛ䆚ヺⱘੑৡ····································7 2.4 ব䞣···················································7 2.3 ᄫヺІ···············································6 2.2 ᭄ ······················································6 2.1 ᄫ䴶ᛣНϞⱘᐌ䞣····························6 2. ෎ᴀὖᗉ·································· 5 1.4 㦋পᐂࡽ ···········································5 1.3 ৃᠻ㸠ⱘPython⿟ᑣ·························4 1.2.2 ᅗབԩᎹ԰˖····························4 1.2.1 䕧ߎ ···········································3 1.2 Փ⫼⑤᭛ӊ ·······································3 1.1 Փ⫼ᏺᦤ⼎ヺⱘ㾷䞞఼ ····················3 1. ㅔҟ·········································· 2 Ⳃᔩ ㅔㅔᯢ 3\WKRQ ᬭ⿟ 1 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 FSKHOORZRUOGS\KRPHVZDURRSELQKHOORZRUOG Q KRPHVZDURRSELXVU;5ELQELQXVUELQXVUORFDOELQRSWPRQRELQ HFKR3$7+ ӏԩԡ㕂䛑ৃ⫼њDŽ 䞣Ёⱘ৘ϾⳂᔩDŽ✊ৢ䖤㸠䙷Ͼ⿟ᑣDŽԴা㽕ㅔऩഄᡞ䖭Ͼ⑤᭛ӊ໡ࠊࠄ PATH ᠔߫ⳂᔩПϔህৃҹՓԴⱘ⿟ᑣ೼ 䆱ˈԴৃҹᡞԴⱘ⿟ᑣֱᄬ೼ PATH ⦃๗ব䞣ЁⱘⳂᔩПϔDŽ↣ᔧԴ䖤㸠ӏԩ⿟ᑣˈ㋏㒳Ӯᶹᡒ߫೼ PATH ⦃๗ব া㽕ⶹ䘧⿟ᑣⱘ⹂ߛԡ㕂ˈԴ⦄೼ህৃҹ䖤㸠⿟ᑣњüüԚᰃབᵰԴᏠᳯԴⱘ⿟ᑣ㛑໳Ң৘Ͼԡ㕂䖤㸠ਸ਼˛䙷ḋⱘ ಴Ў㋏㒳ⶹ䘧ᅗᖙ乏⫼⑤᭛ӊ㄀ϔ㸠ᣛᅮⱘ䙷Ͼ㾷䞞఼ᴹ䖤㸠⿟ᑣDŽ Ўњ᳈ࡴ᳝䍷ϔѯˈԴৃҹᡞԴⱘ᭛ӊৡᬍ៤ҙҙ helloworldˈ✊ ৢ 䖤 㸠 ./helloworldDŽ䖭ḋˈ䖭Ͼ⿟ᑣҡ✊ৃҹᎹ԰ˈ ӊⱘԡ㕂ᴹᠻ㸠⿟ᑣDŽ៥ӀՓ⫼./ᴹᣛ⼎⿟ᑣԡѢᔧࠡⳂᔩDŽ chmod ੑҸ⫼ᴹ ᬍব ᭛ӊⱘ ῵ᓣ ˈ㒭㋏㒳Ё᠔᳝⫼᠋䖭Ͼ⑤᭛ӊⱘᠻ㸠䆌ৃDŽ✊ৢ៥ӀৃҹⳈ᥹䗮䖛ᣛᅮ⑤᭛ +HOOR:RUOG KHOORZRUOGS\ FKPRGD[KHOORZRUOGS\ ੑҸˈ㒭⿟ᑣৃᠻ㸠ⱘ䆌ৃˈ✊ৢ 䖤㸠 ⿟ᑣDŽ ៥Ӏ䳔㽕䗮䖛 chmodܜ䖭䚼ߚݙᆍাᇍ Linux/Unix ⫼᠋䗖⫼ˈϡ䖛 Windows ⫼᠋ৃ㛑гᇍ⿟ᑣⱘ㄀ϔ㸠↨䕗ད༛DŽ佪 1.3 ৃᠻ㸠ⱘ Python ⿟ᑣ 㹿⿄ЎϔϾᄫヺІüü߿ᢙᖗ៥ӀӮ೼ৢ䴶䆺㒚㾷䞞䖭ѯᴃ䇁DŽ 䎳೼⊼䞞Пৢⱘᰃϔহ Python 䇁হ üüᅗাᰃᠧॄ᭛ᴀĀHello WorldāDŽprint ᅲ䰙ϞᰃϔϾ᪡԰ヺˈ㗠ĀHello Worldā helloworld.py ϔḋDŽ 䞞DŽ⊼ᛣˈԴᘏᰃৃҹ䗮䖛Ⳉ᥹೼ੑҸ㸠ᣛᅮ㾷䞞఼ˈҢ㗠೼ӏԩᑇৄϞ䖤㸠Դⱘ⿟ᑣDŽህབৠੑҸ python 䆺㒚㾷خ⿟ᑣDŽ䖭㸠ਞ䆝Դⱘ Linux/Unix ㋏㒳ᔧԴ ᠻ㸠 Դⱘ⿟ᑣⱘᯊ׭ˈᅗᑨ䆹䖤㸠ાϾ㾷䞞఼DŽ䖭Ӯ೼ϟϔ㡖 Python 㟇ᇥᑨᔧ᳝㄀ϔ㸠䙷ḋⱘ⡍⅞ᔶᓣⱘ⊼䞞DŽᅗ㹿⿄԰ 㒘㒛㸠 üü⑤᭛ӊⱘ༈ϸϾᄫヺᰃ#!ˈৢ䴶䎳ⴔϔϾ ⿟ᑣ䇏㗙ⱘヨ䆄DŽ 䅽៥Ӏᗱ㗗ϔϟ䖭Ͼ⿟ᑣⱘࠡϸ㸠DŽᅗӀ㹿⿄԰ ⊼䞞 üüӏԩ೼#ヺোে䴶ⱘݙᆍ䛑ᰃ⊼䞞DŽ⊼䞞Џ㽕԰Ўᦤկ㒭 1.2.2 ᅗᅗབԩᎹ԰˖ ៪㗙ࠊ㸼ヺüü៥Ӏᇚ೼ৢ䴶䅼䆎ЎҔМ䖭⚍ᰃ䞡㽕ⱘDŽ ⱘˈे print Ϣ Print ϡϔḋüü⊼ᛣࠡϔϾᰃᇣݭ p 㗠ৢϔϾᰃ໻ݭ PDŽ঺໪ˈ⹂ֱ೼↣ϔ㸠ⱘᓔྟᄫヺࠡ≵᳝ぎḐ ⹂᮴䇃 ˈ✊ৢݡ䖤㸠ϔϟ⿟ᑣDŽ⊼ᛣ Python ᰃ໻ᇣݭᬣᛳޚ ϛϔԴᕫࠄϔϾ䫭䇃ˈ䙷М䇋⹂ֱԴ䬂ܹⱘ⿟ᑣ བᵰԴᕫࠄⱘ䕧ߎϢϞ䴶᠔⼎ⱘϔḋˈ䙷Мᙁ୰ʽüüԴᏆ㒣៤ࡳഄ䖤㸠њԴⱘ㄀ϔϾ Python ⿟ᑣDŽ 4 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 +HOOR:RUOG S\WKRQKHOORZRUOGS\ 1.2.1 䕧ߎ ⫼ IDLEˈ䇋Փ⫼㦰ऩ Edit->Run Script ៪㗙Փ⫼䬂Ⲭᖿ᥋ᮍᓣ Ctrl-F5DŽ䕧ߎབϟ᠔⼎DŽ Ўњ䖤㸠䖭Ͼ⿟ᑣˈ䇋ᠧᓔ shell˄Linux 㒜ッ៪㗙 DOS ᦤ⼎ヺ˅ˈ✊ৢ䬂ܹੑҸ python helloworld.pyDŽབᵰԴՓ  +HOOR:RUOG SULQW KHOORZRUOGS\)LOHQDPH XVUELQS\WKRQ ՟ 1.2 Փ⫼⑤᭛ӊ ਃࡼԴ䗝ᢽⱘ㓪䕥఼ˈ䕧ܹϟ䴶䖭↉⿟ᑣˈ✊ৢᡞᅗֱᄬЎ helloworld.pyDŽ ᦤߎĀHello Worldā⿟ᑣⱘ Simon Cozens[1]᠔䇈˖Āᅗᰃ㓪⿟П⼲ⱘӴ㒳੦䇁ˈৃҹᐂࡽԴ᳈དⱘᄺд䇁㿔DŽā ⱘџাᰃ䇈ໄ˖ĀHello WorldāDŽℷབخ⿟ᑣˈ䖭Ꮖ㒣៤Ўϔ⾡Ӵ㒳њDŽ೼Դ䖤㸠ĀHello Worldā⿟ᑣⱘᯊ׭ˈᅗ᠔ ⦄೼䅽៥Ӏ䞡ᮄᓔྟ㓪⿟DŽᔧԴᄺдϔ⾡ᮄⱘ㓪⿟䇁㿔ⱘᯊ׭ˈԴ㓪ݭ䖤㸠ⱘ㄀ϔϾ⿟ᑣ䗮ᐌ䛑ᰃĀHello Worldā 1.2 Փ⫼⑤᭛ӊ བᵰԴՓ⫼ⱘᰃ Linux/BSD shellˈ䙷Мᣝ Ctrl-d 䗔ߎᦤ⼎ヺDŽབᵰᰃ೼ Windows ੑҸ㸠Ёˈ߭ᣝ Ctrl-z ݡᣝ EnterDŽ བԩ䗔ߎ Python ᦤ⼎ヺ Դᦤկ㒭ᅗⱘؐDŽ䖭䞠ˈ៥Ӏᦤկⱘᰃ᭛ᴀ Hello Worldˈᅗ㹿䖙䗳ഄᠧॄ೼ሣᐩϞDŽ ⊼ᛣˈPython Ӯ೼ϟϔ㸠ゟे㒭ߎԴ䕧ߎʽԴ߮ᠡ䬂ܹⱘᰃϔহ Python 䇁হ DŽ៥ӀՓ⫼ print˄ϡ㽕᚞䆊˅ᴹᠧॄ !!! KHOORZRUOG  KHOORZRUOG !!!SULQW ՟ 1.1 Փ⫼ᏺᦤ⼎ヺⱘ Python 㾷䞞఼ ⊼ᛣˈ>>>ᰃԴ䬂ܹ Python 䇁হⱘᦤ⼎ヺDŽ ⿟ᑣDŽIDLE ᰃ䲚៤ᓔথ⦃๗ⱘ㓽ݭDŽ⚍ߏᓔྟ->⿟ᑣ->Python 2.3->IDLE(Python GUI)DŽLinux ⫼᠋гৃҹՓ⫼ IDLEDŽ ᇍѢ Windows ⫼᠋ˈা㽕Դℷ⹂ⱘ䆒㕂њ PATH ব䞣ˈԴᑨ䆹ৃҹҢੑҸ㸠ਃࡼ㾷䞞఼DŽ៪㗙Դৃҹ䗝ᢽՓ⫼ IDLE ࠄ䕧ߎⱘऩ䆡 Hello WorldDŽ ೼ੑҸ㸠ⱘ shell ᦤ⼎ヺϟ䬂ܹ pythonˈਃࡼ㾷䞞఼DŽ⦄೼䕧ܹ print 'Hello World'ˈ✊ৢᣝ Enter 䬂DŽԴᑨ䆹ৃҹⳟ 1.1 ՓՓ⫼ᏺᦤ⼎ヺⱘ㾷䞞఼ ᳝ϸ⾡Փ⫼ Python 䖤㸠Դⱘ⿟ᑣⱘᮍᓣüüՓ⫼ѸѦᓣⱘᏺᦤ⼎ヺⱘ㾷䞞఼៪Փ⫼⑤᭛ӊDŽ៥Ӏᇚᄺд䖭ϸ⾡ᮍ⊩ 3 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ঺ϔϾ㸼⼎䖭Ͼ⡍߿ⱘᄫヺІⱘᮍ⊩ᰃ“What's your name?“ˈे⫼ঠᓩোDŽ㉏Ԑഄˈ㽕೼ঠᓩোᄫヺІЁՓ⫼ঠᓩো ড᭰ᴴDŽ⦄೼ԴৃҹᡞᄫヺІ㸼⼎Ў'What\'s your name?'DŽ ҹˈԴ䳔㽕ᣛᯢऩᓩো㗠ϡᰃᄫヺІⱘ㒧ሒDŽৃҹ䗮䖛 䕀Нヺ ᴹᅠ៤䖭ϾӏࡵDŽԴ⫼\'ᴹᣛ⼎ऩᓩোüü⊼ᛣ䖭Ͼ name?DŽԴ㚃ᅮϡӮ⫼'What's your name?'ᴹᣛ⼎ᅗˈ಴Ў Python Ӯᓘϡᯢⱑ䖭ϾᄫヺІҢԩ໘ᓔྟˈԩ໘㒧ᴳDŽ᠔ 䆒Դᛇ㽕೼ϔϾᄫヺІЁࣙ৿ϔϾऩᓩো˄'˅ˈ䙷МԴ䆹ᗢМᣛ⼎䖭ϾᄫヺІ˛՟བˈ䖭ϾᄫヺІᰃ What's your؛  䕀Нヺ  +HVDLG%RQG-DPHV%RQG V\RXUQDPH“,DVNHG :KDW 7KLVLVWKHVHFRQGOLQH 7KLVLVDPXOWLOLQHVWULQJ7KLVLVWKHILUVWOLQH ߽⫼ϝᓩোˈԴৃҹᣛ⼎ϔϾ໮㸠ⱘᄫヺІDŽԴৃҹ೼ϝᓩোЁ㞾⬅ⱘՓ⫼ऩᓩো੠ঠᓩোDŽ՟བ˖  Փ⫼ϝᓩো˄'''៪“““˅ ೼ঠᓩোЁⱘᄫヺІϢऩᓩোЁⱘᄫヺІⱘՓ⫼ᅠܼⳌৠˈ՟བ“What's your name?“DŽ  Փ⫼ঠᓩো˄“˅ Դৃҹ⫼ऩᓩোᣛ⼎ᄫヺІˈህབৠ'Quote me on this'䖭ḋDŽ᠔᳝ⱘぎⱑˈेぎḐ੠ࠊ㸼ヺ䛑✻ॳḋֱ⬭DŽ  Փ⫼ऩᓩো˄'˅ Python ЁՓ⫼ᄫヺІDŽ ៥޴Тৃҹֱ䆕Դ೼↣Ͼ Python ⿟ᑣЁ䛑㽕⫼ࠄᄫヺІˈ᠔ҹ䇋⡍߿⬭ᖗϟ䴶䖭䚼ߚⱘݙᆍDŽϟ䴶ਞ䆝Դབԩ೼ ᄫヺІᰃ ᄫヺⱘᑣ߫ DŽᄫヺІ෎ᴀϞህᰃϔ㒘ऩ䆡DŽ 2.3 ᄫヺІ  (-5+4j)੠(2.3-4.6j)ᰃ໡᭄ⱘ՟ᄤDŽ  3.23 ੠ 52.3E-4 ᰃ⍂⚍᭄ⱘ՟ᄤDŽE ᷛ䆄㸼⼎ 10 ⱘᐖDŽ೼䖭䞠ˈ52.3E-4 㸼⼎ 52.3 * 10-4DŽ  䭓ᭈ᭄ϡ䖛ᰃ໻ϔѯⱘᭈ᭄DŽ  2 ᰃϔϾᭈ᭄ⱘ՟ᄤDŽ ೼ Python Ё᳝ 4 ⾡㉏ൟⱘ᭄üüᭈ᭄ǃ䭓ᭈ᭄ǃ⍂⚍᭄੠໡᭄DŽ 2.2 ᭄ ⱘᐌ䞣DŽ 㸼ᅗ㞾Ꮕˈ㗠ϡӮᰃ߿ⱘҔМϰ㽓üüᅗᰃϔϾᐌ䞣ˈ಴Ўϡ㛑ᬍবᅗⱘؐDŽ಴ℸˈ᠔᳝䖭ѯ䛑㹿⿄Ўᄫ䴶ᛣНϞ ІDŽᅗӀ㹿⿄԰ᄫ䴶ᛣНϞⱘˈ಴ЎᅗӀ݋໛ ᄫ䴶 ⱘᛣНüüԴᣝ✻ᅗӀⱘᄫ䴶ᛣНՓ⫼ᅗӀⱘؐDŽ᭄ 2 ᘏᰃҷ ϔϾᄫ䴶ᛣНϞⱘᐌ䞣ⱘ՟ᄤᰃབৠ 5ǃ1.23ǃ9.25e-3 䖭ḋⱘ᭄ˈ៪㗙བৠ'This is a string'ǃ“It's a string!“䖭ḋⱘᄫヺ 2.1 ᄫᄫ䴶ᛣНϞⱘᐌ䞣 6 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ϔѯ䕧ߎDŽ೼ Python Ёˈ៥ӀৃҹՓ⫼ᐌ䞣੠ব䞣ᴹᅠ៤䖭ѯᎹ԰DŽ ᪡԰ˈݡҢЁᕫࠄخ᳈໮ⱘџüüԴᛇ㽕ᕫࠄϔѯ䕧ܹˈ✊ৢخҙҙᠧॄĀHello Worldāህ䎇໳њ৫˛Դᑨ䆹ᛇ㽕 2. ෎ᴀὖᗉ ⊼ᛣˈ៥Փ⫼ⱘԡ㕂ᰃ೼ Fedora Core 3 Linux Ёⱘԡ㕂üüᅗৃ㛑೼ϡৠⱘথ㸠⠜੠⠜ᴀЁ᳝᠔ϡৠDŽ 㽕ᅗᠧॄϰ㽓DŽ Դᑨ䆹⊼ᛣࠄ៥⡍ᛣ೼ĀprintāϞՓ⫼њᓩোˈ䙷ḋ Python ህৃҹ⧚㾷៥ᰃᏠᳯ㦋প݇ѢĀprintāⱘᐂࡽ㗠ϡᰃᛇ  SULQW !!!KHOS 7\SHKHOSFRS\ULJKWFUHGLWVRUOLFHQVHIRUPRUHLQIRUPDWLRQ 5HG+DWIF @RQOLQX[ >*&&  2FW 3\WKRQ HQY3<7+21'2&6 XVUVKDUHGRFS\WKRQGRFVKWPOS\WKRQ Linux/Unix Ё䕏ᵒഄ䗮䖛 env ੑҸᅠ៤DŽ བᵰԴᛇ㽕㦋প݇Ѣབ print 䙷ḋ᪡԰ヺⱘᐂࡽˈ䙷МԴ䳔㽕ℷ⹂ⱘ䆒㕂 PYTHONDOCS ⦃๗ব䞣DŽ䖭ৃҹ೼ ㉏ԐഄˈԴৃҹ㦋প Python Ё޴Т᠔᳝ϰ㽓ⱘֵᙃDŽՓ⫼ help()এᄺд᳈໮݇Ѣ help ᴀ䑿ⱘϰ㽓ʽ ᣝ T 䗔ߎᐂࡽDŽ ⊼䞞 ˄ᄫヺІ˅DŽ㉏ᇚ೼ৢ䴶䴶৥ᇍ䈵㓪⿟ⱘゴ㡖䆺㒚㾷䞞DŽ Ҹ㸠ⱘᯊ׭ˈᅗकߚ᳝⫼DŽ↨བˈ䖤㸠 help(str)üü䖭Ӯᰒ⼎ str ㉏ⱘᐂࡽDŽstr ㉏⫼ѢֱᄬԴⱘ⿟ᑣՓ⫼ⱘ৘⾡᭛ᴀ བᵰԴ䳔㽕ᶤϾ Python ߑ᭄៪䇁হⱘᖿ䗳ֵᙃᐂࡽˈ䙷МԴৃҹՓ⫼ݙᓎⱘ help ࡳ㛑DŽᇸ݊೼ԴՓ⫼ᏺᦤ⼎ヺⱘੑ 1.4 㦋㦋পᐂࡽ ݊Ҫ Linux 㒜ッ៪ DOS ᦤ⼎ヺੑҸ䙷ḋDŽ ߯䗴Դ㞾ᏅⱘᣛҸˈབৠ cd ៪ڣᔧԴᛇ㽕೼ӏԩᯊ䯈ǃӏԩഄᮍ䖤㸠Դⱘ⿟ᑣⱘᯊ׭ˈ䖭Ͼᮍ⊩कߚ᳝⫼DŽᅗህད PATH=$PATH:/home/swaroop/mydir ᅠ៤ˈ݊ЁĀ/home/swaroop/mydirāᰃ៥ᛇ㽕⏏ࡴࠄ PATH ব䞣ЁⱘⳂᔩDŽ ϾⳌԐⱘⳂᔩDŽԴгৃҹᡞԴ䗝ᢽⱘⳂᔩ⏏ࡴࠄ PATH ব䞣Ёএüü䖭ৃҹ䗮䖛䖤㸠 /home/swaroop/bin ᰃ PATH ব䞣ЁⱘⳂᔩПϔDŽswaroop ᰃ៥ⱘ㋏㒳ЁՓ⫼ⱘ⫼᠋ৡDŽ䗮ᐌˈ೼Դⱘ㋏㒳ЁгӮ᳝ϔ ៥Ӏ㛑໳⫼ echo ੑҸᴹᰒ⼎ PATH ব䞣ˈ⫼$㒭ব䞣ৡࡴࠡ㓔ҹ৥ shell 㸼⼎៥Ӏ䳔㽕䖭Ͼব䞣ⱘؐDŽ៥Ӏⳟࠄ +HOOR:RUOG KHOORZRUOG 5 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 SULQWL L L SULQWL  L YDUS\)LOHQDPH ՟ 2.1 Փ⫼ব䞣੠ᄫ䴶ᛣНϞⱘᐌ䞣 ࠡҟ㒡ⱘৃᠻ㸠ⱘᮍ⊩DŽ ܜ⫼䖤㸠㾷䞞఼ੑҸ S\WKRQSURJUDPS\ ៪㗙Փ⫼ ,'/( 䖤㸠⿟ᑣDŽԴгৃҹՓ 䛑ҹᠽሩৡS\ ֱᄬDŽ ⫼⊼䞞Ё㒭ߎⱘ᭛ӊৡᡞᅗֱᄬЎϔϾ᭛ӊDŽ៥ᣝ✻ᛃ՟ᡞ᠔᳝ⱘ 3\WKRQ ⿟ᑣ 䕧ܹ՟ᄤЁⱘ⿟ᑣҷⷕDŽ ᠧᓔԴ᳔୰⃶ⱘ㓪䕥఼DŽ ⿟DŽ⌕ޚϟ䴶ᰃֱᄬ੠䖤㸠 3\WKRQ ⿟ᑣⱘᷛ བԩ㓪ݭ 3\WKRQ ⿟ᑣ ៥ӀᇚⳟϔϟབԩՓ⫼ব䞣੠ᄫ䴶ᛣНϞⱘᐌ䞣DŽֱᄬϟ䴶䖭Ͼ՟ᄤˈ✊ৢ䖤㸠⿟ᑣDŽ ᇍ䈵ⱘDŽ ህ↣ϔϾϰ㽓ࣙᣀ᭄ǃᄫヺІ⫮㟇ߑ᭄䛑ᰃᇍ䈵䖭ϔ⚍ᴹ䇈ˈ3\WKRQ ᰃᵕ݊ᅠܼഄ䴶৥ 㒭䴶৥ᇍ䈵㓪⿟⫼᠋ⱘ⊼䞞 䇈ĀᶤϾ ᇍ䈵 āDŽ 䆄ԣˈPython ᡞ೼⿟ᑣЁ⫼ࠄⱘӏԩϰ㽓䛑⿄Ў ᇍ䈵 DŽ䖭ᰃҢᑓНϞ䇈ⱘDŽ಴ℸ៥ӀϡӮ䇈Āᶤᶤ ϰ㽓 āˈ៥Ӏ 2.7 ᇍ䈵 䴶ˈ៥ӀӮⷨおᗢМ⫼㉏߯䗴៥Ӏ㞾Ꮕⱘ㉏ൟDŽ ব䞣ৃҹ໘⧚ϡৠ㉏ൟⱘؐˈ⿄Ў᭄᥂㉏ൟDŽ෎ᴀⱘ㉏ൟᰃ᭄੠ᄫヺІˈ៥ӀᏆ㒣䅼䆎䖛ᅗӀњDŽ೼ৢ䴶ⱘゴ㡖䞠 2.6 ᭄᭄᥂㉏ൟ  ᮴ᬜ ᷛ䆚ヺৡ⿄ⱘ՟ᄤ᳝ 2thingsǃthis is spaced out ੠ my-nameDŽ  ᳝ᬜ ᷛ䆚ヺৡ⿄ⱘ՟ᄤ᳝ iǃ__my_nameǃname_23 ੠ a1b2_c3DŽ ໻ݭ NDŽ  ᷛ䆚ヺৡ⿄ᰃᇍ໻ᇣݭᬣᛳⱘDŽ՟བˈmyname ੠ myName ϡᰃϔϾᷛ䆚ヺDŽ⊼ᛣࠡ㗙Ёⱘᇣݭ n ੠ৢ㗙Ёⱘ  ᷛ䆚ヺৡ⿄ⱘ݊Ҫ䚼ߚৃҹ⬅ᄫ↡˄໻ݭ៪ᇣݭ˅ǃϟߦ㒓˄þ _ ÿ˅៪᭄ᄫ˄0-9˅㒘៤DŽ 8 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  ᷛ䆚ヺⱘ㄀ϔϾᄫヺᖙ乏ᰃᄫ↡㸼Ёⱘᄫ↡˄໻ݭ៪ᇣݭ˅៪㗙ϔϾϟߦ㒓˄þ _ ÿ˅DŽ ব䞣ᰃᷛ䆚ヺⱘ՟ᄤDŽ ᷛ䆚ヺ ᰃ⫼ᴹᷛ䆚 ᶤḋϰ㽓 ⱘৡᄫDŽ೼ੑৡᷛ䆚ヺⱘᯊ׭ˈԴ㽕䙉ᕾ䖭ѯ㾘߭˖ 2.5 ᷛ䆚ヺⱘੑৡ Դ㒭ব䞣ৡᄫDŽ ᙃⱘϔ䚼ߚݙᄬDŽϢᄫ䴶ᛣНϞⱘᐌ䞣ϡৠˈԴ䳔㽕ϔѯ㛑໳䆓䯂䖭ѯব䞣ⱘᮍ⊩ˈ಴ℸֵټাᰃԴⱘ䅵ㅫᴎЁᄬ ӏԩϰ㽓DŽব䞣ټᰃЎҔМ㽕ᓩܹ ব䞣 DŽব䞣ህᰃ៥Ӏᛇ㽕ⱘϰ㽓üüᅗӀⱘؐৃҹব࣪ˈेԴৃҹՓ⫼ব䞣ᄬ ᄬֵᙃ জৃҹᇍᅗӀ䖯㸠᪡԰ⱘᮍ⊩DŽ䖭ټҙҙՓ⫼ᄫ䴶ᛣНϞⱘᐌ䞣ᕜᖿህӮᓩথ⚺ᙐüü៥Ӏ䳔㽕ϔ⾡᮶ৃҹ 2.4 ব䞣 DŽ ? ៪ U ?? ヺৃҹݭ៤ ϔᅮ㽕⫼㞾✊ᄫヺІ໘⧚ℷ߭㸼䖒ᓣDŽ৺߭Ӯ䳔㽕Փ⫼ᕜ໮ⱘড᭰ᴴDŽ՟བˈৢ৥ᓩ⫼ 㒭ℷ߭㸼䖒ᓣ⫼᠋ⱘ⊼䞞 “What's your name?“DŽ བᵰԴᡞϸϾᄫヺІᣝᄫ䴶ᛣНⳌ䚏ᬒⴔˈҪӀӮ㹿 Python 㞾ࡼ㑻䖲DŽ՟བˈ'What\'s' 'your name?'Ӯ㹿㞾ࡼ䕀Ў  ᣝᄫ䴶ᛣН㑻䖲ᄫヺІ Ӯ೼ৢ䴶ⱘ⿟ᑣЁⳟࠄЎҔМ៥Ӏ䇈ᅗϡᰃϔϾ㔎⚍DŽ ᰃϔӊണџˈԚᅲ䰙ϞᅗϡᰃDŽ៥Ӏᇚڣ䖭ᛣੇⴔϔᮺԴ߯䗴њϔϾᄫヺІˈԴህϡ㛑ݡᬍবᅗњDŽ㱑✊䖭ⳟ䍋ᴹ  ᄫᄫヺІᰃϡৃবⱘ 䆄ԣˈ೼Դ໘⧚᭛ᴀ᭛ӊⱘᯊ׭Փ⫼ Unicode ᄫヺІˈ⡍߿ᰃᔧԴⶹ䘧䖭Ͼ᭛ӊ৿᳝⫼䴲㣅䇁ⱘ䇁㿔ݭⱘ᭛ᴀDŽ is a Unicode string.“DŽ 䆌Դ໘⧚ Unicode ᭛ᴀüüԴা䳔㽕೼ᄫヺІࠡࡴϞࠡ㓔 u ៪ UDŽ՟བˈu“Thisܕ Unicode ⱘ㓪䕥఼DŽ㉏ԐഄˈPython ᮍ⊩DŽབᵰԴᛇ㽕⫼Դⱘ↡䇁བ࣫ॄᑺ䇁៪䰓ᢝԃ䇁ݭ᭛ᴀˈ䙷МԴ䳔㽕᳝ϔϾᬃᣕޚUnicode ᰃкݭ೑䰙᭛ᴀⱘᷛ  Unicode ᄫヺІ 㒭ᄫヺІࡴϞࠡ㓔 r ៪ R ᴹᣛᅮDŽ՟བ r“Newlines are indicated by \n“DŽ བᵰԴᛇ㽕ᣛ⼎ᶤѯϡ䳔㽕བ䕀Нヺ䙷ḋⱘ⡍߿໘⧚ⱘᄫヺІˈ䙷МԴ䳔㽕ᣛᅮϔϾ㞾✊ᄫヺІDŽ㞾✊ᄫヺІ䗮䖛  㞾✊ᄫヺІ ㄝӋѢ“This is the first sentence. This is the second sentence.“ 7KLVLVWKHVHFRQGVHQWHQFH 7KLVLVWKHILUVWVHQWHQFH? ՟བ˖ ؐᕫ⊼ᛣⱘϔӊџᰃˈ೼ϔϾᄫヺІЁˈ㸠᳿ⱘऩ⣀ϔϾড᭰ᴴ㸼⼎ᄫヺІ೼ϟϔ㸠㒻㓁ˈ㗠ϡᰃᓔྟϔϾᮄⱘ㸠DŽ ࡽѢ䕀НヺDŽ঺໪ˈԴৃҹ⫼䕀Нヺ\\ᴹᣛ⼎ড᭰ᴴᴀ䑿DŽ׳ᴀ䑿ⱘᯊ׭ˈгৃҹ 7 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ᣀোⱘᯊ׭DŽ䖭㹿⿄Ўᱫ⼎ⱘ㸠䖲᥹DŽԴӮ೼ৢ䴶ҟ㒡བԩՓ⫼߫㸼ⱘゴ㡖Ёⳟࠄ䖭⾡⫼⊩DŽ ߎ⦄೼䘏䕥㸠ЁՓ⫼њ೚ᣀোǃᮍᣀো៪⊶ᔶމ䆒ˈৃҹՓԴϡ䳔㽕Փ⫼ড᭰ᴴDŽ䖭⾡ᚙ؛᳝ᯊ׭ˈ᳝ϔ⾡ᱫ⼎ⱘ SULQWL Ϣབϟݭ⊩ᬜᵰⳌৠ˖ L SULQW? ㉏Ԑഄˈ 7KLVLVDVWULQJ7KLVFRQWLQXHVWKHVWULQJ ᅗⱘ䕧ߎ˖ SULQWV  7KLVFRQWLQXHVWKHVWULQJ 7KLVLVDVWULQJ?  V ϟ䴶ᰃϔϾ೼໮Ͼ⠽⧚㸠ЁݭϔϾ䘏䕥㸠ⱘ՟ᄤDŽᅗ㹿⿄Ўᯢᯢ⹂ⱘ㸠䖲᥹DŽ ࠄ䖛ߚোDŽ Փ⫼ߚোˈҢ㗠䅽ҷⷕ᳈ࡴᯧ䇏DŽџᅲϞˈ៥ Ңᴹ≵᳝ ೼ Python ⿟ᑣЁՓ⫼䖛៪ⳟܡ㸠DŽ䖭ѯ䛑ᰃЎњሑৃ㛑䙓 ✊㗠ˈ៥ᔎᔎ⚜ᓎ䆂Դമᣕ೼↣↣Ͼ⠽⧚㸠াݭϔহ䘏䕥㸠DŽҙҙᔧ䘏䕥㸠໾䭓ⱘᯊ׭ˈ೼໮ѢϔϾ⠽⧚㸠ݭϔϾ䘏䕥 SULQWL L ⫮㟇ৃҹݭ៤˖ SULQWL L ৠḋгৃҹݭ៤˖ SULQWL  L 10 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 Ϣϟ䴶䖭ϾⳌৠ˖ SULQWL  L 䘏䕥㸠/䇁হⱘ㒧ᴳDŽ՟བ˖ བᵰԴᛇ㽕೼ϔϾ⠽⧚㸠ЁՓ⫼໮ѢϔϾ䘏䕥㸠ˈ䙷МԴ䳔㽕Փ⫼ߚো˄;˅ᴹ⡍߿ഄᷛᯢ䖭⾡⫼⊩DŽߚো㸼⼎ϔϾ 咬䅸ഄˈPython Ꮰᳯ↣㸠䛑াՓ⫼ϔϾ䇁হˈ䖭ḋՓᕫҷⷕ᳈ࡴᯧ䇏DŽ ᰃϔϾ⠽⧚㸠DŽ Դ೼㓪䕥఼Ёⳟࠄⱘ䙷ḋ˅ˈ䙷Мᅗгڣ䘏䕥㸠ⱘ՟ᄤབ print 'Hello World'䖭ḋⱘ䇁হüüབᵰᅗᴀ䑿ህᰃϔ㸠˄ህ 㸠DŽ ᅮ↣Ͼ ⠽⧚㸠 ᇍᑨϔϾ 䘏䕥؛ ⠽⧚㸠ᰃԴ೼㓪ݭ⿟ᑣᯊ᠔ ⳟ㾕 ⱘDŽ䘏䕥㸠ᰃ Python ⳟ㾕 ⱘऩϾ䇁হDŽPython 2.8 䘏䘏䕥㸠Ϣ⠽⧚㸠 Փ⫼ব䞣ᯊা䳔㽕㒭ᅗӀ䌟ϔϾؐDŽϡ䳔㽕ໄᯢ៪ᅮН᭄᥂㉏ൟDŽ 㒭 &&⿟ᑣਬⱘ⊼䞞 ㉏Ԑഄˈ៥ӀᡞϔϾᄫ䴶ᛣНϞⱘᄫヺІ䌟㒭ব䞣 s ✊ৢᠧॄᅗDŽ ⱘؐࡴ 1ˈݡᡞᅗᄬಲ iDŽ៥Ӏᠧॄᅗᯊˈᕫࠄᳳᳯⱘؐ 6DŽټ✊ৢ៥Ӏᇍ i Ёᄬ হᠧॄ i ⱘؐˈህᰃᡞব䞣ⱘؐᠧॄ೼ሣᐩϞDŽ ᶤӊџᚙˈ೼䖭Ͼഄᮍ៥Ӏᡞব䞣ৡ i Ϣؐ 5 䖲᥹೼ϔ䍋DŽ᥹ϟᴹˈ៥Ӏ⫼ print 䇁خ⿄ЎϔϾ䇁হDŽ䇁হໄᯢ䳔㽕 ៥ӀՓ⫼䌟ؐ䖤ㅫヺ˄=˅ᡞϔϾᄫ䴶ᛣНϞⱘᐌ᭄ 5 䌟㒭ব䞣 iDŽ䖭ϔ㸠ܜϟ䴶ᴹ䇈ᯢϔϟ䖭Ͼ⿟ᑣབԩᎹ԰DŽ佪 ᅗབԩᎹ԰ 7KLVLVWKHVHFRQGOLQH 7KLVLVDPXOWLOLQHVWULQJ   S\WKRQYDUS\ 䕧ߎ SULQWV  7KLVLVWKHVHFRQGOLQH 7KLVLVDPXOWLOLQHVWULQJ  V  9 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  \ 䖨ಲ 7UXHDŽ[ \   ᇣѢㄝѢ䖨ಲ [ ᰃ৺ᇣѢㄝѢ \ [ ৺߭ˈᅗᘏᰃ䖨ಲ )DOVHDŽ 㹿䕀ᤶЎϔϾ݅ৠⱘ㉏ൟDŽܜᄫˈᅗӀ佪 ! 䖨ಲ 7UXHDŽབᵰϸϾ᪡԰᭄䛑ᰃ᭄ ! ໻Ѣ 䖨ಲ [ ᰃ৺໻Ѣ \  䖨ಲ 7UXHDŽ ˄े 7UXH˅DŽ↨䕗ৃҹ㹿ӏᛣ䖲᥹˖   䖨ಲ  䖨ಲ ˄े )DOVH˅㗠 䖭ѯব䞣ৡⱘ໻ݭDŽ ⡍⅞ⱘব䞣 7UXH ੠ )DOVH ㄝӋDŽ⊼ᛣˈ DŽ䖭ߚ߿Ϣ؛⼎ಲ  㸼⼎ⳳˈ䖨ಲ  㸼 䖨ಲ [ ᰃ৺ᇣѢ \DŽ᠔᳝↨䕗䖤ㅫヺ䖨  ᇣѢ [  a ᕫࠄ DŽ a ᣝԡ㗏䕀[ ⱘᣝԡ㗏䕀ᰃ  A ᣝԡᓖ៪᭄ⱘᣝԡᓖ៪ A ᕫࠄ _ ᣝԡ៪ ᭄ⱘᣝԡ៪ _ ᕫࠄ DŽ  ᕫࠄ DŽ   ᣝԡϢ ᭄ⱘᣝԡϢ 䖯ࠊⱘ DŽ ˈ৥ে⿏ࡼ  ↨⡍ৢᕫࠄ ˈेक !! ᕫࠄ DŽüü ᣝ↨⡍㸼⼎Ў !! ে⿏ ᡞϔϾ᭄ⱘ↨⡍৥ে⿏ϔᅮ᭄Ⳃ   ᕫࠄ DŽüü ᣝ↨⡍㸼⼎Ў ᄫˈे  ੠ ˅ ᭄೼ݙᄬЁ䛑㸼⼎Ў↨⡍៪Ѡ䖯ࠊ᭄ ᡞϔϾ᭄ⱘ↨⡍৥Ꮊ⿏ϔᅮ᭄Ⳃ˄↣Ͼ  Ꮊ⿏   প῵ 䖨ಲ䰸⊩ⱘԭ᭄  ᕫࠄ DŽ ᕫࠄ   পᭈ䰸 䖨ಲଚⱘᭈ᭄䚼ߚ  ᕫࠄ   ៪  ᕫࠄ  䰸 [ 䰸ҹ \  ᕫࠄ ˄ᭈ᭄ⱘ䰸⊩ᕫࠄᭈ᭄㒧ᵰ˅DŽ ˅     ᕫࠄ ˄े   ᐖ 䖨ಲ[ⱘ\⃵ᐖ DŽ ODODOD  ᕫࠄ  OD  ᕫࠄ DŽ  ⃵ⱘᄫヺІ  Ь ϸϾ᭄ⳌЬ៪ᰃ䖨ಲϔϾ㹿䞡໡㢹ᑆ এ঺ϔϾ᭄  ᕫࠄϔϾ䋳᭄DŽ ᕫࠄ DŽޣᕫࠄ䋳᭄៪ᰃϔϾ᭄ ޣ  DŽ DE ᕫࠄ E  D  ࡴ ϸϾᇍ䈵Ⳍࡴ  ᕫࠄ DŽ 䖤ㅫヺ ৡ⿄ 䇈ᯢ ՟ᄤ 㸼 3.1 䖤ㅫヺϢᅗӀⱘ⫼⊩ !!!   !!!  !!! Python 㾷䞞఼˖ ԴৃҹѸѦഄՓ⫼㾷䞞఼ᴹ䅵ㅫ՟ᄤЁ㒭ߎⱘ㸼䖒ᓣDŽ՟བˈЎњ⌟䆩㸼䖒ᓣ 2 + 3ˈՓ⫼ѸѦᓣⱘᏺᦤ⼎ヺⱘ ᡔᡔᎻ 12 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ៥Ӏᇚㅔऩ⌣㾜ϔϟ䖤ㅫヺ੠ᅗӀⱘ⫼⊩˖ 3.2 䖤ㅫヺ ԰᭄DŽ Դ㓪ݭⱘ໻໮᭄䇁হ˄䘏䕥㸠˅䛑ࣙ৿㸼䖒ᓣDŽϔϾㅔऩⱘ㸼䖒ᓣ՟ᄤབ 2 + 3DŽϔϾ㸼䖒ᓣৃҹߚ㾷Ў䖤ㅫヺ੠᪡ 3.1 ㅔҟ 3. 䖤ㅫヺϢ㸼䖒ᓣ ⫼䖭ϔ⾡亢ḐDŽ 䗝ᢽ䖭ϝ⾡㓽䖯亢ḐПϔDŽ᳈ࡴ䞡㽕ⱘᰃˈ䗝ᢽϔ⾡亢Ḑˈ✊ৢϔ䌃ഄՓ⫼ᅗˈेাՓ ៥ᔎ⚜ᓎ䆂Դ೼↣Ͼ㓽䖯ሖ⃵Փ⫼ऩϾࠊ㸼ヺ៪ϸϾ៪ಯϾぎḐDŽ ϡ㽕⏋ড়Փ⫼ࠊ㸼ヺ੠ぎḐᴹ㓽䖯ˈ಴Ў䖭೼䎼䍞ϡৠⱘᑇৄⱘᯊ׭ˈ᮴⊩ℷᐌᎹ԰DŽ བԩ㓽䖯 ゴ㡖ˈབ᥻ࠊ⌕Ё䆺㒚ҟ㒡DŽ ᅗਞ䆝Դˈ Դϡ㛑䱣ᛣഄᓔྟᮄⱘ䇁হഫ ˄ᔧ✊䰸њԴϔⳈ೼Փ⫼ⱘЏഫ˅DŽԩᯊԴ㛑໳Փ⫼ᮄഫˈᇚӮ೼ৢ䴶ⱘ ⊼ᛣˈ೼㄀Ѡ㸠ⱘ㸠佪᳝ϔϾぎḐDŽPython ᣛ⼎ⱘ䖭Ͼ䫭䇃ਞ䆝៥Ӏ⿟ᑣⱘ䇁⊩ᰃ᮴ᬜⱘˈे⿟ᑣ≵᳝ℷ⹂ഄ㓪ݭDŽ LQYDOLGV\QWD[6\QWD[(UURU A L(UURU1RWLFHDVLQJOHVSDFHDWWKHVWDUWRIWKHOLQH 9DOXHLV SULQW )LOHZKLWHVSDFHS\OLQH ᔧԴ䖤㸠䖭Ͼ⿟ᑣⱘᯊ׭ˈԴӮᕫࠄϟ䴶ⱘ䫭䇃˖ L ,UHSHDWWKHYDOXHLV SULQW L(UURU1RWLFHDVLQJOHVSDFHDWWKHVWDUWRIWKHOLQH 9DOXHLV SULQW  L Դ䳔㽕䆄ԣⱘϔḋϰ㽓ᰃ䫭䇃ⱘ㓽䖯Ӯᓩথ䫭䇃DŽ՟བ˖ ⫼໘ⱘ՟ᄤDŽ 䖭ᛣੇⴔৠϔሖ⃵ⱘ䇁হᖙᖙ乏᳝Ⳍৠⱘ㓽䖯DŽ↣ϔ㒘䖭ḋⱘ䇁হ⿄ЎϔϾഫഫDŽ៥Ӏᇚ೼ৢ䴶ⱘゴ㡖Ёⳟࠄ᳝݇ഫⱘ ᅮ䇁হⱘߚ㒘DŽއᅮ䘏䕥㸠ⱘ㓽䖯ሖ⃵ˈҢ㗠⫼ᴹ އぎⱑ೼ Python Ёᰃ䞡㽕ⱘDŽџᅲϞ㸠㸠佪ⱘぎⱑᰃ䞡㽕ⱘDŽᅗ⿄Ў㓽㓽䖯DŽ೼䘏䕥㸠佪ⱘぎⱑ˄ぎḐ੠ࠊ㸼ヺ˅⫼ᴹ 2.9 㓽㓽䖯 11 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ᄫヺІ ⼎ GDWXP` ᄫ݌ᰒ^NH\ ⼎ >H[SUHVVLRQ@ ߫㸼ᰒ ⼎ 㒘ᰒܗ 㒥ᅮ៪  H[SHUHVVLRQ ⫼ DUJXPHQWV  ߑ᭄䇗 I LQGH[@ ᇏഔ↉[>LQGH[ [>LQGH[@ ϟᷛ 㗗 [DWWULEXWH ሲᗻখ  ᣛ᭄ 䕀 a[ ᣝԡ㗏 [ˈ[ ℷ䋳ো পԭ 䰸⊩Ϣ Ь⊩ǃ ˈˈ ⫣ޣ ˈ ࡴ⊩Ϣ ˈ!! ⿏ԡ  ᣝԡϢ ៪ A ᣝԡᓖ _ ᣝԡ៪   ↨䕗 ˈ!ˈ! ˈ ˈˈ ⌟䆩 LVˈLVQRW ৠϔᗻ 䆩 LQˈQRWLQ ៤ਬ⌟ Ā䴲ā QRW[ Ꮧᇨ ĀϢā DQG Ꮧᇨ Ā៪ā RU Ꮧᇨ 㸼䖒ᓣ ODPEGD /DPEGD 䖤ㅫヺ ᦣ䗄 ぎḐ䯂乬˅DŽ䖭ᰃ Python བԩՓ⿟ᑣਬⱘ⫳⌏বᕫ᳈ࡴ䕏ᵒⱘϔϾ՟ᄤDŽ Ͼ⏙᱄ⓖ҂ⱘ䕧ߎˈ㗠⿟ᑣгবᕫ᳈ࡴᯧ䇏˄಴Ў៥Ӏϡ䳔㽕ᢙᖗ䕧ߎП䯈ⱘ area П䯈ᣛᅮぎḐˈPython 㞾ࡼ೼䙷䞠ᬒњϔϾぎḐˈ䖭ḋ៥Ӏህৃҹᕫࠄϔ ঺໪ˈ⊼ᛣ Python བԩᠧॄĀⓖ҂ⱘā䕧ߎDŽሑㅵ៥Ӏ≵᳝೼'Area is'੠ব䞣 breadth)ⱘؐDŽ ৢ⫼ print 䇁হᠧॄDŽ೼঺ϔϾᠧॄ䇁হЁˈ៥ӀⳈ᥹Փ⫼㸼䖒ᓣ 2 * (length + ೼ব䞣 area Ёˈ ✊ټⶽᔶⱘ䴶⿃੠䖍䭓DŽ៥Ӏ㸼䖒ᓣ length * breadth ⱘ㒧ᵰᄬ ࡽ㸼䖒ᓣՓ⫼ᅗӀ䅵ㅫ׳೼ҹᅗӀੑৡⱘব䞣ЁDŽ៥Ӏټⶽᔶⱘ䭓ᑺϢᆑᑺᄬ ᅗབԩᎹ԰ 3HULPHWHULV $UHDLV S\WKRQH[SUHVVLRQS\ 䕧ߎ  OHQJWKEUHDGWK   3HULPHWHULV SULQW DUHD $UHDLV SULQW EUHDGWK DUHD OHQJWK  EUHDGWK  OHQJWK  H[SUHVVLRQS\)LOHQDPH XVUELQS\WKRQ ՟ 3.1 Փ⫼㸼䖒ᓣ Փ⫼㸼䖒ᓣ 3.4 㸼䖒ᓣ ে৥Ꮊ㒧ড়ⱘˈे a = b = c 㹿໘⧚Ў a = (b = c)DŽ ㅫDŽ՟བˈ2 + 3 + 4 㹿䅵ㅫ៤(2 + 3) + 4DŽϔѯབ䌟ؐ䖤ㅫヺ䙷ḋⱘ䖤ㅫヺᰃ⬅ 㑻ⱘ䖤ㅫヺᣝ✻ҢᎺ৥েⱘ乎ᑣ䅵ܜ䖤ㅫヺ䗮ᐌ⬅Ꮊ৥ে㒧ড়ˈे݋᳝ⳌৠӬ 3.3.2 㒧ড়㾘ᕟ Ё䅽ࡴ⊩೼Ь⊩Пࠡ䅵ㅫˈ䙷МԴህᕫݭ៤㉏Ԑ(2 + 3) * 4 ⱘḋᄤDŽ ᵰԴᛇ㽕ᬍবᅗӀⱘ䅵ㅫ乎ᑣˈԴᕫՓ⫼೚ᣀোDŽ՟བˈԴᛇ㽕೼ϔϾ㸼䖒ᓣ ᅮњાϾ䖤ㅫヺ೼߿ⱘ䖤ㅫヺПࠡ䅵ㅫDŽ✊㗠ˈབއ㑻㸼ܜ咬䅸ഄˈ䖤ㅫヺӬ 3.3.1 䅵䅵ㅫ乎ᑣ 㑻DŽܜ㑻 DŽ՟བˈ+੠-᳝ⳌৠⱘӬܜ೼㸼Ё߫೼ৠϔ㸠ⱘ䖤ㅫヺ݋᳝ ⳌৠӬ ݊Ё៥Ӏ䖬≵᳝᥹㾺䖛ⱘ䖤ㅫヺᇚ೼ৢ䴶ⱘゴ㡖Ёҟ㒡DŽ 14 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 㑻ܜ㸼 3.2 䖤ㅫヺӬ ⏙᱄DŽϢℸৠᯊˈ೚ᣀোгᑨ䆹ℷ⹂Փ⫼ˈ㗠ϡᑨ䆹⫼ᕫ䖛Ⓓ˄↨བ 2 + (3 + 4)˅DŽ 乎ᑣˈՓ⿟ᑣሑৃ㛑ഄᯧ䇏DŽ՟བˈ2 + (3 * 4)ᰒ✊↨ 2 + 3 * 4ৢܜ㒘䖤ㅫヺ੠᪡԰᭄ˈҹ֓㛑໳ᯢ⹂ഄᣛߎ䖤ㅫⱘ ϟ䴶䖭ᓴ㸼˄Ϣ Python খ㗗᠟ݠЁⱘ䙷Ͼ㸼ϔ῵ϔḋ˅Ꮖ㒣乒ঞњᅠᭈⱘ䳔㽕DŽџᅲϞˈ៥ᓎ䆂ԴՓ⫼೚ᣀোᴹߚ 䅵ㅫ㸼Ё䕗ϟ䴶ⱘ䖤ㅫヺˈ✊ৢ೼䅵ㅫ߫೼㸼Ϟ䚼ⱘ䖤ㅫヺDŽܜᛣੇⴔ೼ϔϾ㸼䖒ᓣЁˈPython Ӯ佪 㑻˄᳔㋻ᆚഄ㒧ড়˅DŽ䖭ܜ㑻˄᳔ᵒᬷഄ㒧ড়˅ࠄ᳔催ⱘӬܜ㑻ˈҢ᳔ԢⱘӬܜϟ䴶䖭Ͼ㸼㒭ߎ Python ⱘ䖤ㅫヺӬ 㑻催Ѣࡴ⊩䖤ㅫヺDŽܜü䖭ᛣੇⴔЬ⊩䖤ㅫヺⱘӬ Ь⊩üخܜЬ⊩˛៥ӀⱘЁᄺ᭄ᄺਞ䆝៥Ӏᑨᔧخܜࡴ⊩ਸ਼ˈ䖬ᰃخܜབᵰԴ᳝ϔϾབ 2 + 3 * 4 䙷ḋⱘ㸼䖒ᓣˈᰃ 㑻ܜ3.3 䖤䖤ㅫヺӬ ⷁ䏃䅵ㅫ೼䖭䞠г䗖⫼DŽ [RU\ 䖨ಲ 7UXHDŽ\ )DOVH[ 7UXH 䖨ಲ \ ⱘ䅵ㅫؐDŽ བᵰ [ ᰃ 7UXHˈᅗ䖨ಲ 7UXHˈ৺߭ᅗ Ā៪ā RU Ꮧᇨ ⦄䈵⿄Ўⷁ䏃䅵ㅫDŽ 㚃ᅮᰃ )DOVH˄಴Ў [ ᰃ )DOVH˅DŽ䖭Ͼ ϡӮ䅵ㅫ \ˈ಴Ўᅗⶹ䘧䖭Ͼ㸼䖒ᓣⱘؐ ᰃ )DOVHˈ䖨ಲ )DOVHDŽ೼䖭䞠ˈ3\WKRQ [DQG\ˈ⬅Ѣ [\ 7UXH[ )DOVH ৺߭ᅗ䖨ಲ \ ⱘ䅵ㅫؐDŽ བᵰ [ Ў )DOVHˈ[DQG\ 䖨ಲ )DOVHˈ ĀϢā DQG Ꮧᇨ QRW\ 䖨ಲ )DOVHDŽ)DOVHˈᅗ䖨ಲ 7UXHDŽ [ 7UXH བᵰ [ Ў 7UXHˈ䖨ಲ )DOVHDŽབᵰ [ Ў Ā䴲ā QRW Ꮧᇨ [ \ 䖨ಲ 7UXHDŽ \   ϡㄝѢ ↨䕗ϸϾᇍ䈵ᰃ৺ϡⳌㄝ [ [ \ 䖨ಲ 7UXHDŽ VWU  \  VWU  [ \䖨ಲ )DOVHDŽ[ VW5  \  VWU  [ \䖨ಲ 7UXHDŽ[ \ [   ㄝѢ ↨䕗ᇍ䈵ᰃ৺Ⳍㄝ [! \ 䖨ಲ 7UXHDŽ \ !  ໻ѢㄝѢ䖨ಲ [ ᰃ৺໻ѢㄝѢ \ [ 13 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ⳟࠄ⿟ᑣⱘ㒧ሒˈㅔऩⱘ㒧ᴳ䖤㸠DŽ 䖭Ͼ՟ᄤЁˈ䖭Ͼ䇁হഫᰃЏഫDŽ⿟ᑣҢЏഫᓔྟᠻ㸠ˈ㗠ϟϔϾ䇁হᰃ print 'Done'䇁হDŽ೼䖭ПৢˈPython ೼ Python ᠻ㸠ᅠϔϾᅠᭈⱘ if 䇁হҹঞϢᅗⳌ݇㘨ⱘ elif ੠ else ҢহПৢˈᅗ⿏৥ if 䇁হഫⱘϟϔϾ䇁হDŽ೼    DUJXPHQWV DUH ZH XVLQJBV\VS\ 7KHFRPPDQGOLQHDUJXPHQWVDUH S\WKRQXVLQJBV\VS\ZHDUHDUJXPHQWV 䕧ߎ  ?Q V\VSDWK ?Q?Q7KH3<7+213$7+LV SULQW  SULQWL IRULLQV\VDUJY  7KHFRPPDQGOLQHDUJXPHQWVDUH SULQW  LPSRUWV\V  XVLQJBV\VS\)LOHQDPH XVUELQS\WKRQ ՟ 6.1 Փ⫼ sys ῵ഫ 6.1.1 ՓՓ⫼ sys ῵ഫ ᑧ῵ഫDŽޚՓ⫼ᷛ ៥Ӏᇚᄺдབԩˈܜᑧⱘᮍ⊩DŽ佪ޚ῵ഫৃҹҢ݊Ҫ⿟ᑣ 䕧ܹ ҹ߽֓⫼ᅗⱘࡳ㛑DŽ䖭гᰃ៥ӀՓ⫼ Python ᷛ 29 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 !!!LPSRUWV\V S\WKRQ ՟ 6.4 Փ⫼ dir ߑ᭄ 6.6.1 Փ⫼ dir ߑ᭄ ⿄߫㸼DŽ ᔧԴЎ dir()ᦤկϔϾ῵ഫৡⱘᯊ׭ˈᅗ䖨ಲ῵ഫᅮНⱘৡ⿄߫㸼DŽབᵰϡᦤկখ᭄ˈᅗ䖨ಲᔧࠡ῵ഫЁᅮНⱘৡ ԴৃҹՓ⫼ݙᓎⱘ dir ߑ᭄ᴹ߫ߎ῵ഫᅮНⱘᷛ䆚ヺDŽᷛ䆚ヺ᳝ߑ᭄ǃ㉏੠ব䞣DŽ 6.6 dir()ߑ᭄ mymodule_demo2.py ⱘ䕧ߎϢ mymodule_demo.py ᅠܼⳌৠDŽ YHUVLRQ 9HUVLRQ SULQW  VD\KL   IURPP\PRGXOHLPSRUW $OWHUQDWLYH IURPP\PRGXOHLPSRUWVD\KLYHUVLRQ  P\PRGXOHBGHPRS\)LOHQDPH XVUELQS\WKRQ ϟ䴶ᰃϔϾՓ⫼ from..import 䇁⊩ⱘ⠜ᴀDŽ 6.5.2 from..import ϡ䳔㽕ϡᮁഄᄺдᮄⱘᮍ⊩DŽ ⊼ᛣ៥ӀՓ⫼њⳌৠⱘ⚍োᴹՓ⫼῵ഫⱘ៤ਬDŽPython ᕜདഄ䞡⫼њⳌৠⱘ䆄োᴹˈՓ៥Ӏ䖭ѯ Python ⿟ᑣਬ 6.5.1.1 ᅗᅗབԩᎹ԰ 9HUVLRQ +LWKLVLVP\PRGXOHVSHDNLQJ S\WKRQP\PRGXOHBGHPRS\ 䕧ߎ P\PRGXOHYHUVLRQ 9HUVLRQ SULQW  P\PRGXOHVD\KL  LPSRUWP\PRGXOH  P\PRGXOHBGHPRS\)LOHQDPH 32 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 XVUELQS\WKRQ 䆄ԣ䖭Ͼ῵ഫᑨ䆹㹿ᬒ㕂೼៥Ӏ䕧ܹᅗⱘ⿟ᑣⱘৠϔϾⳂᔩЁˈ៪㗙೼ sys.path ᠔߫ⳂᔩПϔDŽ ⳟⳟབԩ೼៥Ӏ߿ⱘ Python ⿟ᑣЁՓ⫼䖭Ͼ῵ഫDŽ Ϟ䴶ᰃϔϾ ῵ഫ ⱘ՟ᄤDŽԴᏆ㒣ⳟࠄˈᅗϢ៥Ӏ᱂䗮ⱘ Python ⿟ᑣⳌ↨ᑊ≵᳝ҔМ⡍߿П໘DŽ៥Ӏ᥹ϟᴹᇚ (QGRIP\PRGXOHS\     YHUVLRQ   +LWKLVLVP\PRGXOHVSHDNLQJ SULQW  GHIVD\KL  P\PRGXOHS\)LOHQDPH XVUELQS\WKRQ ՟ 6.3 བԩ߯ᓎԴ㞾Ꮕⱘ῵ഫ 6.5.1 ߯ᓎԴ㞾Ꮕⱘ῵ഫ ৡњDŽϟ䴶䖭Ͼ՟ᄤᇚӮՓᅗ᳈ࡴ⏙᱄DŽ ʽ↣Ͼ Python ⿟ᑣгᰃϔϾ῵ഫDŽԴᏆ㒣⹂ֱᅗ݋᳝.py ᠽሩخ߯ᓎԴ㞾Ꮕⱘ῵ഫᰃकߚㅔऩⱘˈԴϔⳈ೼䖭ḋ 6.5 ࠊ䗴Դ㞾Ꮕⱘ῵ഫ ⱘᙄᔧ᪡԰DŽ ↣Ͼ Python ῵ഫ䛑᳝ᅗⱘ__name__ˈབᵰᅗᰃ'__main__'ˈ䖭䇈ᯢ䖭Ͼ῵ഫ㹿⫼᠋ऩ⣀䖤㸠ˈ៥Ӏৃҹ䖯㸠Ⳍᑨ 6.4.1.1 ᅗᅗབԩᎹ԰ !!! ,DPEHLQJLPSRUWHGIURPDQRWKHUPRGXOH !!!LPSRUWXVLQJBQDPH S\WKRQ  7KLVSURJUDPLVEHLQJUXQE\LWVHOI S\WKRQXVLQJBQDPHS\ 䕧ߎ  ,DPEHLQJLPSRUWHGIURPDQRWKHUPRGXOH SULQW HOVH  7KLVSURJUDPLVEHLQJUXQE\LWVHOI SULQW 31 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  LWHPVWRSXUFKDVH  VKRSOLVW OHQ ,KDYH SULQW  @ EDQDQD  FDUURW  PDQJR  DSSOH VKRSOLVW > 7KLVLVP\VKRSSLQJOLVW  XVLQJBOLVWS\)LOHQDPH XVUELQS\WKRQ ՟ 7.1 Փ⫼߫㸼 7.2.2 Փ⫼߫㸼 ㉏г䗮䖛⚍োՓ⫼ˈ՟བ mylist.fieldDŽ ϔϾ㉏г᳝ඳˈᅗᰃҙҙЎ㉏㗠ᅮНⱘব䞣DŽҙҙ೼Դ᳝ϔϾ䆹㉏ⱘᇍ䈵ⱘᯊ׭ˈԴᠡৃҹՓ⫼䖭ѯব䞣/ৡ⿄DŽ ࡴ䙷ϾᄫヺІDŽ⊼ᛣˈՓ⫼⚍োᴹՓ⫼ᇍ䈵ⱘᮍ⊩DŽ Ў list ㉏ᦤկњ append ᮍ⊩ˈ䖭Ͼᮍ⊩䅽Դ೼߫㸼ሒ⏏ࡴϔϾ乍ⳂDŽ՟བ mylist.append('an item')߫㸼 mylist Ё๲ ㉏г᳝ᮍ⊩ˈेҙҙЎ㉏㗠ᅮНഄߑ᭄DŽҙҙ೼Դ᳝ϔϾ䆹㉏ⱘᇍ䈵ⱘᯊ׭ˈԴᠡৃҹՓ⫼䖭ѯࡳ㛑DŽ՟བˈPython ㉏˄㉏ൟ˅int ⱘᇍ䈵˄ᅲ՟˅iDŽџᅲϞˈԴৃҹⳟϔϟ help(int)ҹ᳈དഄ⧚㾷䖭ϔ⚍DŽ ߫㸼ᰃՓ⫼ᇍ䈵੠㉏ⱘϔϾ՟ᄤDŽᔧԴՓ⫼ব䞣 i ᑊ㒭ᅗ䌟ؐⱘᯊ׭ˈ↨བ䌟ᭈ᭄ 5ˈԴৃҹ䅸ЎԴ߯ᓎњϔϾ ᥶㋶䖭ϾЏ乬DŽ ϔ⚍㾷䞞ৃҹՓԴ᳈དⱘ⧚㾷߫㸼DŽ៥ӀӮ೼Ⳍᑨⱘゴ㡖䆺㒚خሑㅵ៥ϔⳈ᥼䖳䅼䆎ᇍ䈵੠㉏ˈԚᰃ⦄೼ᇍᅗӀ 7.2.1 ᇍ䈵Ϣ㉏ⱘᖿ䗳ܹ䮼 ᰃৃҹ㹿ᬍবⱘDŽ ⏏ࡴǃߴ䰸៪ᰃ᧰㋶߫㸼Ёⱘ乍ⳂDŽ⬅ѢԴৃҹ๲ࡴ៪ߴ䰸乍Ⳃˈ៥Ӏ䇈߫㸼ᰃ ৃবⱘ ᭄᥂㉏ൟˈे䖭⾡㉏ൟ ߫㸼Ёⱘ乍Ⳃᑨ䆹ࣙᣀ೼ᮍᣀোЁˈ䖭ḋ Python ህⶹ䘧Դᰃ೼ᣛᯢϔϾ߫㸼DŽϔᮺԴ߯ᓎњϔϾ߫㸼ˈԴৃҹ Python ЁˈԴ೼↣Ͼ乍ⳂП䯈⫼䗫োߚࡆDŽ Ϟ䴶䆄䕑ⴔԴ㽕фⱘϰ㽓ˈԴህᆍᯧ⧚㾷߫㸼њDŽাϡ䖛೼Դⱘ䌁⠽㸼Ϟˈৃ㛑↣ḋϰ㽓䛑⣀㞾ऴ᳝ϔ㸠ˈ㗠೼ ᛇԴ᳝ϔϾ䌁⠽߫㸼ˈ؛ϔϾ ᑣ߫ ⱘ乍ⳂDŽټlist ᰃ໘⧚ϔ㒘᳝ᑣ乍Ⳃⱘ᭄᥂㒧ᵘˈेԴৃҹ೼ϔϾ߫㸼Ёᄬ 7.2 ߫㸼 বᕫㅔऩDŽ 㒘੠ᄫ݌DŽ៥ӀᇚӮᄺдབԩՓ⫼ᅗӀˈҹঞᅗӀབԩՓ㓪⿟ܗ೼ Python Ё᳝ϝ⾡ݙᓎⱘ᭄᥂㒧ᵘüü߫㸼ǃ ϔ㒘Ⳍ᭄݇᥂ⱘDŽټ᭄᥂㒧ᵘ෎ᴀϞህᰃüüᅗӀᰃৃҹ໘⧚ϔѯ ᭄᥂ ⱘ 㒧ᵘ DŽ៪㗙䇈ˈᅗӀᰃ⫼ᴹᄬ 7.1 ㅔҟ 7. ᭄᭄᥂㒧ᵘ 34 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 Ңᴹ≵᳝ᄬ೼䖛ϔḋDŽڣব䞣 aüüᅗህད ݇Ѣ del ⱘϔ⚍⊼䞞üü䖭Ͼ䇁হ೼䖤㸠ৢ㹿⫼ᴹ ߴ䰸 ϔϾব䞣/ৡ⿄DŽ೼䖭Ͼ՟ᄤЁˈdel aˈԴᇚ᮴⊩ݡՓ⫼ ϞⳌৠⱘؐDŽ៥ӀՓ⫼ del 䇁হߴ䰸ᔧࠡ῵ഫЁⱘব䞣/ሲᗻˈ䖭Ͼব࣪ݡϔ⃵ড᯴೼ dir ⱘ䕧ߎЁDŽ Ўњ㾖ᆳ dir ⱘ԰⫼ˈ៥ӀᅮНϔϾᮄⱘব䞣 a ᑊϨ㒭ᅗ䌟ϔϾؐˈ✊ৢẔ偠 dirˈ៥Ӏ㾖ᆳࠄ೼߫㸼Ё๲ࡴњҹ ᰃ߫㸼ⱘϔ䚼ߚDŽ ᥹ϟᴹˈ៥Ӏϡ㒭 dir ߑ᭄Ӵ䗦খ᭄㗠Փ⫼ᅗüü咬䅸ഄˈᅗ䖨ಲᔧࠡ῵ഫⱘሲᗻ߫㸼DŽ⊼ᛣˈ䕧ܹⱘ῵ഫৠḋ ៥Ӏᴹⳟϔϟ೼䕧ܹⱘ sys ῵ഫϞՓ⫼ dirDŽ៥Ӏⳟࠄᅗࣙ৿ϔϾᑲ໻ⱘሲᗻ߫㸼DŽˈܜ佪 6.6.1.1 ᅗᅗབԩᎹ԰ !!! @ V\V  BBQDPHBB  BBGRFBB  BBEXLOWLQVBB >  !!!GLU !!! !!!GHODGHOHWHUHPRYHDQDPH !!! @ V\V  D  BBQDPHBB  BBGRFBB  BBEXLOWLQVBB >  !!!GLU  D !!!D FUHDWHDQHZYDULDEOH !!! @ V\V  BBQDPHBB  BBGRFBB  BBEXLOWLQVBB >  JHWOLVWRIDWWULEXWHVIRUFXUUHQWPRGXOH !!!GLU @ ZDUQRSWLRQV  YHUVLRQBLQIR  YHUVLRQ  VWGRXW  VWGLQ  VWGHUU  VHWWUDFH  VHWUHFXUVLRQOLPLW  VHWSURILOH  VHWGORSHQIODJV  VHWFKHFNLQWHUYDO  SV  SV  SUHIL[  SODWIRUP  SDWKBLPSRUWHUBFDFKH  SDWKBKRRNV  SDWK  PRGXOHV  PHWDBSDWK  PD[XQLFRGH  PD[LQW  KH[YHUVLRQ  JHWUHIFRXQW  JHWUHFXUVLRQOLPLW  JHWILOHV\VWHPHQFRGLQJ  JHWGORSHQIODJV  JHWGHIDXOWHQFRGLQJ  JHWFKHFNLQWHUYDO  H[LW  H[HFXWDEOH  H[HFBSUHIL[  H[FHSWKRRN  H[FBW\SH  H[FBLQIR  H[FBFOHDU  GLVSOD\KRRN  FRS\ULJKW  FDOOVWDWV  FDOOBWUDFLQJ  E\WHRUGHU  EXLOWLQBPRGXOHBQDPHV  DUJY  DSLBYHUVLRQ  BJHWIUDPH  BBVWGRXWBB  BBVWGLQBB  BBVWGHUUBB  BBQDPHBB  BBH[FHSWKRRNBB  BBGRFBB  BBGLVSOD\KRRNBB > V\V JHWOLVWRIDWWULEXWHVIRUV\VPRGXOH !!!GLU 33 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  SHQJXLQ  HOHSKDQW  ZROI  GROSKLQ  PRQNH\ $OO DQLPDOV LQ QHZ ]RR DUH 1XPEHURIDQLPDOVLQWKHQHZ]RRLV 1XPEHURIDQLPDOVLQWKH]RRLV S\WKRQXVLQJBWXSOHS\ 䕧ߎ QHZB]RR>@>@ /DVWDQLPDOEURXJKWIURPROG]RRLV SULQW QHZB]RR>@ $QLPDOVEURXJKWIURPROG]RRDUH SULQW QHZB]RR $OODQLPDOVLQQHZ]RRDUH SULQW  QHZB]RR OHQ 1XPEHURIDQLPDOVLQWKHQHZ]RRLV SULQW  ]RR GROSKLQ  PRQNH\  QHZB]RR   ]RR OHQ 1XPEHURIDQLPDOVLQWKH]RRLV SULQW  SHQJXLQ  HOHSKDQW  ZROI  ]RR  XVLQJBWXSOHS\)LOHQDPH XVUELQS\WKRQ 㒘ܗ⫼՟ 7.2 Փ 㒘ܗ⫼7.3.1 Փ বDŽ 㒘ⱘؐϡӮᬍܗ㒘䗮ᐌ⫼೼Փ䇁হ៪⫼᠋ᅮНⱘߑ᭄㛑໳ᅝܼഄ䞛⫼ϔ㒘ؐⱘᯊ׭ˈे㹿Փ⫼ⱘܗⱘ乍ⳂᅮНDŽ 㒘䗮䖛೚ᣀোЁ⫼䗫োߚࡆܗ㒘DŽܗ㒘੠ᄫヺІϔḋᰃ ϡৃবⱘ ेԴϡ㛑ׂᬍܗ㒘੠߫㸼कߚ㉏Ԑˈাϡ䖛ܗ 㒘ܗܗ 7.3 བᵰԴᛇ㽕ⶹ䘧߫㸼ᇍ䈵ᅮНⱘ᠔᳝ᮍ⊩ˈৃҹ䗮䖛 help(list)㦋ᕫᅠᭈⱘⶹ䆚DŽ ㋴ˈ಴ℸ៥ӀՓ⫼ del shoplist[0]˄䆄ԣˈPython Ң 0 ᓔྟ䅵᭄˅DŽܗ䰸߫㸼Ёⱘ㄀ϔϾ ԰DŽ䖭䞠ˈ៥Ӏᣛߎ៥Ӏᛇ㽕ߴ䰸߫㸼ЁⱘાϾ乍Ⳃˈ㗠 del 䇁হЎ៥ӀҢ߫㸼Ёߴ䰸ᅗDŽ៥Ӏᣛᯢ៥Ӏᛇ㽕ߴ ᳔ৢˈԚ៥Ӏᅠ៤њ೼Ꮦഎ䌁фϔḋϰ㽓ⱘᯊ׭ˈ៥Ӏᛇ㽕ᡞᅗҢ߫㸼Ёߴ䰸DŽ៥ӀՓ⫼ del 䇁হᴹᅠ៤䖭ϾᎹ ᬍৢⱘ߫㸼üü䖭ϢᄫヺІᎹ԰ⱘᮍ⊩ϡৠDŽ䖭ህᰃ៥Ӏ᠔䇈ⱘ߫㸼ᰃ ৃবⱘ 㗠ᄫヺІᰃ ϡৃবⱘ DŽ ݡ᥹ϟᴹˈ៥ӀՓ⫼߫㸼ⱘ sort ᮍ⊩ᴹᇍ߫㸼ᥦᑣDŽ䳔㽕⧚㾷ⱘᰃˈ䖭Ͼᮍ⊩ᕅડ߫㸼ᴀ䑿ˈ㗠ϡᰃ䖨ಲϔϾׂ ϔϾᭈ⋕ⱘ䕧ߎDŽ ⱘݙᆍᴹẔ偠䖭Ͼ乍Ⳃᰃ৺⹂ᅲ㹿⏏ࡴ䖯߫㸼њDŽᠧॄ߫㸼া䳔ㅔऩഄᡞ߫㸼Ӵ䗦㒭 print 䇁হˈ៥Ӏৃҹᕫࠄ ᥹ϟᴹˈ៥ӀՓ⫼ append ᮍ⊩೼߫㸼Ё⏏ࡴњϔϾ乍Ⳃˈህབࠡ䴶Ꮖ㒣䅼䆎䖛ⱘϔḋDŽ✊ৢ៥Ӏ䗮䖛ᠧॄ߫㸼 䖛⹂ᅲㅔऩ᳝ᬜDŽ 36 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ᳝⚍䲒ⳟˈϡخ⊼ᛣˈ៥Ӏ೼ print 䇁হⱘ㒧ሒՓ⫼њϔϾ 䗫ো ᴹ⍜䰸↣Ͼ print 䇁হ㞾ࡼᠧॄⱘᤶ㸠ヺDŽ䖭ḋ ᗻӮ೼ৢ䴶ⱘゴ㡖Ё䅼䆎DŽ ៥ӀгՓ⫼њ for..in ᕾ⦃೼߫㸼Ё৘乍Ⳃ䯈䗦ᔦDŽҢ⦄೼ᓔྟˈԴϔᅮᏆ㒣ᛣ䆚ࠄ߫㸼гᰃϔϾᑣ߫DŽᑣ߫ⱘ⡍ 㸼Ё⏏ࡴ ӏԩ⾡㉏ⱘᇍ䈵 ࣙᣀ᭄⫮㟇݊Ҫ߫㸼DŽ 䌁фⱘϰ㽓ⱘৡᄫᄫヺІˈԚᰃ䆄ԣˈԴৃҹ೼߫ټব䞣 shoplist ᰃᶤҎⱘ䌁⠽߫㸼DŽ೼ shoplist Ёˈ៥Ӏাᄬ 7.2.2.1 ᅗᅗབԩᎹ԰ @ ULFH  PDQJR  FDUURW  EDQDQD 0\VKRSSLQJOLVWLVQRZ> ,ERXJKWWKHDSSOH 7KHILUVWLWHP,ZLOOEX\LVDSSOH @ ULFH  PDQJR  FDUURW  EDQDQD  DSSOH 6RUWHGVKRSSLQJOLVWLV> ,ZLOOVRUWP\OLVWQRZ @ ULFH  EDQDQD  FDUURW  PDQJR  DSSOH 0\VKRSSLQJOLVWLVQRZ> ,DOVRKDYHWREX\ULFH DSSOHPDQJRFDUURWEDQDQD7KHVHLWHPVDUH ,KDYHLWHPVWRSXUFKDVH S\WKRQXVLQJBOLVWS\ 䕧ߎ VKRSOLVW 0\VKRSSLQJOLVWLVQRZ SULQW ROGLWHP ,ERXJKWWKH SULQW GHOVKRSOLVW>@ ROGLWHP VKRSOLVW>@ VKRSOLVW>@ 7KHILUVWLWHP,ZLOOEX\LV SULQW  VKRSOLVW 6RUWHGVKRSSLQJOLVWLV SULQW  VKRSOLVWVRUW  ,ZLOOVRUWP\OLVWQRZ SULQW  VKRSOLVW 0\VKRSSLQJOLVWLVQRZ SULQW  ULFH VKRSOLVWDSSHQG  ?Q,DOVRKDYHWREX\ULFH SULQW  SULQWLWHP IRULWHPLQVKRSOLVW 1RWLFHWKHFRPPDDWHQGRIWKHOLQH 7KHVHLWHPVDUH SULQW  35 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  VZDURRSFK#E\WHRIS\WKRQLQIR  6ZDURRS DE ^  RRN E GGUHVV D LVVKRUWIRU DE   XVLQJBGLFWS\)LOHQDPH XVUELQS\WKRQ ՟ 7.4 Փ⫼ᄫ݌ 7.4.1 Փ⫼ᄫ݌ ᄫ݌ᰃ dict ㉏ⱘᅲ՟/ᇍ䈵DŽ 䆄ԣᄫ݌Ёⱘ䬂/ؐᇍᰃ≵᳝乎ᑣⱘDŽབᵰԴᛇ㽕ϔϾ⡍ᅮⱘ乎ᑣˈ䙷МԴᑨ䆹೼Փ⫼ࠡ㞾ᏅᇍᅗӀᥦᑣDŽ ᇍ⫼䗫োߚࡆˈ᠔᳝䖭ѯ䛑ࣙᣀ೼㢅ᣀোЁDŽ 䬂ؐᇍ೼ᄫ݌Ёҹ䖭ḋⱘᮍᓣᷛ䆄˖d = {key1 : value1, key2 : value2 }DŽ⊼ᛣᅗӀⱘ䬂/ؐᇍ⫼ݦোߚࡆˈ㗠৘Ͼ ෎ᴀ䇈ᴹህᰃˈԴᑨ䆹াՓ⫼ㅔऩⱘᇍ䈵԰Ў䬂DŽ ⊼ᛣˈԴা㛑Փ⫼ϡৃবⱘᇍ䈵˄↨བᄫヺІ˅ᴹ԰Ўᄫ݌ⱘ䬂ˈԚᰃԴৃҹϡৃব៪ৃবⱘᇍ䈵԰Ўᄫ݌ⱘؐDŽ བᵰ᳝ϸϾҎᙄᎻৠৡⱘ䆱ˈԴ᮴⊩ᡒࠄℷ⹂ⱘֵᙃDŽڣ㋏೼ϔ䍋DŽ⊼ᛣˈ䬂ᖙ乏ᰃଃϔⱘˈህ 㘨˅މⱘഄഔ㈓ˈेˈ៥Ӏᡞ䬂˄ৡᄫ˅੠ؐ˄䆺㒚ᚙމᄫ݌㉏ԐѢԴ䗮䖛㘨㋏Ҏৡᄫᶹᡒഄഔ੠㘨㋏Ҏ䆺㒚ᚙ 7.4 ᄫ݌ া᳝ϔϾᅮࠊⱘᯊ׭᳝ᬜDŽ ೼㄀ѠϾ print 䇁হЁˈ៥ӀՓ⫼њϔϾᅮࠊˈৢ䴶䎳ⴔ%ヺোৢⱘऩϾ乍Ⳃüü≵᳝೚ᣀোDŽ䖭া೼ᄫヺІЁ ໮ϔሖⱘẔ偠⿟ᑣᰃ৺ℷ⹂DŽܡৃ㛑ᏠᳯՓ⫼ℷ⹂ⱘᅮࠊˈҢ㗠ৃҹ䙓 ೼໻໮᭄ᯊ׭ˈԴৃҹাՓ⫼%s ᅮࠊˈ㗠䅽 Python ᴹᦤԴ໘⧚࠽ԭⱘџᚙDŽ䖭⾡ᮍ⊩ᇍ᭄ৠḋ༣ᬜDŽ✊㗠ˈԴ њ៥ӀϔⳈҹᴹՓ⫼ⱘ䗫োDŽܡњ䆌໮ᄫヺІ᪡԰DŽᅗг䙓ܡprint ⱘ䖭Ͼ⫼⊩Փᕫ㓪ݭ䕧ߎবᕫᵕ݊ㅔऩˈᅗ䙓 ব䞣 name ⱘؐˈձℸ㉏᥼DŽ 㒘Ёⱘ↣Ͼ乍Ⳃ䕀ᤶ៤ᄫヺІᑊϨ⫼ᄫヺІⱘؐ᳓ᤶᅮࠊⱘԡ㕂DŽ಴ℸ%s 㹿᳓ᤶЎܗⱘᰃᡞخPython ೼䖭䞠᠔ 㒘ⱘ㄀ѠϾ乍Ⳃ ageDŽܗᅗᇍᑨ 㒘Ёⱘ㄀ϔϾ乍ⳂDŽ㗠㄀ѠϾᅮࠊᰃ%dˈܗՓ⫼%sˈ䖭ᇍᑨব䞣 nameˈᅗᰃܜ㒘ˈ៥Ӏ佪ܗ㾖ᆳ៥ӀՓ⫼ⱘ㄀ϔϾ 㒘ᖙ乏ᣝ✻Ⳍৠⱘ乎ᑣᴹᇍᑨ䖭ѯᅮࠊDŽܗᓣDŽᅮࠊৃҹᰃ%s 㸼⼎ᄫヺІ៪%d 㸼⼎ᭈ᭄DŽ 㒘ⱘᄫヺІDŽ䖭ѯᄫヺІ݋໛ᅮࠊⱘࡳ㛑DŽᅮࠊ䅽䕧ߎ⒵䎇ᶤ⾡⡍ᅮⱘḐܗprint 䇁হৃҹՓ⫼䎳ⴔ%ヺোⱘ乍Ⳃ 7.3.2.1 ᅗᅗབԩᎹ԰ 38 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 :K\LV6ZDURRSSOD\LQJZLWKWKDWS\WKRQ“ 6ZDURRSLV\HDUVROG S\WKRQSULQWBWXSOHS\ 䕧ߎ QDPH :K\LVVSOD\LQJZLWKWKDWS\WKRQ“ SULQW  QDPHDJH  VLVG\HDUVROG SULQW   6ZDURRS  QDPH  DJH  SULQWBWXSOHS\)LOHQDPH XVUELQS\WKRQ 㒘䕧ߎܗ⫼՟ 7.3 Փ 㒘᳔䗮ᐌⱘ⫼⊩ᰃ⫼೼ᠧॄ䇁হЁˈϟ䴶ᰃϔϾ՟ᄤ˖ܗ 㒘Ϣᠧॄ䇁হܗ 7.3.2 ⱘᇍ䈵DŽټাᰃՓ⫼঺ϔϾᇍ䈵ᄬ 㒘Ёⱘ߫㸼ㄝㄝ䛑ᰃབℸDŽা㽕ᰃ 3\WKRQˈᅗӀህܗ㒘ˈ៪ܗ㒘ˈ៪߫㸼Ёⱘܗⱘ 㒘Ёܗ3HUO Ё䙷ḋ㹿ᠧᬷDŽৠḋ ڣ߫㸼ПЁⱘ߫㸼ϡӮ༅এᅗⱘ䑿ӑˈे߫㸼ϡӮ 㒭 3HUO ⿟ᑣਬⱘ⊼䞞 㒘ⱘᯊ׭ˈԴᑨ䆹ᣛᯢ singleton = (2 , )DŽܗϾᏺ೚ᣀোⱘᇍ䈵DŽेབᵰԴᛇ㽕ⱘᰃϔϾࣙ৿乍Ⳃ 2 ⱘ 㒘੠㸼䖒ᓣЁϔܗ㒘ህϡ䙷МㅔऩњDŽԴᖙ乏೼㄀ϔϾ˄ଃϔϔϾ˅乍Ⳃৢ䎳ϔϾ䗫োˈ䖭ḋ Python ᠡ㛑ऎߚ ܗ㋴ⱘܗ㒘⬅ϔᇍぎⱘ೚ᣀো㒘៤ˈབ myempty = ()DŽ✊㗠ˈ৿᳝ऩϾܗ㒘DŽϔϾぎⱘܗ৿᳝ 0 Ͼ៪ 1 Ͼ乍Ⳃⱘ 㒘ⱘ㄀ϝϾ乍Ⳃⱘ㄀ϝϾ乍ⳂDŽܗ ԰㋶ᓩ䖤ㅫヺDŽ៥ӀՓ⫼ new_zoo[2]ᴹ䆓䯂 new_zoo Ёⱘ㄀ϝϾ乍ⳂDŽ៥ӀՓ⫼ new_zoo[2][2]ᴹ䆓䯂 new_zoo ៥Ӏᇍ߫㸼ⱘ⫼⊩ϔḋDŽ䖭㹿⿄ڣ㒘Ёⱘ乍Ⳃˈህܗ៥Ӏৃҹ䗮䖛ϔᇍᮍᣀোᴹᣛᯢᶤϾ乍Ⳃⱘԡ㕂Ң㗠ᴹ䆓䯂 㒘ϡӮ༅এᅗⱘ䑿ӑDŽܗ㒘Пݙⱘܗ⠽ುᏺ䖛ᴹⱘࡼ⠽DŽಲࠄ䆱乬ˈ⊼ᛣ 㒘ࣙ৿њϔѯᏆ㒣೼䙷䞠ⱘࡼ⠽੠Ң㗕ࡼܗ ⬅Ѣ㗕ࡼ⠽ು݇䯁њˈ៥Ӏᡞࡼ⠽䕀⿏ࠄᮄࡼ⠽ುDŽ಴ℸˈnew_zoo 㒘гᰃϔϾᑣ߫DŽܗ㒘ⱘ䭓ᑺDŽ䖭г㸼ᯢܗ㒘ˈ៥Ӏⳟࠄ len ߑ᭄ৃҹ⫼ᴹ㦋পܗব䞣 zoo ᰃϔϾ 7.3.1.1 ᅗᅗབԩᎹ԰ /DVWDQLPDOEURXJKWIURPROG]RRLVSHQJXLQ  SHQJXLQ  HOHSKDQW  ZROI $QLPDOVEURXJKWIURPROG]RRDUH 37 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 @<VKRSOLVW ,WHPWRLV SULQW @VKRSOLVW> ,WHPWRHQGLV SULQW @VKRSOLVW> ,WHPWRLV SULQW 6OLFLQJRQDOLVW  VKRSOLVW>@ ,WHPLV SULQW VKRSOLVW>@ ,WHPLV SULQW VKRSOLVW>@ ,WHPLV SULQW VKRSOLVW>@ ,WHPLV SULQW VKRSOLVW>@ ,WHPLV SULQW VKRSOLVW>@ ,WHPLV SULQW RSHUDWLRQ 6XEVFULSWLRQ ,QGH[LQJRU  @ EDQDQD  FDUURW  PDQJR  DSSOH VKRSOLVW >  VHTS\)LOHQDPH XVUELQS\WKRQ ՟ 7.5 Փ⫼ᑣ߫ 7.5.1 Փ⫼ᑣ߫ ेϔ䚼ߚᑣ߫DŽ ߛ⠛᪡԰ヺDŽ㋶ᓩ᪡԰ヺ䅽៥ӀৃҹҢᑣ߫ЁᡧপϔϾ⡍ᅮ乍ⳂDŽߛ⠛᪡԰ヺ䅽៥Ӏ㛑໳㦋পᑣ߫ⱘϔϾߛ⠛ˈ 㒘੠ᄫヺІ䛑ᰃᑣ߫ˈԚᰃᑣ߫ᰃҔМˈᅗӀЎҔМབℸ⡍߿ਸ਼˛ᑣ߫ⱘϸϾЏ㽕⡍⚍ᰃ㋶ᓩ᪡԰ヺ੠ܗ߫㸼ǃ 7.5 ᑣᑣ߫ ˄䖭೼㓪䆥఼䆒䅵ⱘᴃ䇁Ё㹿⿄԰ ヺো㸼 ˅DŽ üüԴ೼ߑ᭄ᅮНⱘখ᭄߫㸼ЁՓ⫼ⱘ䬂/ؐᇍDŽᔧԴ೼ߑ᭄ЁՓ⫼ব䞣ⱘᯊ׭ˈᅗাϡ䖛ᰃՓ⫼ϔϾᄫ݌ⱘ䬂 ݇䬂ᄫখ᭄Ϣᄫ݌DŽབᵰᤶϔϾ㾦ᑺⳟᕙԴ೼ߑ᭄ЁՓ⫼ⱘ݇䬂ᄫখ᭄ⱘ䆱ˈԴᏆ㒣Փ⫼њᄫ݌њʽা䳔ᛇϔϟ ᶹⳟ dict ㉏ⱘᅠᭈᮍ⊩߫㸼DŽ ៥ӀৃҹՓ⫼ in ᪡԰ヺᴹẔ偠ϔϾ䬂/ؐᇍᰃ৺ᄬ೼ˈ៪㗙Փ⫼ dict ㉏ⱘ has_key ᮍ⊩DŽԴৃҹՓ⫼ help(dict)ᴹ forˉഫЁᠧॄ䖭ѯؐDŽ ࣙ৿ϔᇍ乍Ⳃüü䬂ϢᇍᑨⱘؐDŽ៥Ӏᡧপ䖭Ͼᇍˈ✊ৢߚ߿䌟㒭 for..in ᕾ⦃Ёⱘব䞣 name ੠ address ✊ৢ೼ 㒘䛑ܗ㒘ⱘ߫㸼ˈ݊Ё↣Ͼܗ᥹ϟᴹˈ៥ӀՓ⫼ᄫ݌ⱘ items ᮍ⊩ˈᴹՓ⫼ᄫ݌Ёⱘ↣Ͼ䬂/ؐᇍDŽ䖭Ӯ䖨ಲϔϾ ✊ৢᡞᅗӀӴ䗦㒭 del 䇁হህৃҹњDŽᠻ㸠䖭Ͼ᪡԰ⱘᯊ׭ˈ៥Ӏ᮴䳔ⶹ䘧䙷Ͼ䬂᠔ᇍᑨⱘؐDŽ ៥ӀৃҹՓ⫼៥Ӏⱘ㗕᳟টüüdel 䇁হᴹߴ䰸䬂/ؐᇍDŽ៥Ӏা䳔㽕ᣛᯢᄫ݌੠⫼㋶ᓩ᪡԰ヺᣛᯢ㽕ߴ䰸ⱘ䬂ˈ ⱘϔḋDŽخᇍ Guido ᠔ ೼Ϟ䴶ⱘ՟ᄤЁ៥Ӏڣ៥ӀৃҹՓ⫼㋶ᓩ᪡԰ヺᴹᇏഔϔϾ䬂ᑊЎᅗ䌟ؐˈ䖭ḋህ๲ࡴњϔϾᮄⱘ䬂/ؐᇍˈህ 40 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 䬂ˈҢ㗠Փ⫼䬂/ؐᇍDŽ៥Ӏৃҹⳟࠄᄫ݌ⱘ䇁⊩ৠḋकߚㅔऩDŽ 㒘ゴ㡖ЁᏆ㒣䅼䆎䖛ⱘ㋶ᓩ᪡԰ヺᴹᣛᅮܗ៥ӀՓ⫼Ꮖ㒣ҟ㒡䖛ⱘᷛ䆄߯ᓎњᄫ݌ abDŽ✊ৢ៥ӀՓ⫼೼߫㸼੠ 7.4.1.1 ᅗᅗབԩᎹ԰ VDGGUHVVLVJXLGR#S\WKRQRUJ *XLGR  &RQWDFW*XLGRDWJXLGR#S\WKRQRUJ &RQWDFW/DUU\DWODUU\#ZDOORUJ &RQWDFW0DWVXPRWRDWPDW]#UXE\ODQJRUJ &RQWDFW6ZDURRSDWVZDURRSFK#E\WHRIS\WKRQLQIR  7KHUHDUHFRQWDFWVLQWKHDGGUHVVERRN  VDGGUHVVLVVZDURRSFK#E\WHRIS\WKRQLQIR 6ZDURRS S\WKRQXVLQJBGLFWS\ 䕧ߎ @ *XLGR VDGGUHVVLVVDE> SULQW?Q*XLGR  *XLGR 25DEKDVBNH\LQDE *XLGR LI   QDPHDGGUHVV  &RQWDFWVDWV SULQW  IRUQDPHDGGUHVVLQDELWHPV  DE OHQ ?Q7KHUHDUHGFRQWDFWVLQWKHDGGUHVVERRN?Q SULQW  @ 6SDPPHU GHODE> 'HOHWLQJDNH\YDOXHSDLU   JXLGR#S\WKRQRUJ  @ *XLGR DE> $GGLQJDNH\YDOXHSDLU  @ 6ZDURRS VDGGUHVVLVVDE> SULQW6ZDURRS  `  VSDPPHU#KRWPDLOFRP  6SDPPHU   PDW]#UXE\ODQJRUJ  0DWVXPRWR   ODUU\#ZDOORUJ  /DUU\  39 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 VKRSOLVW VKRSOLVWLV SULQW  GHOP\OLVW>@UHPRYHILUVWLWHP @PDNHDFRS\E\GRLQJDIXOOVOLFHP\OLVW VKRSOLVW>  &RS\E\PDNLQJDIXOOVOLFH SULQW  FRQILUPLQJWKDWWKH\SRLQWWRWKHVDPHREMHFW DSSOH WKH QRWLFHWKDWERWKVKRSOLVWDQGP\OLVWERWKSULQWWKHVDPHOLVWZLWKRXW P\OLVW P\OLVWLV SULQW VKRSOLVW VKRSOLVWLV SULQW  GHOVKRSOLVW>@  P\OLVW VKRSOLVWP\OLVWLVMXVWDQRWKHUQDPHSRLQWLQJWRWKHVDPHREMHFW @ EDQDQD  FDUURW  PDQJR  DSSOH VKRSOLVW >  6LPSOH$VVLJQPHQW SULQW  UHIHUHQFHS\)LOHQDPH XVUELQS\WKRQ ՟ 7.6 ᇍ䈵Ϣখ㗗 7.6.1 ᇍ䈵Ϣখ㗗 ϔ㠀䇈ᴹˈԴϡ䳔㽕ᢙᖗ䖭Ͼˈাᰃ೼খ㗗Ϟ᳝ѯ㒚ᖂⱘᬜᵰ䳔㽕Դ⊼ᛣDŽ䖭Ӯ䗮䖛ϟ䴶䖭Ͼ՟ᄤࡴҹ䇈ᯢDŽ 䙷Ͼᇍ䈵ⱘݙᄬDŽ䖭㹿⿄԰ৡ⿄ࠄᇍ䈵ⱘ㒥ᅮDŽټ䇈ˈব䞣ৡᣛ৥Դ䅵ㅫᴎЁᄬ ᔧԴ߯ᓎϔϾᇍ䈵ᑊ㒭ᅗ䌟ϔϾব䞣ⱘᯊ׭ˈ䖭Ͼব䞣ҙҙ খ㗗 䙷Ͼᇍ䈵ˈ㗠ϡᰃ㸼⼎䖭Ͼᇍ䈵ᴀ䑿ʽгህᰃ 7.6 খখ㗗 㒘ǃ߫㸼੠ᄫヺІDŽܗৃҹ⫼Ⳍৠⱘᮍ⊩䆓䯂 Փ⫼ Python 㾷䞞఼ѸѦഄᇱ䆩ϡৠߛ⠛ᣛᅮ㒘ড়ˈे೼ᦤ⼎ヺϟԴ㛑໳偀Ϟⳟࠄ㒧ᵰDŽᑣ߫ⱘ⼲༛П໘೼ѢԴ ᳝乍Ⳃⱘᑣ߫ߛ⠛DŽ ߛ⠛DŽ䋳᭄⫼೼Ңᑣ߫ሒᓔྟ䅵ㅫⱘԡ㕂DŽ՟བˈshoplist[:-1]Ӯ䖨ಲ䰸њ᳔ৢϔϾ乍Ⳃ໪ࣙ৿᠔خԴৃҹ⫼䋳᭄ 乍Ⳃⱘߛ⠛DŽ㉏Ԑഄˈshoplist[:]䖨ಲᭈϾᑣ߫ⱘᣋ䋱DŽ ℶ೼ԡ㕂 3 ⱘϔϾᑣ߫ߛ⠛ˈ಴ℸ䖨ಲϔϾ৿᳝ϸϾذ䖭ḋˈshoplist[1:3]䖨ಲҢԡ㕂 1 ᓔྟˈࣙᣀԡ㕂 2ˈԚᰃ ߛ⠛໪DŽ ᑣ߫Ңᓔྟԡ㕂 ᓔྟ ˈ߮ད೼ 㒧ᴳ ԡ㕂Пࠡ㒧ᴳDŽेᓔྟԡ㕂ᰃࣙ৿೼ᑣ߫ߛ⠛Ёⱘˈ㗠㒧ᴳԡ㕂㹿ᥦ᭹೼ 42 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ℶ೼ᑣ߫ሒDŽ⊼ᛣˈ䖨ಲⱘذϡᣛᅮ㄀ϔϾ᭄ˈPython ህҢᑣ߫佪ᓔྟDŽབᵰ≵᳝ᣛᅮ㄀ѠϾ᭄ˈ߭ Python Ӯ ߛ⠛᪡԰ヺЁⱘ㄀ϔϾ᭄˄ݦোПࠡ˅㸼⼎ߛ⠛ᓔྟⱘԡ㕂ˈ㄀ѠϾ᭄˄ݦোПৢ˅㸼⼎ߛ⠛ࠄા䞠㒧ᴳDŽབᵰ ヺकߚⳌԐDŽ䆄ԣ᭄ᰃৃ䗝ⱘˈ㗠ݦোᰃᖙ乏ⱘDŽ ߛ⠛᪡԰ヺᰃᑣ߫ৡৢ䎳ϔϾᮍᣀোˈᮍᣀোЁ᳝ϔᇍৃ䗝ⱘ᭄ᄫˈᑊ⫼ݦোߚࡆDŽ⊼ᛣ䖭ϢԴՓ⫼ⱘ㋶ᓩ᪡԰ ᭄㄀ѠϾ乍ⳂDŽצ㋴㗠 shoplist[-2]ᡧপᑣ߫ⱘ ܗϟˈԡ㕂ᰃҢᑣ߫ሒᓔྟ䅵ㅫⱘDŽ಴ℸˈshoplist[-1]㸼⼎ᑣ߫ⱘ᳔ৢϔϾމ㋶ᓩৠḋৃҹᰃ䋳᭄ˈ೼䙷ḋⱘᚙ ㋴DŽܗᡧপ㄀ϔϾ乍Ⳃˈshoplist[3]ᡧপ shoplist ᑣ߫Ёⱘ㄀ಯϾ ᴹᣛᅮϔϾᑣ߫ⱘᯊ׭ˈPython ӮЎԴᡧপᑣ߫Ёᇍᑨԡ㕂ⱘ乍ⳂDŽ䆄ԣˈPython Ң 0 ᓔྟ䅵᭄DŽ಴ℸˈshoplist[0] ៥ӀᴹᄺдབԩՓ⫼㋶ᓩᴹপᕫᑣ߫ЁⱘऩϾ乍ⳂDŽ䖭г㹿⿄԰ᰃϟᷛ᪡԰DŽ↣ᔧԴ⫼ᮍᣀোЁⱘϔϾ᭄ˈܜ佪 7.5.1.1 ᅗᅗབԩᎹ԰ FKDUDFWHUVVWDUWWRHQGLVVZDURRS FKDUDFWHUVWRLVZDURR FKDUDFWHUVWRHQGLVDURRS FKDUDFWHUVWRLVZD @ EDQDQD  FDUURW  PDQJR  DSSOH ,WHPVWDUWWRHQGLV> @ FDUURW  PDQJR ,WHPWRLV> @ EDQDQD  FDUURW ,WHPWRHQGLV> @ FDUURW  PDQJR ,WHPWRLV> ,WHPLVFDUURW ,WHPLVEDQDQD ,WHPLVEDQDQD ,WHPLVFDUURW ,WHPLVPDQJR ,WHPLVDSSOH S\WKRQVHTS\ 䕧ߎ @QDPH> FKDUDFWHUVVWDUWWRHQGLV SULQW @QDPH> FKDUDFWHUVWRLV SULQW @QDPH> FKDUDFWHUVWRHQGLV SULQW @QDPH> FKDUDFWHUVWRLV SULQW  VZDURRS  QDPH 6OLFLQJRQDVWULQJ  @VKRSOLVW> ,WHPVWDUWWRHQGLV SULQW 41 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 2. ໛ӑᑨ䆹ֱᄬ೼Џ໛ӑⳂᔩЁDŽ 1. 䳔㽕໛ӑⱘ᭛ӊ੠Ⳃᔩ⬅ϔϾ߫㸼ᣛᅮDŽ џⱘᮍ⊩ˈ䖭ᕜℷᐌDŽخ䯂乬üü↣ϾҎ䛑᳝݊ އѢ䖭Ͼ䯂乬ˈ៥Ꮖ㒣߯ᓎњϟ䴶䖭Ͼ߫㸼ҹ䇈ᯢ ៥ བԩ䅽ᅗᎹ԰DŽབᵰᰃԴ䆒䅵ⱘ䆱ˈԴৃ㛑ϡӮ䖭ḋᴹ㾷 ೼ᙄᔧഄߚᵤњ䖭Ͼ䯂乬Пৢˈ៥Ӏᓔྟ䆒䅵៥Ӏⱘ⿟ᑣDŽ៥Ӏ߫њϔᓴ㸼ˈ㸼⼎៥Ӏⱘ⿟ᑣᑨ䆹བԩᎹ԰DŽᇍ ໛ӑ˛ټӀབԩ⹂ᅮ䆹໛ӑાѯ᭛ӊ˛໛ӑֱᄬ೼ા䞠˛៥ӀᗢМḋᄬ ᅗDŽ䖯ϔℹⱘߚᵤᰃᖙ䳔ⱘDŽ՟བˈ៥އሑㅵ䖭ᰃϔϾㅔऩⱘ䯂乬ˈԚᰃ䯂乬ᴀ䑿ᑊ≵᳝㒭៥Ӏ䎇໳ⱘֵᙃᴹ㾷 ៥ᦤߎⱘ䯂乬ᰃ˖ ៥ᛇ㽕ϔϾৃҹЎ៥ⱘ᠔᳝䞡㽕᭛ӊ߯ᓎ໛ӑⱘ⿟ᑣDŽ 8.1 䯂乬 ϔѯ⹂ᅲ᳝⫼ⱘџᚙⱘ⿟ᑣDŽ خ Ͼ㛑໳ ៥ӀᏆ㒣ⷨおњ Python 䇁㿔ⱘӫ໮ݙᆍˈ⦄೼៥ӀᇚᴹᄺдϔϟᗢМᡞ䖭ѯݙᆍ㒧ড়䍋ᴹDŽ៥Ӏᇚ䆒䅵㓪ݭϔ 8. ϔϾ Python ᅲ՟ ԰Ўߚ䱨ヺⱘᄫヺІ join ᑣ߫ⱘ乍Ⳃⱘᭈ⋕ⱘᮍ⊩ˈᅗ䖨ಲϔϾ⫳៤ⱘ໻ᄫヺІDŽ find ᮍ⊩⫼ᴹᡒߎ㒭ᅮᄫヺІ೼঺ϔϾᄫヺІЁⱘԡ㕂ˈ៪㗙䖨ಲ-1 ҹ㸼⼎ᡒϡࠄᄤᄫヺІDŽstr ㉏г᳝ҹϔϾ Ẕ偠ϔϾ㒭ᅮᄫヺІᰃ৺Ў঺ϔϾᄫヺІⱘϔ䚼ߚDŽ 䖭䞠ˈ៥ӀⳟࠄՓ⫼њ䆌໮ᄫヺІᮍ⊩DŽstartwith ᮍ⊩ᰃ⫼ᴹ⌟䆩ᄫヺІᰃ৺ҹ㒭ᅮᄫヺІᓔྟDŽin ᪡԰ヺ⫼ᴹ 7.7.1.1 ᅗᅗབԩᎹ԰ B&KLQD B,QGLDB B5XVVLDB %UD]LOB   B B  GHOLPLWHU    @ EDQDQD  FDUURW  PDQJR VKRSOLVWLV> &RS\E\PDNLQJDIXOOVOLFH @ EDQDQD  FDUURW  PDQJR P\OLVWLV> @ EDQDQD  FDUURW  PDQJR VKRSOLVWLV> 6LPSOH$VVLJQPHQW S\WKRQUHIHUHQFHS\ 䕧ߎ QRWLFHWKDWQRZWKHWZROLVWVDUHGLIIHUHQW P\OLVW P\OLVWLV SULQW 43 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 㒭 :LQGRZV ⫼᠋ⱘ⊼䞞 ໛ӑʽخ៥Ӏⱘ䞡㽕᭛ӊ ḍ᥂ੑҸⱘ䕧ߎˈ៥Ӏᠧॄᇍᑨⱘ⍜ᙃˈᰒ⼎໛ӑᰃ৺߯ᓎ៤ࡳDŽདњˈህᰃ䖭ḋ៥ӀᏆ㒣߯ᓎњϔϾ㛮ᴀᴹᇍ ҸüüབᵰੑҸ៤ࡳ䖤㸠ˈᅗ䖨ಲ 0ˈ৺߭ᅗ䖨ಲ䫭䇃োDŽ ೼ ㋏㒳 Ё䖤㸠ੑҸϔḋDŽे೼ shell Ё䖤㸠ੑڣ᳔ৢˈ៥ӀՓ⫼ os.system ߑ᭄ 䖤㸠 ੑҸˈ߽⫼䖭Ͼߑ᭄ህད ৢݡᰃᕙ໛ӑⱘ᭛ӊ੠Ⳃᔩ߫㸼DŽ៥ӀՓ⫼Ꮖ㒣ᄺд䖛ⱘᄫヺІ join ᮍ⊩ᡞ source ߫㸼䕀ᤶЎᄫヺІDŽ ࣙᣀᄤⳂᔩҹঞᄤⳂᔩЁⱘ᭛ӊDŽϸϾ䗝乍ৃҹ㒘ড়៤㓽ݭᔶᓣ-qrDŽ䗝乍ৢ䴶䎳ⴔᕙ߯ᓎⱘ zip ᔦḷⱘৡ⿄ˈ✊ zip ੑҸ᳝ϔѯ䗝乍੠খ᭄DŽ-q 䗝乍⫼ᴹ㸼⼎ zip ੑҸᅝ䴭ഄᎹ԰DŽ-r 䗝乍㸼⼎ zip ੑҸᇍⳂᔩ䗦ᔦഄᎹ԰ˈेᅗ ˄Linux 㒜ッ៪㗙 DOS ᦤ⼎ヺ˅Ё䖤㸠ᅗˈҹẔ偠ᅗᰃ৺Ꮉ԰DŽ ᓎњⳂᷛ zip ᭛ӊⱘৡ⿄DŽ᥹ⴔ៥Ӏ߯ᓎњ zip_command ᄫヺІˈᅗࣙ৿៥Ӏᇚ㽕ᠻ㸠ⱘੑҸDŽԴৃҹ೼ shell ៥ӀՓ⫼ࡴ⊩᪡԰ヺᴹ 㑻䖲 ᄫヺІˈेᡞϸϾᄫヺІ䖲᥹೼ϔ䍋䖨ಲϔϾᮄⱘᄫヺІDŽ䗮䖛䖭⾡ᮍᓣˈ៥Ӏ߯ ᅠܼⳌৠ˅ 㒘˅㉏Ԑ˄Ԛϡܗখ㗗᠟ݠNJࣙ৿೼Դⱘ Python থ㸠⠜ЁDŽ⊼ᛣ䖭ѯᅮࠊϢ⫼Ѣ print 䇁হⱘᅮࠊ˄%ৢ䎳ϔϾ ҹ೼ljPython খ㗗᠟ݠNJЁ㦋ᕫDŽljPythonৃމП䯈ⱘϔϾक䖯ࠊ᳜ӑ᭄᳓ҷˈ݊Ҫձ⃵㉏᥼DŽ䖭ѯᅮࠊⱘ䆺㒚ᚙ time.strftime()ߑ᭄䳔㽕៥Ӏ೼Ϟ䴶ⱘ⿟ᑣЁՓ⫼ⱘ䙷⾡ᅮࠊDŽ%Y Ӯ㹿᮴Ϫ㑾ⱘᑈӑ᠔᳓ҷDŽ%m Ӯ㹿 01 ࠄ 12 time.strftime()ߑ᭄㦋ᕫDŽᅗ䖬ࣙᣀ.zip ᠽሩৡˈᇚ㹿ֱᄬ೼ target_dir ⳂᔩЁDŽ ໛ӑ᭛ӊⱘഄᮍˈᅗ⬅ target_dir ব䞣ᣛᅮDŽzip ᔦḷⱘৡ⿄ᰃⳂࠡⱘ᮹ᳳ੠ᯊ䯈ˈ៥ӀՓ⫼ټⳂᔩᰃ៥Ӏᛇ㽕ᄬ ៥ӀՓ⫼њ os ੠ time ῵ഫˈ᠔ҹ៥Ӏ䕧ܹᅗӀDŽ✊ৢˈ៥Ӏ೼ source ߫㸼Ёᣛᅮ䳔㽕໛ӑⱘ᭛ӊ੠ⳂᔩDŽⳂᷛ ᥹ϟᴹԴᇚⳟࠄ៥Ӏབԩᡞ 䆒䅵 ϔℹϔℹഄ䕀ᤶЎ ҷⷕ DŽ 8.2.1.1 ᅗᅗབԩᎹ԰ ḋˈ៥Ӏህᕫ䇗䆩៥Ӏⱘ⿟ᑣˈे⍜䰸⿟ᑣЁⱘ ⨩⮉ ˄䫭䇃˅DŽ ⦄೼ˈ៥ӀᏆ㒣໘Ѣ⌟䆩⦃㡖њˈ೼䖭Ͼ⦃㡖ˈ៥Ӏ⌟䆩៥Ӏⱘ⿟ᑣᰃ৺ℷ⹂Ꮉ԰DŽབᵰᅗϢ៥Ӏ᠔ᳳᳯⱘϡϔ 6XFFHVVIXOEDFNXSWRPQWHEDFNXS]LS S\WKRQEDFNXSBYHUS\ 䕧ߎ  %DFNXS)$,/(' SULQW HOVH WDUJHW 6XFFHVVIXOEDFNXSWR SULQW   ]LSBFRPPDQG LIRVV\VWHP 46 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 5XQWKHEDFNXS   VRXUFH MRLQ  WDUJHW V V ]LSBFRPPDQG ]LSTU LQ8QL[/LQX[ WRSXWWKHILOHVLQD]LSDUFKLYH :HXVHWKH]LSFRPPDQG   ]LS  <PG+06 WDUJHW WDUJHWBGLUWLPHVWUIWLPH 7KHQDPHRIWKH]LSDUFKLYHLVWKHFXUUHQWGDWHDQGWLPH 7KHILOHVDUHEDFNHGXSLQWRD]LSILOH  XVLQJ 5HPHPEHUWRFKDQJHWKLVWRZKDW\RXZLOOEH PQWHEDFNXS  WDUJHWBGLU 7KHEDFNXSPXVWEHVWRUHGLQDPDLQEDFNXSGLUHFWRU\  VRPHWKLQJOLNHWKDW @RU ?:RUN' U ?'RFXPHQWV& ,I\RXDUHXVLQJ:LQGRZVXVHVRXUFH >U @ KRPHVZDURRSELQ  KRPHVZDURRSE\WH VRXUFH > 7KHILOHVDQGGLUHFWRULHVWREHEDFNHGXSDUHVSHFLILHGLQDOLVW  LPSRUWWLPH LPSRUWRV  EDFNXSBYHUS\)LOHQDPH XVUELQS\WKRQ ՟ 8.1 ໛ӑ㛮ᴀüü⠜ᴀϔ 8.2.1 ⠜ᴀϔ ᮍḜⱘᅲᮑDŽއᔧ៥Ӏ෎ᴀᅠ៤⿟ᑣⱘ䆒䅵ˈ៥Ӏህৃҹ㓪ݭҷⷕњˈᅗᰃᇍ៥Ӏⱘ㾷 ᮍḜއ8.2 㾷㾷 ᭄㒭ᅗDŽ ⊼ᛣԴৃҹՓ⫼ӏԩഄᄬḷੑҸˈা㽕ᅗ᳝ੑҸ㸠⬠䴶ህৃҹњˈ䙷ḋⱘ䆱៥ӀৃҹҢ៥Ӏⱘ㛮ᴀЁӴ䗦খ ⱘ zip ੑҸˈᅗ䗮ᐌ咬䅸ഄ䱣 Linux/Unix থ㸠⠜ᦤկDŽWindows ⫼᠋ৃҹՓ⫼ Info-Zip ⿟ᑣDŽޚ5. ៥ӀՓ⫼ᷛ 4. zip ᄬḷⱘৡ⿄ᰃᔧࠡⱘ᮹ᳳ੠ᯊ䯈DŽ 3. ᭛ӊ໛ӑ៤ϔϾ zip ᭛ӊDŽ 45 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ㋏㒳ϟᎹ԰DŽ ᅗᰃ'\\'ˈ㗠೼ Mac OS ϟᅗᰃ':'DŽՓ⫼ os.sep 㗠䴲Ⳉ᥹Փ⫼ᄫヺˈӮՓ៥Ӏⱘ⿟ᑣ݋᳝⿏ỡᗻˈৃҹ೼Ϟ䗄䖭ѯ ⊼ᛣ os.sep ব䞣ⱘ⫼⊩üü䖭Ӯḍ᥂Դⱘ᪡԰㋏㒳㒭ߎⳂᔩߚ䱨ヺˈे೼ LinuxǃUnix ϟᅗᰃ'/'ˈ೼ Windows ϟ ⿄ⱘⳂᔩDŽབᵰ≵᳝ˈ៥ӀՓ⫼ os.mkdir ߑ᭄߯ᓎDŽ ϸϾ⿟ᑣⱘ໻䚼ߚᰃⳌৠⱘDŽᬍবⱘ䚼ߚЏ㽕ᰃՓ⫼ os.exists ߑ᭄Ẕ偠೼Џ໛ӑⳂᔩЁᰃ৺᳝ҹᔧࠡ᮹ᳳ԰Ўৡ 8.2.2.1 ᅗᅗབԩᎹ԰ 6XFFHVVIXOEDFNXSWRPQWHEDFNXS]LS S\WKRQEDFNXSBYHUS\  6XFFHVVIXOEDFNXSWRPQWHEDFNXS]LS 6XFFHVVIXOO\FUHDWHGGLUHFWRU\PQWHEDFNXS S\WKRQEDFNXSBYHUS\ 䕧ߎ  %DFNXS)$,/(' SULQW HOVH WDUJHW 6XFFHVVIXOEDFNXSWR SULQW   ]LSBFRPPDQG LIRVV\VWHP 5XQWKHEDFNXS   VRXUFH MRLQ  WDUJHW V V ]LSBFRPPDQG ]LSTU LQ8QL[/LQX[ WRSXWWKHILOHVLQD]LSDUFKLYH :HXVHWKH]LSFRPPDQG   ]LS WDUJHW WRGD\RVVHSQRZ 7KHQDPHRIWKH]LSILOH  WRGD\ 6XFFHVVIXOO\FUHDWHGGLUHFWRU\ SULQW WRGD\ PDNHGLUHFWRU\ RVPNGLU  WRGD\ LIQRWRVSDWKH[LVWV WDOUHDG\WKHUH &UHDWHWKHVXEGLUHFWRU\LILWLVQ   +06 QRZ WLPHVWUIWLPH 7KHFXUUHQWWLPHLVWKHQDPHRIWKH]LSDUFKLYH  <PG WRGD\ WDUJHWBGLUWLPHVWUIWLPH 7KHFXUUHQWGD\LVWKHQDPHRIWKHVXEGLUHFWRU\LQWKHPDLQGLUHFWRU\ 48 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 7KHILOHVDUHEDFNHGXSLQWRD]LSILOH  XVLQJ 5HPHPEHUWRFKDQJHWKLVWRZKDW\RXZLOOEH PQWHEDFNXS  WDUJHWBGLU 7KHEDFNXSPXVWEHVWRUHGLQDPDLQEDFNXSGLUHFWRU\  VRPHWKLQJOLNHWKDW @RU ?:RUN' U ?'RFXPHQWV& ,I\RXDUHXVLQJ:LQGRZVXVHVRXUFH >U @ KRPHVZDURRSELQ  KRPHVZDURRSE\WH VRXUFH > 7KHILOHVDQGGLUHFWRULHVWREHEDFNHGXSDUHVSHFLILHGLQDOLVW  LPSRUWWLPH LPSRUWRV  EDFNXSBYHUS\)LOHQDPH XVUELQS\WKRQ ՟ 8.2 ໛ӑ㛮ᴀüü⠜ᴀѠ ಴Ўা᳝೼Դ߯ᓎњ໛ӑˈᠡӮߎ⦄䙷໽ⱘⳂᔩDŽ ⱘ䭓ᑺгৃҹবⷁDŽ䖬᳝ϔϾӬ࢓ᰃ䞛⫼৘㞾⣀ゟⱘ᭛ӊ།ৃҹᐂࡽԴᮍ֓ഄẔ偠Դᰃ৺೼↣ϔ໽߯ᓎњ໛ӑˈ ಴ℸᅗህ᳈ࡴᆍᯧㅵ⧚њDŽ঺໪ϔϾӬ࢓ᰃ᭛ӊৡˈټⱘϔϾӬ࢓ᰃԴⱘ໛ӑӮҹㄝ㑻㒧ᵘᄬخӑⳂᔩЁDŽ䖭ḋ ៥䅸ЎӬ࣪Пϔᰃ䞛⫼᳈དⱘ᭛ӊৡᴎࠊüüՓ⫼ ᯊ䯈 ԰Ў᭛ӊৡˈ㗠ᔧࠡⱘ ᮹ᳳ ԰ЎⳂᔩৡˈᄬᬒ೼Џ໛ ⱘ㓈ᡸ⦃㡖DŽ ѯӬ࣪ҹ֓䅽ᅗ೼៥Ӏⱘ᮹ᐌᎹ԰Ёবᕫ᳈དDŽ䖭⿄Ў䕃ӊخ㄀ϔϾ⠜ᴀⱘ㛮ᴀৃҹᎹ԰DŽ✊㗠ˈ៥Ӏৃҹᇍᅗ 8.2.2 ⠜⠜ᴀѠ ϟˈԴᑨ䆹ಲࠄ䆒䅵⦃㡖៪㗙䇗䆩⿟ᑣDŽމ䆒䅵ᙄᔧˈজ៪㗙Դ೼䕧ܹҷⷕⱘᯊ׭থ⫳њϔ⚍䫭䇃ˈㄝㄝDŽℷᐌᚙ Ϟ䴶ⱘ⿟ᑣৃҹℷ⹂Ꮉ԰ˈԚᰃ˄䗮ᐌ˅㄀ϔϾ⿟ᑣᑊϡᰃϢԴ᠔ᳳᳯⱘᅠܼϔḋDŽ՟བˈৃ㛑᳝ѯ䯂乬Դ≵᳝ ⱘᅲᮑ⦃㡖៪ᓔথ⦃㡖DŽ Linux/Unix ⫼᠋Փ⫼ࠡ䴶ҟ㒡ⱘৃᠻ㸠ⱘᮍ⊩ˈ䖭ḋህৃҹ೼ӏԩഄᮍӏԩᯊ׭䖤㸠໛ӑ㛮ᴀњDŽ䖭㹿⿄Ў䕃ӊ ⦄೼៥ӀᏆ㒣᳝њϔϾৃҹᎹ԰ⱘ໛ӑ㛮ᴀˈ៥Ӏৃҹ೼ӏԩ៥Ӏᛇ㽕ᓎゟ᭛ӊ໛ӑⱘᯊ׭Փ⫼ᅗDŽᓎ䆂 ৡⱘ䕀Нヺ?'ʽ üüԴ೼Փ⫼ϔϾϡⶹ ?'RFXPHQWV& 㗠ϡᰃ ?'RFXPHQWV& ៪ U ??'RFXPHQWV& ᠔ҹˈԴᕫՓ⫼䕀Нヺᴹ㸼⼎ড᭰ᴴᴀ䑿៪㗙Փ⫼㞾✊ᄫヺІDŽ՟བˈՓ⫼ ⼎䕀Нヺʽ ᕫᇣᖗϔѯDŽ䯂乬ᰃ :LQGRZV ᡞড᭰ᴴ˄?˅԰ЎⳂᔩߚ䱨ヺˈ㗠 3\WKRQ ⫼ড᭰ᴴ㸼 Դৃҹᡞ VRXUFH ߫㸼੠ WDUJHW Ⳃᔩ䆒㕂៤ӏԩ᭛ӊ੠ⳂᔩৡˈԚᰃ೼ :LQGRZV ЁԴ 47 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  VRPHWKLQJOLNHWKDW @RU ?:RUN' U ?'RFXPHQWV& ,I\RXDUHXVLQJ:LQGRZVXVHVRXUFH >U @ KRPHVZDURRSELQ  KRPHVZDURRSE\WH VRXUFH > 7KHILOHVDQGGLUHFWRULHVWREHEDFNHGXSDUHVSHFLILHGLQDOLVW  LPSRUWWLPH LPSRUWRV  EDFNXSBYHUS\)LOHQDPH XVUELQS\WKRQ ՟ 8.4 ໛ӑ㛮ᴀüü⠜ᴀಯ 8.2.4 ⠜ᴀಯ Փ⫼⠽⧚㸠ሒⱘড᭰ᴴᴹ㸼⼎䘏䕥㸠೼ϟϔ⠽⧚㸠㒻㓁DŽ᠔ҹˈ៥Ӏׂℷњ⿟ᑣDŽ䖭㹿⿄Ўׂ䅶DŽ ෎ᴀϞˈPython থ⦄ࡴ⊩᪡԰ヺ˄ˇ˅೼䙷ϔ䘏䕥㸠≵᳝ӏԩ᪡԰᭄ˈ಴ℸᅗϡⶹ䘧䆹བԩ㒻㓁DŽ䆄ԣ៥Ӏৃҹ 䗮䖛Ҩ㒚ⱘ㾖ᆳˈ៥Ӏথ⦄ϔϾ䘏䕥㸠㹿ߚ៤њϸϾ⠽⧚㸠ˈԚᰃ៥Ӏᑊ≵᳝ᣛᯢ䖭ϸϾ⠽⧚㸠ሲѢৠϔ䘏䕥㸠DŽ 㒭ߎⱘ䫭䇃ⱘᯊ׭ˈᅗгਞ䆝њ៥ӀᅗẔ⌟ߎ䫭䇃ⱘԡ㕂DŽ᠔ҹ៥ӀҢ䙷㸠ᓔྟ 䇗䆩 ៥Ӏⱘ⿟ᑣDŽ 䖭Ͼ⿟ᑣϡᎹ԰ʽPython 䇈᳝ϔϾ䇁⊩䫭䇃ˈ䖭ᛣੇⴔ㛮ᴀϡ⒵䎇 Python ৃҹ䆚߿ⱘ㒧ᵘDŽᔧ៥Ӏ㾖ᆳ Python ᅗᅗབԩ˄ϡ˅Ꮉ԰ LQYDOLGV\QWD[6\QWD[(UURU A  B WDUJHW WRGD\RVVHSQRZ )LOHEDFNXSBYHUS\OLQH S\WKRQEDFNXSBYHUS\ 䕧ߎ  %DFNXS)$,/(' SULQW HOVH WDUJHW 6XFFHVVIXOEDFNXSWR SULQW   ]LSBFRPPDQG LIRVV\VWHP 5XQWKHEDFNXS   VRXUFH MRLQ  WDUJHW V V ]LSBFRPPDQG ]LSTU LQ8QL[/LQX[ WRSXWWKHILOHVLQD]LSDUFKLYH :HXVHWKH]LSFRPPDQG  WRGD\ 6XFFHVVIXOO\FUHDWHGGLUHFWRU\ SULQW 50 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 WRGD\ PDNHGLUHFWRU\ RVPNGLU  WRGD\ LIQRWRVSDWKH[LVWV WDOUHDG\WKHUH &UHDWHWKHVXEGLUHFWRU\LILWLVQ   ]LS  B   FRPPHQWUHSODFH  B WDUJHW WRGD\RVVHSQRZ HOVH  ]LS WDUJHW WRGD\RVVHSQRZ FKHFNLIDFRPPHQWZDVHQWHUHG  FRPPHQW LIOHQ  (QWHUDFRPPHQW! FRPPHQW UDZBLQSXW 7DNHDFRPPHQWIURPWKHXVHUWRFUHDWHWKHQDPHRIWKH]LSILOH   +06 QRZ WLPHVWUIWLPH 7KHFXUUHQWWLPHLVWKHQDPHRIWKH]LSDUFKLYH  <PG WRGD\ WDUJHWBGLUWLPHVWUIWLPH 7KHFXUUHQWGD\LVWKHQDPHRIWKHVXEGLUHFWRU\LQWKHPDLQGLUHFWRU\ 7KHILOHVDUHEDFNHGXSLQWRD]LSILOH  XVLQJ 5HPHPEHUWRFKDQJHWKLVWRZKDW\RXZLOOEH PQWHEDFNXS  WDUJHWBGLU 7KHEDFNXSPXVWEHVWRUHGLQDPDLQEDFNXSGLUHFWRU\  VRPHWKLQJOLNHWKDW @RU ?:RUN' U ?'RFXPHQWV& ,I\RXDUHXVLQJ:LQGRZVXVHVRXUFH >U @ KRPHVZDURRSELQ  KRPHVZDURRSE\WH VRXUFH > 7KHILOHVDQGGLUHFWRULHVWREHEDFNHGXSDUHVSHFLILHGLQDOLVW  LPSRUWWLPH LPSRUWRV  EDFNXSBYHUS\)LOHQDPH XVUELQS\WKRQ ՟ 8.3 ໛ӑ㛮ᴀüü⠜ᴀϝ˄ϡᎹ԰ʽ˅ ⱘৡ⿄㘨㋏䍋ᴹDŽ䖭ৃҹ䗮䖛೼ zip ᔦḷৡϞ䰘ᏺϔϾ⫼᠋ᦤկⱘ⊼䞞ᴹᮍ֓ഄᅲ⦄DŽ њϔѯ䞡㽕ⱘᬍবˈѢᰃ៥ᛇ㽕ᡞ䖭ѯᬍবϢ zip ᔦḷخ〓ⱘˈӮবᕫकߚೄ䲒ʽ՟བˈ៥ৃ㛑ᇍ⿟ᑣ៪㗙ⓨ䆆 䕗໮໛ӑⱘᯊ׭䖬Ꮉ԰ᕫϡ䫭ˈԚᰃབᵰ᳝ᵕ໮໛ӑⱘᯊ׭ˈ៥থ⦄㽕ऎߚ↣Ͼ໛ӑᰃᑆҔМخ㄀ѠϾ⠜ᴀ೼៥ 8.2.3 ⠜⠜ᴀϝ 49 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ߑ᭄ˈᅗᆍᯧᓩথϹ䞡ⱘ䫭䇃DŽ њՓ⫼RVV\VWHP 䖭Ͼϡ᥼㤤Փ⫼ⱘܡᑧⱘϔ䚼ߚˈৃҹկԴՓ⫼DŽՓ⫼䖭ѯᑧህ䙓 ޚ᳔⧚ᛇⱘ߯ᓎ䖭ѯᔦḷⱘᮍ⊩ᰃߚ߿Փ⫼ ]LSILOH ੠ WDUILOHDŽᅗӀᰃ 3\WKRQ ᷛ 䞡㽕 ࣙᣀ᠔᳝ҹ~㒧ሒⱘ᭛ӊDŽ  -X 㸼⼎৿೼ᣛᅮ᭛ӊৡ߫㸼Ёⱘ᭛ӊӮ㹿ᥦ䰸೼໛ӑП໪DŽ՟བˈԴৃҹ೼᭛ӊЁᣛᅮ*~ˈҢ㗠ϡ䅽໛ӑ  -f 㸼⼎ᔎ䖿߯ᓎᔦḷˈेབᵰᏆ㒣᳝ϔϾৠৡ᭛ӊˈᅗӮ㹿᳓ᤶDŽ  -z 㸼⼎Փ⫼ gzip Ⓒ⊶఼DŽ  -v 㸼⼎ѸѦˈेੑҸ᳈݋ѸѦᗻDŽ  -c 㸼⼎߯ᓎϔϾᔦḷDŽ 䗝乍㾷䞞བϟ˖  VUFGLU MRLQ  WDUJHW   WDU FY]I V V ; KRPHVZDURRSH[FOXGHVW[W  WDU ੑҸᄫヺІ⦄೼ᇚ⿄Ў˖ ໻໮᭄ Linux/Unix ㋏㒳Ё䛑ᰃ咬䅸ৃ⫼ⱘDŽWindows ⫼᠋гৃҹϟ䕑ᅝ㺙ᅗDŽ ໛ӑӮ᳈ᖿ᳈ᇣDŽབᵰԴᛇ㽕೼ Windows ЁՓ⫼䖭ѯᔦḷˈWinZip г㛑ᮍ֓ഄ໘⧚䖭ѯ.tar.gz ᭛ӊDŽtar ੑҸ೼ ⱘϔϾӬ࢓ᰃ೼Դ㒧ড়Փ⫼ tar ੠ gzip ੑҸⱘᯊ׭ˈخ៥䖬Ꮰᳯ᳝ⱘϔϾӬ࣪ᰃՓ⫼ tar ੑҸ᳓ҷ zip ੑҸDŽ䖭ḋ ৢ៥ӀৃҹՓ⫼ list ㉏ᦤկⱘ extend ᮍ⊩ᡞᅗӀࡴࠄ source ߫㸼ЁএDŽ ঺ϔϾৃ㛑ⱘᬍ䖯ᰃՓ᭛ӊ੠Ⳃᔩ㛑໳䗮䖛ੑҸ㸠Ⳉ᥹Ӵ䗦㒭㛮ᴀDŽ៥Ӏৃҹ䗮䖛 sys.argv ߫㸼ᴹ㦋পᅗӀˈ✊ ⿟ᑣЁࣙ৿ ѸѦ ⿟ᑺüüԴৃҹ⫼-v 䗝乍ᴹՓԴⱘ⿟ᑣ᳈݋ѸѦᗻDŽ ᇍѢ໻໮᭄⫼᠋ᴹ䇈ˈ㄀ಯϾ⠜ᴀᰃϔϾ⒵ᛣⱘᎹ԰㛮ᴀњˈԚᰃᅗҡ✊᳝䖯ϔℹᬍ䖯ⱘぎ䯈DŽ↨བˈԴৃҹ೼ 8.2.5 䖯ϔℹӬ࣪ ϟߦ㒓üü䖭ᰃ಴Ў໘⧚䖭ḋⱘ᭛ӊৡ㽕ᆍᯧᕫ໮DŽ ✊㗠ˈབᵰᦤկњ⊼䞞ˈ䙷МᅗӮ㹿䰘ࡴࠄ zip ᔦḷৡˈህ೼.zip ᠽሩৡПࠡDŽ⊼ᛣ៥Ӏᡞ⊼䞞ЁⱘぎḐ᳓ᤶ៤ ҔМ⡍߿ⱘׂᬍ˅ˈ䙷М៥ӀህབПࠡ䙷ḋ㒻㓁᪡԰DŽخᛃ՟໛ӑˈ≵᳝ ৢ䗮䖛 len ߑ᭄ᡒߎ䕧ܹⱘ䭓ᑺҹẔ偠⫼᠋ᰃ৺⹂ᅲ䕧ܹњҔМϰ㽓DŽབᵰ⫼᠋াᰃᣝњಲ䔺˄↨བ䖭াᰃϔϾ 䖭Ͼ⿟ᑣ⦄೼Ꮉ԰њʽ䅽៥Ӏⳟϔϟ⠜ᴀϝЁ԰ߎⱘᅲ䋼ᗻᬍ䖯DŽ៥ӀՓ⫼ raw_input ߑ᭄ᕫࠄ⫼᠋ⱘ⊼䞞ˈ✊ 8.2.4.1 ᅗᅗབԩᎹ԰ 6XFFHVVIXOEDFNXSWRPQWHEDFNXS]LS (QWHUDFRPPHQW! S\WKRQEDFNXSBYHUS\ 52 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  6XFFHVVIXOEDFNXSWRPQWHEDFNXSBDGGHGBQHZBH[DPSOHV]LS (QWHUDFRPPHQW!DGGHGQHZH[DPSOHV S\WKRQEDFNXSBYHUS\ 䕧ߎ  %DFNXS)$,/(' SULQW HOVH WDUJHW 6XFFHVVIXOEDFNXSWR SULQW   ]LSBFRPPDQG LIRVV\VWHP 5XQWKHEDFNXS   VRXUFH MRLQ  WDUJHW V V ]LSBFRPPDQG ]LSTU LQ8QL[/LQX[ WRSXWWKHILOHVLQD]LSDUFKLYH :HXVHWKH]LSFRPPDQG  WRGD\ 6XFFHVVIXOO\FUHDWHGGLUHFWRU\ SULQW WRGD\ PDNHGLUHFWRU\ RVPNGLU  WRGD\ LIQRWRVSDWKH[LVWV WDOUHDG\WKHUH &UHDWHWKHVXEGLUHFWRU\LILWLVQ  1RWLFHWKHEDFNVODVK  ]LS  B   FRPPHQWUHSODFH ? B WDUJHW WRGD\RVVHSQRZ HOVH  ]LS WDUJHW WRGD\RVVHSQRZ FKHFNLIDFRPPHQWZDVHQWHUHG  FRPPHQW LIOHQ  (QWHUDFRPPHQW! FRPPHQW UDZBLQSXW 7DNHDFRPPHQWIURPWKHXVHUWRFUHDWHWKHQDPHRIWKH]LSILOH   +06 QRZ WLPHVWUIWLPH 7KHFXUUHQWWLPHLVWKHQDPHRIWKH]LSDUFKLYH  <PG WRGD\ WDUJHWBGLUWLPHVWUIWLPH 7KHFXUUHQWGD\LVWKHQDPHRIWKHVXEGLUHFWRU\LQWKHPDLQGLUHFWRU\ 7KHILOHVDUHEDFNHGXSLQWRD]LSILOH  XVLQJ 5HPHPEHUWRFKDQJHWKLVWRZKDW\RXZLOOEH PQWHEDFNXS  WDUJHWBGLU 7KHEDFNXSPXVWEHVWRUHGLQDPDLQEDFNXSGLUHFWRU\ 51 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ⱘᮍ⊩˅DŽЎњ偠䆕ˈ៥Ӏㅔऩഄᠧॄњ䖭Ͼব䞣ⱘ㉏ൟDŽᅗਞ䆝៥Ӏ៥ӀᏆ㒣೼__main__῵ഫЁ᳝њϔϾ Person ᥹ϟᴹˈ៥ӀՓ⫼㉏ৡৢ䎳ϔᇍ೚ᣀোᴹ߯ᓎϔϾᇍ䈵/ᅲ՟DŽ˄៥Ӏᇚ೼ϟ䴶ⱘゴ㡖Ёᄺд᳈໮ⱘབԩ߯ᓎᅲ՟ ӀՓ⫼њϔϾぎⱑഫˈᅗ⬅ pass 䇁হ㸼⼎DŽ ៥ӀՓ⫼ class 䇁হৢ䎳㉏ৡˈ߯ᓎњϔϾᮄⱘ㉏DŽ䖭ৢ䴶䎳ⴔϔϾ㓽䖯ⱘ䇁হഫᔶ៤㉏ԧDŽ೼䖭Ͼ՟ᄤЁˈ៥ 9.3.1.1 ᅗབԩᎹ԰ BBPDLQBB3HUVRQLQVWDQFHDW[IIFEF! S\WKRQVLPSOHVWFODVVS\ 䕧ߎ SULQWS  S 3HUVRQ  SDVV$QHPSW\EORFN FODVV3HUVRQ  VLPSOHVWFODVVS\)LOHQDPH XVUELQS\WKRQ ՟ 9.1 ߯ᓎϔϾ㉏ 9.3.1 ߯ᓎϔϾ㉏ ϔϾሑৃ㛑ㅔऩⱘ㉏བϟ䴶䖭Ͼ՟ᄤ᠔⼎DŽ 9.3 ㉏㉏ 䖭гᛣੇⴔབᵰԴ᳝ϔϾϡ䳔㽕খ᭄ⱘᮍ⊩ˈԴ䖬ᰃᕫ㒭䖭Ͼᮍ⊩ᅮНϔϾ self খ᭄DŽ 䖭Ӯ⬅ Python 㞾ࡼ䕀Ў MyClass.method(MyObject, arg1, arg2)üü䖭ህᰃ self ⱘॳ⧚њDŽ ㉏⿄Ў MyClass ੠䖭Ͼ㉏ⱘϔϾᅲ՟ MyObjectDŽᔧԴ䇗⫼䖭Ͼᇍ䈵ⱘᮍ⊩ MyObject.method(arg1, arg2)ⱘᯊ׭ˈ བԴ᳝ϔϾ؛Դϔᅮᕜ༛ᗾ Python བԩ㒭 self 䌟ؐҹঞЎԩԴϡ䳔㽕㒭ᅗ䌟ؐDŽВϔϾ՟ᄤӮՓℸবᕫ⏙᱄DŽ 3\WKRQ Ёⱘ VHOI ㄝӋѢ &Ёⱘ VHOI ᣛ䩜੠ -DYDǃ&Ёⱘ WKLV খ㗗DŽ 㒭 &-DYD&⿟ᑣਬⱘ⊼䞞 гৃҹᐂࡽԴDŽ ⱘৡ⿄᳝ᕜ໮Ӭ⚍üüԴⱘ⿟ᑣ䇏㗙ৃҹ䖙䗳䆚߿ᅗˈབᵰՓ⫼ self ⱘ䆱ˈ䖬᳝ѯ IDE˄䲚៤ᓔথ⦃๗˅ޚϔϾᷛ 㱑✊Դৃҹ㒭䖭Ͼখ᭄ӏԩৡ⿄ˈԚᰃ ᔎ⚜ᓎ䆂 ԴՓ⫼ self 䖭Ͼৡ⿄üü݊Ҫৡ⿄䛑ᰃϡ䌲៤ԴՓ⫼ⱘDŽՓ⫼ ᯊ׭ԴϡЎ䖭Ͼখ᭄䌟ؐˈPython Ӯᦤկ䖭ϾؐDŽ䖭Ͼ⡍߿ⱘব䞣ᣛᇍ䈵ᴀ䑿ˈᣝ✻ᛃ՟ᅗⱘৡ⿄ᰃ selfDŽ ㉏ⱘᮍ⊩Ϣ᱂䗮ⱘߑ᭄া᳝ϔϾ⡍߿ⱘऎ߿üüᅗӀᖙ乏᳝ϔϾ乱໪ⱘ㄀ϔϾখ᭄ৡ⿄ˈԚᰃ೼䇗⫼䖭Ͼᮍ⊩ⱘ 9.2 self 54 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ㉏Փ⫼ class ݇䬂ᄫ߯ᓎDŽ㉏ⱘඳ੠ᮍ⊩㹿߫೼ϔϾ㓽䖯ഫЁDŽ ඳ᳝ϸ⾡㉏ൟüüሲѢ↣Ͼᅲ՟/㉏ⱘᇍ䈵៪ሲѢ㉏ᴀ䑿DŽᅗӀߚ߿㹿⿄Ўᅲ՟ব䞣੠㉏ব䞣DŽ ᮍ⊩ৃҹড়⿄Ў㉏ⱘሲᗻDŽ ⱘߑ᭄ᴹ݋᳝ࡳ㛑DŽ䖭ḋⱘߑ᭄㹿⿄Ў㉏ⱘᮍ⊩DŽ䖭ѯᴃ䇁ᐂࡽ៥ӀᡞᅗӀϢᄸゟⱘߑ᭄੠ব䞣ऎߚᓔᴹDŽඳ੠ ᭄᥂DŽሲѢϔϾᇍ䈵៪㉏ⱘব䞣㹿⿄ЎඳDŽᇍ䈵гৃҹՓ⫼ ሲѢ ㉏ټᇍ䈵ৃҹՓ⫼᱂䗮ⱘ ሲѢ ᇍ䈵ⱘব䞣ᄬ ⿟ᑣਬӮ❳ᙝ䖭Ͼὖᗉˈ಴Ўᅗ㉏ԐϢᇕ㺙Ϣ㾷ᇕ㺙ⱘὖᗉDŽ LQW њ㾷᳈໮䖭Ͼ㉏ⱘ䆺ᚙDŽ&੠ -DYD ᭈ᭄㒃㊍԰Ў㉏ൟᰃϡৠⱘDŽ䗮䖛 KHOS ⊼ᛣˈे֓ᰃᭈ᭄г㹿԰Ўᇍ䈵˄ሲѢ LQW ㉏˅DŽ䖭੠ &ǃ-DYD˄ ⠜Пࠡ˅ᡞ 㒭 &&-DYD&⿟ᑣਬⱘ⊼䞞 ᭈ᭄ⱘব䞣ᰃ int ㉏ⱘᅲ՟˄ᇍ䈵˅DŽټ㉏ൟⱘব䞣ˈ䖭ᄬ ㉏੠ᇍ䈵ᰃ䴶৥ᇍ䈵㓪⿟ⱘϸϾЏ㽕ᮍ䴶DŽ㉏߯ᓎϔϾᮄ㉏ൟˈ㗠ᇍ䈵䖭Ͼ㉏ⱘ ᅲ՟ DŽ䖭㉏ԐѢԴ᳝ϔϾ int ᮍḜⱘᯊ׭ˈԴህᕫՓ⫼䴶৥ᇍ䈵ⱘ㓪⿟ᡔᴃDŽއ ⿟⧚ᗉDŽ೼໻໮᭄ᯊ׭ԴৃҹՓ⫼䖛⿟ᗻ㓪⿟ˈԚᰃ᳝ѯᯊ׭ᔧԴᛇ㽕㓪ݭ໻ൟ⿟ᑣ៪ᰃᇏ∖ϔϾ᳈ࡴড়䗖ⱘ㾷 ⿟DŽ䖬᳝ϔ⾡ᡞ᭄᥂੠ࡳ㛑㒧ড়䍋ᴹˈ⫼⿄Ўᇍ䈵ⱘϰ㽓ࣙ㻍䍋ᴹ㒘㒛⿟ᑣⱘᮍ⊩DŽ䖭⾡ᮍ⊩⿄Ў 䴶৥ᇍ䈵ⱘ 㓪 ࠄⳂࠡЎℶˈ೼៥Ӏⱘ⿟ᑣЁˈ៥Ӏ䛑ᰃḍ᥂᪡԰᭄᥂ⱘߑ᭄៪䇁হഫᴹ䆒䅵⿟ᑣⱘDŽ䖭㹿⿄Ў 䴶৥䖛⿟ⱘ 㓪 9.1 ㅔҟ 9. 䴶৥ᇍ䈵ⱘ㓪⿟ ӊᰃ䭓ߎᴹⱘˈ㗠ϡᰃᓎ䗴ⱘāDŽ ࡴӏԩԴᛇ㽕ⱘ⡍ᗻˈḍ᥂䳔㽕ϔ⃵⃵䞡໡䖭Ͼ㓪ݭˉ⌟䆩ˉՓ⫼ⱘ਼ᳳDŽ䆄ԣĀ䕃 ᮑϔϾㅔऩⱘ⠜ᴀDŽᇍᅗ䖯㸠⌟䆩Ϣ䇗䆩DŽՓ⫼ᅗҹ⹂ֵᅗབ乘ᳳ䙷ḋഄᎹ԰DŽݡ๲ ៥Ӏ߯ᓎ䖭Ͼ໛ӑ㛮ᴀⱘ䖛⿟ᰃ㓪ݭ⿟ᑣⱘ᥼㤤ᮍ⊩üü䖯㸠ߚᵤϢ䆒䅵DŽᓔྟᯊᅲ 䞡㽕 6. 㓈ᡸ˄Ӭ࣪˅ 5. Փ⫼˄ᅲᮑ៪ᓔথ˅ 4. ⌟䆩˄⌟䆩Ϣ䇗䆩˅ 3. 㓪ݭ˄ᅲᮑ˅ 2. བԩ˄䆒䅵˅ 1. ҔМ˄ߚᵤ˅ ⦄೼ˈ៥ӀᏆ㒣䍄䖛њ㓪ݭϔϾ䕃ӊⱘ৘Ͼ⦃㡖DŽ䖭ѯ⦃㡖ৃҹὖᣀབϟ˖ 8.3 䕃䕃ӊᓔথ䖛⿟ ḋⱘ䆱ˈ՟ᄤህৃҹㅔऩࠄ䅽↣ϾҎ䛑㛑໳⧚㾷ˈৠᯊгᏆ㒣䎇໳⫼њDŽ ✊㗠ˈ៥೼ᴀ㡖ЁՓ⫼ RVV\VWHP ⱘᮍ⊩ᴹ߯ᓎ໛ӑˈ䖭㒃㊍ᰃЎњᬭᄺⱘ䳔㽕DŽ䖭 53 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ᇍ䈵ⱘব䞣 ⬅㉏ⱘ↣Ͼᇍ䈵/ᅲ՟ᢹ᳝DŽ಴ℸ↣Ͼᇍ䈵᳝㞾Ꮕᇍ䖭Ͼඳⱘϔӑᣋ䋱ˈेᅗӀϡᰃ݅ѿⱘˈ೼ৠϔ ࡼⱘᯊ׭ˈ䖭ϾᬍࡼӮড᯴ࠄ᠔᳝݊Ҫⱘᅲ՟ϞDŽ њᬍخ㉏ⱘব䞣 ⬅ϔϾ㉏ⱘ᠔᳝ᇍ䈵˄ᅲ՟˅݅ѿՓ⫼DŽা᳝ϔϾ㉏ব䞣ⱘᣋ䋱ˈ᠔ҹᔧᶤϾᇍ䈵ᇍ㉏ⱘব䞣 ᳝ϸ⾡㉏ൟⱘ ඳ üü㉏ⱘব䞣੠ᇍ䈵ⱘব䞣ˈᅗӀḍ᥂ᰃ㉏䖬ᰃᇍ䈵 ᢹ᳝ 䖭Ͼব䞣㗠ऎߚDŽ 䯈 㒥ᅮ ⱘ᱂䗮ব䞣ˈे䖭ѯৡ⿄া೼䖭ѯ㉏Ϣᇍ䈵ⱘࠡᦤϟ᳝ᬜDŽ ៥ӀᏆ㒣䅼䆎њ㉏Ϣᇍ䈵ⱘࡳ㛑䚼ߚˈ⦄೼៥Ӏᴹⳟϔϟᅗⱘ᭄᥂䚼ߚDŽџᅲϞˈᅗӀাᰃϢ㉏੠ᇍ䈵ⱘৡ⿄ぎ 9.6 ㉏Ϣᇍ䈵ⱘᮍ⊩ BBLQLWBBᮍ⊩㉏ԐѢ &ǃ&੠ -DYD ЁⱘFRQVWUXFWRUDŽ 㒭 &-DYD&⿟ᑣਬⱘ⊼䞞 ⦄೼ˈ៥Ӏ㛑໳೼៥Ӏⱘᮍ⊩ЁՓ⫼ self.name ඳDŽ䖭೼ sayHi ᮍ⊩Ёᕫࠄњ偠䆕DŽ ㉏ৡৢ䴶ˈҢ㗠Ӵ䗦㒭__init__ᮍ⊩DŽ䖭ᰃ䖭⾡ᮍ⊩ⱘ䞡㽕П໘DŽ ᳔䞡㽕ⱘᰃˈ៥Ӏ≵᳝ϧ䮼䇗⫼__init__ᮍ⊩ˈাᰃ೼߯ᓎϔϾ㉏ⱘᮄᅲ՟ⱘᯊ׭ˈᡞখ᭄ࣙᣀ೼೚ᣀোݙ䎳೼ Ͼᮄⱘඳˈг⿄Ў nameDŽ⊼ᛣᅗӀᰃϸϾϡৠⱘব䞣ˈሑㅵᅗӀ᳝ⳌৠⱘৡᄫDŽ⚍োՓ៥Ӏ㛑໳ऎߚᅗӀDŽ 䖭䞠ˈ៥Ӏᡞ__init__ᮍ⊩ᅮНЎপϔϾখ᭄ name˄ҹঞ᱂䗮ⱘখ᭄ self˅DŽ೼䖭Ͼ__init__䞠ˈ៥Ӏাᰃ߯ᓎϔ 9.5.1.1 ᅗᅗབԩᎹ԰ +HOORP\QDPHLV6ZDURRS S\WKRQFODVVBLQLWS\ 䕧ߎ  VD\+L 6ZDURRS 7KLVVKRUWH[DPSOHFDQDOVREHZULWWHQDV3HUVRQ   SVD\+L  6ZDURRS S 3HUVRQ  VHOIQDPH +HOORP\QDPHLV SULQW  VHOI GHIVD\+L VHOIQDPH QDPH  VHOIQDPH GHIBBLQLWBB FODVV3HUVRQ  FODVVBLQLWS\)LOHQDPH XVUELQS\WKRQ 56 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ՟ 9.3 Փ⫼__init__ᮍ⊩ 9.5.1 Փ⫼__init__ᮍ⊩ ᛣˈ䖭Ͼৡ⿄ⱘᓔྟ੠㒧ሒ䛑ᰃঠϟߦ㒓DŽ ϔѯԴᏠᳯⱘ ߱ྟ࣪ DŽ⊼خ__init__ᮍ⊩೼㉏ⱘϔϾᇍ䈵㹿ᓎゟᯊˈ偀Ϟ䖤㸠DŽ䖭Ͼᮍ⊩ৃҹ⫼ᴹᇍԴⱘᇍ䈵 ೼ Python ⱘ㉏Ё᳝ᕜ໮ᮍ⊩ⱘৡᄫ᳝⡍⅞ⱘ䞡㽕ᛣНDŽ⦄೼៥Ӏᇚᄺд__init__ᮍ⊩ⱘᛣНDŽ 9.5 __init__ᮍ⊩ 䖭䞠៥Ӏⳟࠄњ self ⱘ⫼⊩DŽ⊼ᛣ sayHi ᮍ⊩≵᳝ӏԩখ᭄ˈԚҡ✊೼ߑ᭄ᅮНᯊ᳝ selfDŽ 9.4.1.1 ᅗབԩᎹ԰ +HOORKRZDUH\RX“ S\WKRQPHWKRGS\ 䕧ߎ  VD\+L 7KLVVKRUWH[DPSOHFDQDOVREHZULWWHQDV3HUVRQ   SVD\+L  S 3HUVRQ   +HOORKRZDUH\RX“ SULQW  VHOI GHIVD\+L FODVV3HUVRQ  PHWKRGS\)LOHQDPH XVUELQS\WKRQ ՟ 9.2 Փ⫼ᇍ䈵ⱘᮍ⊩ 9.4.1 Փ⫼ᇍ䈵ⱘᮍ⊩ ӀᴹᄺдϔϾ՟ᄤDŽ ߑ᭄ϔḋⱘᮍ⊩ˈ䖭ѯᮍ⊩Ϣߑ᭄ⱘऎ߿াᰃϔϾ乱໪ⱘ self ব䞣DŽ⦄೼៥ڣ៥ӀᏆ㒣䅼䆎њ㉏/ᇍ䈵ৃҹᢹ᳝ 9.4 ᇍᇍ䈵ⱘᮍ⊩ ᇍ䈵DŽټৃҹ೼ӏԩぎԡᄬ ᇍ䈵ⱘ䅵ㅫᴎݙᄬഄഔгᠧॄњߎᴹDŽ䖭Ͼഄഔ೼Դⱘ䅵ㅫᴎϞӮᰃ঺໪ϔϾؐˈ಴Ў Pythonټৃҹ⊼ᛣࠄᄬ ㉏ⱘᅲ՟DŽ 55 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ៥Ӏ๲ࡴњϔϾҎDŽৠḋৃҹথ⦄ˈself.name ⱘؐḍ᥂↣Ͼᇍ䈵ᣛᅮˈ䖭㸼ᯢњᅗ԰Ўᇍ䈵ⱘব䞣ⱘᴀ䋼DŽ 㾖ᆳৃҹথ⦄__init__ᮍ⊩⫼ϔϾৡᄫᴹ߱ྟ࣪ Person ᅲ՟DŽ೼䖭Ͼᮍ⊩Ёˈ៥Ӏ䅽 population ๲ࡴ 1ˈ䖭 ᰃ ಴ Ў ㉏ⱘব䞣DŽname ব䞣ሲѢᇍ䈵˄ᅗՓ⫼ self 䌟ؐ˅಴ℸᰃᇍ䈵ⱘব䞣DŽ 䖭ᰃϔϾᕜ䭓ⱘ՟ᄤˈԚᰃᅗ᳝ࡽѢ䇈ᯢ㉏Ϣᇍ䈵ⱘব䞣ⱘᴀ䋼DŽ䖭䞠ˈpopulation ሲѢ Person ㉏ˈ಴ℸᰃϔϾ 9.6.1.1 ᅗᅗབԩᎹ԰ ,DPWKHODVWRQH 6ZDURRSVD\VE\H 7KHUHDUHVWLOOSHRSOHOHIW $EGXO.DODPVD\VE\H :HKDYHSHUVRQVKHUH +LP\QDPHLV6ZDURRS :HKDYHSHUVRQVKHUH +LP\QDPHLV$EGXO.DODP  ,QLWLDOL]LQJ$EGXO.DODP ,DPWKHRQO\SHUVRQKHUH +LP\QDPHLV6ZDURRS  ,QLWLDOL]LQJ6ZDURRS S\WKRQREMYDUS\ 䕧ߎ  VZDURRSKRZ0DQ\  VZDURRSVD\+L   NDODPKRZ0DQ\  NDODPVD\+L  $EGXO.DODP NDODP 3HUVRQ   VZDURRSKRZ0DQ\  VZDURRSVD\+L  6ZDURRS VZDURRS 3HUVRQ  3HUVRQSRSXODWLRQ :HKDYHGSHUVRQVKHUH SULQW HOVH  ,DPWKHRQO\SHUVRQKHUH SULQW  LI3HUVRQSRSXODWLRQ  3ULQWVWKHFXUUHQWSRSXODWLRQ  58 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  VHOI GHIKRZ0DQ\  VHOIQDPH +LP\QDPHLVV SULQW  VDOOLWGRHV 5HDOO\WKDW  *UHHWLQJE\WKHSHUVRQ   VHOI GHIVD\+L  3HUVRQSRSXODWLRQ 7KHUHDUHVWLOOGSHRSOHOHIW SULQW HOVH  ,DPWKHODVWRQH SULQW  LI3HUVRQSRSXODWLRQ   3HUVRQSRSXODWLRQ  VHOIQDPH VVD\VE\H SULQW  ,DPG\LQJ   VHOI GHIBBGHOBB   3HUVRQSRSXODWLRQ DGGVWRWKHSRSXODWLRQ :KHQWKLVSHUVRQLVFUHDWHGKHVKH  VHOIQDPH ,QLWLDOL]LQJV SULQW VHOIQDPH QDPH  VGDWD ,QLWLDOL]HVWKHSHUVRQ   VHOIQDPH GHIBBLQLWBB   SRSXODWLRQ  5HSUHVHQWVDSHUVRQ  FODVV3HUVRQ  REMYDUS\)LOHQDPH XVUELQS\WKRQ ՟ 9.4 Փ⫼㉏Ϣᇍ䈵ⱘব䞣 9.6.1 ՓՓ⫼㉏Ϣᇍ䈵ⱘব䞣 Ͼ㉏ⱘϡৠᅲ՟Ёˈ㱑✊ᇍ䈵ⱘব䞣᳝Ⳍৠⱘৡ⿄ˈԚᰃᰃѦϡⳌ݇ⱘDŽ䗮䖛ϔϾ՟ᄤӮՓ䖭ϾᯧѢ⧚㾷DŽ 57 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  VHOI GHIWHOO  VHOIQDPH V,QLWLDOL]HG6WXGHQW SULQW VHOIPDUNV PDUNV  VHOIQDPHDJH 6FKRRO0HPEHUBBLQLWBB  VHOIQDPHDJHPDUNV GHIBBLQLWBB  5HSUHVHQWVDVWXGHQW   6FKRRO0HPEHU FODVV6WXGHQW  VHOIVDODU\ G6DODU\ SULQW  VHOI 6FKRRO0HPEHUWHOO  VHOI GHIWHOO  VHOIQDPH V,QLWLDOL]HG7HDFKHU SULQW VHOIVDODU\ VDODU\  VHOIQDPHDJH 6FKRRO0HPEHUBBLQLWBB  VHOIQDPHDJHVDODU\ GHIBBLQLWBB  5HSUHVHQWVDWHDFKHU   6FKRRO0HPEHU FODVV7HDFKHU   VHOIQDPHVHOIDJH  VV$JH1DPH SULQW  7HOOP\GHWDLOV   VHOI GHIWHOO  VHOIQDPH V,QLWLDOL]HG6FKRRO0HPEHU SULQW VHOIDJH DJH VHOIQDPH QDPH  VHOIQDPHDJH GHIBBLQLWBB  5HSUHVHQWVDQ\VFKRROPHPEHU  FODVV6FKRRO0HPEHU  LQKHULWS\)LOHQDPH XVUELQS\WKRQ ՟ 9.5 Փ⫼㒻ᡓ 9.7.1 ՓՓ⫼㒻ᡓ ⦄೼ˈ៥ӀᇚᄺдϔϾ՟ᄤ⿟ᑣDŽ 60 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ೼Ϟ䗄ⱘഎড়ЁˈSchoolMember ㉏㹿⿄Ў ෎ᴀ㉏ ៪ 䍙㉏ DŽ㗠 Teacher ੠ Student ㉏㹿⿄Ў ᇐߎ㉏ ៪ ᄤ㉏ DŽ њDŽخ៥Ӏህϡᕫϡ䖭М ঺໪ˈ៥ӀӮথ⦄೼ 䞡⫼ ⠊㉏ⱘҷⷕⱘᯊ׭ˈ៥Ӏ᮴䳔೼ϡৠⱘ㉏Ё䞡໡ᅗDŽ㗠བᵰ៥ӀՓ⫼⣀ゟⱘ㉏ⱘ䆱ˈ ⱘഎড়ৃҹ㹿᳓ᤶ៤⠊㉏ൟˈेᇍ䈵ৃҹ㹿㾚԰ᰃ⠊㉏ⱘᅲ՟ˈ䖭⾡⦄䈵㹿⿄Ў໮ᗕ⦄䈵DŽ SchoolMember ᇍ䈵ᴹՓ⫼ˈ䖭೼ᶤѯഎড়⡍߿᳝⫼ˈ↨བ㒳䅵ᄺ᷵៤ਬⱘҎ᭄DŽϔϾᄤ㉏ൟ೼ӏԩ䳔㽕⠊㉏ൟ ⱘᬍࡼϡӮᕅડࠄ߿ⱘᄤ㉏ൟDŽ঺໪ϔϾӬ⚍ᰃԴৃҹᡞᬭᏜ੠ᄺ⫳ᇍ䈵䛑԰ЎخϔϾᄤ㉏ൟПЁ ՟བˈԴ㽕ЎᬭᏜ੠ᄺ⫳䛑๲ࡴϔϾᮄⱘ䑿ӑ䆕ඳˈ䙷МԴা䳔ㅔऩഄᡞᅗࡴࠄ SchoolMember ㉏ЁDŽ✊㗠ˈ೼ Փ⫼䖭⾡ᮍ⊩᳝ᕜ໮Ӭ⚍DŽབᵰ៥Ӏ๲ࡴ/ᬍবњ SchoolMember Ёⱘӏԩࡳ㛑ˈᅗӮ㞾ࡼഄড᯴ࠄᄤ㉏ൟПЁDŽ 䛑ᰃ䖭Ͼ㉏ൟ˄㉏˅ⱘᄤ㉏ൟˈ✊ৢ៥ӀݡЎ䖭ѯᄤ㉏ൟ⏏ࡴϧ᳝ⱘሲᗻDŽ ϔϾ↨䕗དⱘᮍ⊩ᰃ߯ᓎϔϾ݅ৠⱘ㉏⿄Ў SchoolMember ✊ৢ䅽ᬭᏜ੠ᄺ⫳ⱘ㉏ 㒻ᡓ 䖭Ͼ݅ৠⱘ㉏DŽेᅗӀ 㽕೼䖭ϸϾ⣀ゟⱘ㉏Ё䛑๲ࡴ䖭ϾሲᗻDŽ䖭ᕜᖿህӮᰒᕫϡᅲ⫼DŽ ⱘ䆱ˈབᵰ㽕๲ࡴϔϾᮄⱘ᳝݅ሲᗻˈህᛣੇⴔخԴৃҹЎᬭᏜ੠ᄺ⫳ᓎゟϸϾ⣀ゟⱘ㉏ᴹ໘⧚ᅗӀˈԚᰃ䖭ḋ ᳳˈᄺ⫳ⱘ៤㒽੠ᄺ䌍DŽ؛г᳝ϧ᳝ⱘሲᗻˈ↨བᬭᏜⱘ㭾∈ǃ䇒⿟੠ DŽҪӀ᳝ϔѯ݅ৠሲᗻˈ↨བྦྷৡǃᑈ啘੠ഄഔDŽҪӀމ䆒Դᛇ㽕ݭϔϾ⿟ᑣᴹ䆄ᔩᄺ᷵ПЁⱘᬭᏜ੠ᄺ⫳ᚙ؛ ⧚㾷៤㉏П䯈ⱘ ㉏ൟ੠ᄤ㉏ൟ ݇㋏DŽ 䴶৥ᇍ䈵ⱘ㓪⿟ᏺᴹⱘЏ㽕ད໘Пϔᰃҷⷕⱘ䞡⫼ˈᅲ⦄䖭⾡䞡⫼ⱘᮍ⊩Пϔᰃ䗮䖛 㒻ᡓ ᴎࠊDŽ㒻ᡓᅠܼৃҹ 9.7 㒻㒻ᡓ ৠḋˈ⊼ᛣBBGHOBBᮍ⊩ϢGHVWUXFWRUⱘὖᗉ㉏ԐDŽ ϡᰃ 3\WKRQ ᠔㽕∖ⱘ˄Ϣঠϟߦ㒓ࠡ㓔ϡৠ˅DŽ 㗠݊Ҫⱘৡ⿄䛑ᇚ԰Ў݀݅ⱘˈৃҹ㹿݊Ҫ㉏ᇍ䈵Փ⫼DŽ䆄ԣ䖭াᰃϔϾᛃ՟ˈᑊ 䖭ḋህ᳝ϔϾᛃ՟ˈབᵰᶤϾব䞣াᛇ೼㉏៪ᇍ䈵ЁՓ⫼ˈህᑨ䆹ҹऩϟߦ㒓ࠡ㓔DŽ 3\WKRQ ⱘৡ⿄ㅵ⧚ԧ㋏Ӯ᳝ᬜഄᡞᅗ԰Ў⾕᳝ব䞣DŽ া᳝ϔϾ՟໪˖བᵰԴՓ⫼ⱘ᭄᥂៤ਬৡ⿄ҹঠϟߦ㒓ࠡ㓔↨བBBSULYDWHYDUˈ 3\WKRQ Ё᠔᳝ⱘ㉏៤ਬ˄ࣙᣀ᭄᥂៤ਬ˅䛑ᰃ݀݅ⱘˈ᠔᳝ⱘᮍ⊩䛑ᰃ᳝ᬜⱘDŽ 㒭 &-DYD&⿟ᑣਬⱘ⊼䞞 䖤㸠ˈԴህᕫՓ⫼ del 䇁হˈህབৠ៥Ӏ೼ҹࠡⱘ՟ᄤЁՓ⫼ⱘ䙷ḋDŽ ᔧᇍ䈵ϡݡ㹿Փ⫼ᯊˈ__del__ᮍ⊩䖤㸠ˈԚᰃᕜ䲒ֱ䆕䖭Ͼᮍ⊩おコ೼ ҔМᯊ׭ 䖤㸠DŽབᵰԴᛇ㽕ᣛᯢᅗⱘ 1DŽ ޣ ⫼ˈᅗ᠔ऴ⫼ⱘݙᄬᇚ䖨ಲ㒭㋏㒳԰ᅗ⫼DŽ೼䖭Ͼᮍ⊩䞠䴶ˈ៥Ӏাᰃㅔऩഄᡞ Person.population ህབৠ__init__ᮍ⊩ϔḋˈ䖬᳝ϔϾ⡍⅞ⱘᮍ⊩__del__ˈᅗ೼ᇍ䈵⍜䗱ⱘᯊ׭㹿䇗⫼DŽᇍ䈵⍜䗱ेᇍ䈵ϡݡ㹿Փ Person.sayHi.__doc__ᴹߚ߿䆓䯂㉏Ϣᮍ⊩ⱘ᭛ḷᄫヺІDŽ ೼䖭Ͼ⿟ᑣЁˈ៥Ӏ䖬ⳟࠄ docstring ᇍѢ㉏੠ᮍ⊩ৠḋ᳝⫼DŽ៥Ӏৃҹ೼䖤㸠ᯊՓ⫼ Person.__doc__੠ 䆄ԣˈԴা㛑Փ⫼ self ব䞣ᴹখ㗗ৠϔϾᇍ䈵ⱘব䞣੠ᮍ⊩DŽ䖭㹿⿄Ў ሲᗻখ㗗 DŽ 59 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  FORVHWKHILOH IFORVH 1RWLFHFRPPDWRDYRLGDXWRPDWLFQHZOLQHDGGHGE\3\WKRQ SULQWOLQH EUHDN =HUROHQJWKLQGLFDWHV(2)  OLQH LIOHQ  OLQH IUHDGOLQH ZKLOH7UXH HDGPRGHLVDVVXPHGE\GHIDXOW U LIQRPRGHLVVSHFLILHG  SRHPW[W I ILOH   FORVHWKHILOH IFORVH SRHP ZULWHWH[WWRILOH IZULWH ULWLQJ Z  RSHQIRU Z  SRHPW[W I ILOH   XVH3\WKRQ LI\RXZDQQDPDNH\RXUZRUNDOVRIXQ :KHQWKHZRUNLVGRQH 3URJUDPPLQJLVIXQ ?  SRHP  XVLQJBILOHS\)LOHQDPH XVUELQS\WKRQ ՟ 10.1 Փ⫼᭛ӊ 10.1.1 Փ⫼᭛ӊ ᮍ⊩ᴹਞ䆝 Python ៥Ӏᅠ៤њᇍ᭛ӊⱘՓ⫼DŽ ᭛ӊDŽᇍ᭛ӊⱘ䇏ݭ㛑࡯ձ䌪ѢԴ೼ᠧᓔ᭛ӊᯊᣛᅮⱘ῵ᓣDŽ᳔ৢˈᔧԴᅠ៤ᇍ᭛ӊⱘ᪡԰ⱘᯊ׭ˈԴ䇗⫼ close Դৃҹ䗮䖛߯ᓎϔϾ file ㉏ⱘᇍ䈵ᴹᠧᓔϔϾ᭛ӊˈߚ߿Փ⫼ file ㉏ⱘ readǃreadline ៪ write ᮍ⊩ᴹᙄᔧഄ䇏ݭ 10.1 ᭛ӊ བԩᅲ⦄䖭ѯࡳ㛑DŽ ঺ϔϾᐌ⫼ⱘ䕧ܹ/䕧ߎ㉏ൟᰃ໘⧚᭛ӊDŽ߯ᓎǃ䇏੠ݭ᭛ӊⱘ㛑࡯ᰃ䆌໮⿟ᑣ᠔ᖙ䳔ⱘˈ៥ӀᇚӮ೼䖭ゴ᥶㋶ ㉏DŽ՟བˈԴ㛑໳Փ⫼ rjust ᮍ⊩ᴹᕫࠄϔϾᣝϔᅮᆑᑺেᇍ唤ⱘᄫヺІDŽ߽⫼ help(str)㦋ᕫ᳈໮䆺ᚙDŽ ៥Ӏৃҹߚ߿Փ⫼ raw_input ੠ print 䇁হᴹᅠ៤䖭ѯࡳ㛑DŽᇍѢ䕧ߎˈԴгৃҹՓ⫼໮⾡໮ḋⱘ str˄ᄫヺІ˅ ೼ᕜ໮ᯊ׭ˈԴӮᛇ㽕䅽Դⱘ⿟ᑣϢ⫼᠋˄ৃ㛑ᰃԴ㞾Ꮕ˅ѸѦDŽԴӮҢ⫼᠋䙷䞠ᕫࠄ䕧ܹˈ✊ৢᠧॄϔѯ㒧ᵰDŽ 10. 䕧䕧ܹ/䕧ߎ 62 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 㒘Ё߫њϔϾҹϞⱘ㉏ˈ䙷Мᅗህ㹿⿄԰ ໮䞡㒻ᡓ DŽܗϔϾᴃ䇁ⱘ⊼䞞üüབᵰ೼㒻ᡓ 㒘ПЁᣛᯢⱘDŽܗྟࠄ෎ᴀ㉏Ё䗤ϾᶹᡒDŽ෎ᴀ㉏ᰃ೼㉏ᅮНⱘᯊ׭ˈ೼ ᶹᡒᇍᑨ㉏ൟⱘᮍ⊩ˈ೼䖭Ͼ՟ᄤЁህᰃབℸDŽབᵰᅗϡ㛑೼ᇐߎ㉏Ёᡒࠄᇍᑨⱘᮍ⊩ˈᅗᠡᓔܜPython ᘏᰃ佪 ঺໪ˈ೼䖭Ͼ՟ᄤЁˈ៥Ӏ䇗⫼њᄤ㉏ൟⱘ tell ᮍ⊩ˈ㗠ϡᰃ SchoolMember ㉏ⱘ tell ᮍ⊩DŽৃҹ䖭ḋᴹ⧚㾷ˈ ⱘᅲ՟DŽ ⊼ᛣˈ೼៥ӀՓ⫼ SchoolMember ㉏ⱘ tell ᮍ⊩ⱘᯊ׭ˈ៥Ӏᡞ Teacher ੠ Student ⱘᅲ՟ҙҙ԰Ў SchoolMember ៥Ӏ䖬㾖ᆳࠄ៥Ӏ೼ᮍ⊩䇗⫼ПࠡࡴϞ㉏ৡ⿄ࠡ㓔ˈ✊ৢᡞ self ব䞣ঞ݊Ҫখ᭄Ӵ䗦㒭ᅗDŽ Ӯ㞾ࡼ䇗⫼෎ᴀ㉏ⱘ constructorˈԴᕫ҆㞾ϧ䮼䇗⫼ᅗDŽ __init__ᮍ⊩ϧ䮼Փ⫼ self ব䞣䇗⫼ˈ䖭ḋ៥Ӏህৃҹ߱ྟ࣪ᇍ䈵ⱘ෎ᴀ㉏䚼ߚDŽ䖭ϔ⚍कߚ䞡㽕üüPython ϡ 㒘䎳೼ᅮН㉏ᯊⱘ㉏ৡ⿄ПৢDŽ✊ৢˈ៥Ӏ⊼ᛣࠄ෎ᴀ㉏ⱘܗЎњՓ⫼㒻ᡓˈ៥Ӏᡞ෎ᴀ㉏ⱘৡ⿄԰ЎϔϾ 9.7.1.1 ᅗᅗབԩᎹ԰ 0DUNV6ZDURRS$JH1DPH 6DODU\0UV6KULYLG\D$JH1DPH   6ZDURRS,QLWLDOL]HG6WXGHQW  6ZDURRS,QLWLDOL]HG6FKRRO0HPEHU  0UV6KULYLG\D,QLWLDOL]HG7HDFKHU  0UV6KULYLG\D,QLWLDOL]HG6FKRRO0HPEHU S\WKRQLQKHULWS\ 䕧ߎ  ZRUNVIRUERWK7HDFKHUVDQG6WXGHQWV PHPEHUWHOO IRUPHPEHULQPHPEHUV PHPEHUV >WV@  SULQWSULQWVDEODQNOLQH    6ZDURRS V 6WXGHQW   0UV6KULYLG\D W 7HDFKHU  VHOIPDUNV G0DUNV SULQW  VHOI 6FKRRO0HPEHUWHOO 61 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 DŽމབԴⱘ⿟ᑣЁ᳝ϔѯ᮴ᬜⱘ䇁হˈӮᗢМḋਸ਼˛Python Ӯᓩথᑊਞ䆝Դ䙷䞠᳝ϔϾ䫭䇃ˈҢ㗠໘⧚䖭ḋⱘᚙ؛ ҹՓ⫼ᓖᐌᴹ໘⧚DŽৃމᄬ೼DŽ៪㗙೼⿟ᑣ䖤㸠ⱘᯊ׭ˈԴϡᇣᖗᡞᅗߴ䰸њDŽϞ䗄䖭ѯᚙ ⱘᯊ׭ˈᓖᐌህথ⫳њDŽ՟བˈᔧԴᛇ㽕䇏ᶤϾ᭛ӊⱘᯊ׭ˈ㗠䙷Ͼ᭛ӊϡމ⢊ ᔧԴⱘ⿟ᑣЁߎ⦄ᶤѯ ᓖᐌⱘ 11. ᓖᐌ ᄬ DŽټ᥹ϟᴹˈ៥ӀՓ⫼ pickle ῵ഫⱘ load ߑ᭄ⱘ䖨ಲᴹপಲᇍ䈵DŽ䖭Ͼ䖛⿟⿄Ў প ᄬ DŽټ ࠄᠧᓔⱘ᭛ӊЁDŽ䖭Ͼ䖛⿟⿄Ў ᄬټᄬ఼῵ഫⱘ dump ߑ᭄ˈᡞᇍ䈵ټ⫼ҹݭ῵ᓣᠧᓔϔϾ file ᇍ䈵ˈ✊ৢ䇗ܜᄬϔϾᇍ䈵ˈ佪ټЎњ೼᭛ӊ䞠 ׭ˈ៥Ӏㅔऩഄᡞ䖭Ͼ῵ഫ⿄Ў pDŽ ᄤЁˈᅗ䖬䅽៥Ӏ㛑໳䗮䖛ㅔऩഄᬍবϔ㸠ህߛᤶࠄ঺ϔϾ῵ഫ˄cPickle ៪㗙 pickle˅ʽ೼⿟ᑣⱘ݊ԭ䚼ߚⱘᯊ 䇋⊼ᛣ៥ӀՓ⫼њ import..as 䇁⊩DŽ䖭ᰃϔ⾡߽֓ᮍ⊩ˈҹ֓Ѣ៥ӀৃҹՓ⫼᳈ⷁⱘ῵ഫৡ⿄DŽ೼䖭Ͼ՟ˈܜ佪 10.2.1.1 ᅗᅗབԩᎹ԰ @ FDUURW  PDQJR  DSSOH > S\WKRQSLFNOLQJS\ 䕧ߎ SULQWVWRUHGOLVW  I VWRUHGOLVW SORDG  VKRSOLVWILOH I ILOH 5HDGEDFNIURPWKHVWRUDJH  GHOVKRSOLVWUHPRYHWKHVKRSOLVW   IFORVH VKRSOLVWI GXPSWKHREMHFWWRDILOH SGXPS  Z VKRSOLVWILOH I ILOH :ULWHWRWKHILOH  @ FDUURW  PDQJR  DSSOH VKRSOLVW >  WKHQDPHRIWKHILOHZKHUHZHZLOOVWRUHWKHREMHFW  VKRSOLVWGDWD  VKRSOLVWILOH 64 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  LPSRUWSLFNOHDVS LPSRUWF3LFNOHDVS  SLFNOLQJS\)LOHQDPH XVUELQS\WKRQ ᄬټᄬϢপټ ՟ 10.2 ᄬټᄬϢপټ 10.2.1 䛑ㅔ⿄Ў pickle ῵ഫDŽ DŽԴৃҹՓ⫼ᅗӀЁⱘӏϔϾˈ㗠៥Ӏ೼䖭䞠ᇚՓ⫼ cPickle ῵ഫDŽ䆄ԣˈ៥Ӏᡞ䖭ϸϾ῵ഫ˅ס pickle ᖿ 1000 䖬᳝঺ϔϾ῵ഫ⿄Ў cPickleˈᅗ ⱘ ࡳ 㛑 ੠ pickle ῵ഫᅠܼⳌৠˈাϡ䖛ᅗᰃ⫼ C 䇁㿔㓪ݭⱘˈ಴ℸ㽕ᖿᕫ໮˄↨ ᄬᇍ䈵DŽټ ᅗᅠᭈ᮴㔎ഄপߎᴹDŽ䖭㹿⿄Ў ᣕЙഄ ᄬӏԩ Python ᇍ䈵ˈПৢԴজৃҹᡞټⱘ῵ഫˈ⿄Ў pickleDŽՓ⫼ᅗԴৃҹ೼ϔϾ᭛ӊЁޚPython ᦤկϔϾᷛ ᄬ఼ټ 10.2 ⦄೼ˈᴹⳟϔϟ poem.txt ᭛ӊⱘݙᆍᴹ偠䆕⿟ᑣ⹂ᅲᎹ԰ℷᐌњDŽ Ӏ⫼ close ݇䯁䖭Ͼ᭛ӊDŽ ⊼ᛣˈ಴ЎҢ᭛ӊ䇏ࠄⱘݙᆍᏆ㒣ҹᤶ㸠ヺ㒧ሒˈ᠔ҹ៥Ӏ೼ print 䇁হϞՓ⫼䗫োᴹ⍜䰸㞾ࡼᤶ㸠DŽ᳔ৢˈ៥ ℶᕾ⦃DŽذヺІ㹿䖨ಲⱘᯊ׭ˈे㸼⼎᭛ӊ᳿Ꮖ㒣ࠄ䖒њˈѢᰃ៥Ӏ Ёˈ៥ӀՓ⫼ readline ᮍ⊩䇏᭛ӊⱘ↣ϔ㸠DŽ䖭Ͼᮍ⊩䖨ಲࣙᣀ㸠᳿ᤶ㸠ヺⱘϔϾᅠᭈ㸠DŽ᠔ҹˈᔧϔϾ ぎⱘ ᄫ ᥹ϟᴹˈ៥Ӏݡϔ⃵ᠧᓔৠϔϾ᭛ӊᴹ䇏᭛ӊDŽབᵰ៥Ӏ≵᳝ᣛᅮ῵ᓣˈ䇏῵ᓣӮ԰Ў咬䅸ⱘ῵ᓣDŽ೼ϔϾᕾ⦃ ݭ῵ᓣᠧᓔ᭛ӊˈ✊ৢՓ⫼ file ㉏ⱘ write ᮍ⊩ᴹݭ᭛ӊˈ᳔ৢ៥Ӏ⫼ close ݇䯁䖭Ͼ᭛ӊDŽ⫼ܜ៥Ӏ佪 ៪䗑ࡴ῵ᓣ˄'a'˅DŽџᅲϞ䖬᳝໮ᕫ໮ⱘ῵ᓣৃҹՓ⫼ˈԴৃҹՓ⫼ help(file)ᴹњ㾷ᅗӀⱘ䆺ᚙDŽ ៥Ӏ䗮䖛ᣛᯢ៥ӀᏠᳯᠧᓔⱘ᭛ӊ੠῵ᓣᴹ߯ᓎϔϾ file ㉏ⱘᅲ՟DŽ῵ᓣৃҹЎ䇏῵ᓣ˄'r'˅ǃݭ῵ᓣ ˄ 'w'˅ˈܜ佪 10.1.1.1 ᅗᅗབԩᎹ԰ XVH3\WKRQ LI\RXZDQQDPDNH\RXUZRUNDOVRIXQ :KHQWKHZRUNLVGRQH 3URJUDPPLQJLVIXQ S\WKRQXVLQJBILOHS\ 䕧ߎ 63 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  ([FHSWLRQ FODVV6KRUW,QSXW([FHSWLRQ  UDLVLQJS\)LOHQDPH XVUELQS\WKRQ ՟ 11.2 བԩᓩথᓖᐌ 11.3.1 བԩᓩথᓖᐌ 䇃៪ᓖᐌᑨ䆹ߚ߿ᰃϔϾ Error ៪ Exception ㉏ⱘⳈ᥹៪䯈᥹ᇐߎ㉏DŽ ԴৃҹՓ⫼ raise 䇁হ ᓩথ ᓖᐌDŽԴ䖬ᕫᣛᯢ䫭䇃/ᓖᐌⱘৡ⿄੠Ԉ䱣ᓖᐌ 㾺থⱘ ᓖᐌᇍ䈵DŽԴৃҹᓩথⱘ䫭 11.3 ᓩথᓖᐌ ៥Ӏ䖬ৃҹᕫࠄᓖᐌᇍ䈵ˈҢ㗠㦋প᳈໮᳝Ͼ䖭ϾᓖᐌⱘֵᙃDŽ䖭Ӯ೼ϟϔϾ՟ᄤЁ䇈ᯢDŽ Դ䖬ৃҹ䅽 try..catch ഫ݇㘨ϞϔϾ else ҢহDŽᔧ≵᳝ᓖᐌথ⫳ⱘᯊ׭ˈelse Ңহᇚ㹿ᠻ㸠DŽ ៥ӀᏆ㒣ⳟࠄњ䖭ḋⱘ໘⧚DŽ བᵰᶤϾ䫭䇃៪ᓖᐌ≵᳝㹿໘⧚ˈ咬䅸ⱘ Python ໘⧚఼ህӮ㹿䇗⫼DŽᅗӮ㒜ℶ⿟ᑣⱘ䖤㸠ˈᑊϨᠧॄϔϾ⍜ᙃˈ ⧚ ᠔᳝ⱘ 䫭䇃੠ᓖᐌDŽᇍѢ↣Ͼ try Ңহˈ㟇ᇥ䛑᳝ϔϾⳌ݇㘨ⱘ except ҢহDŽ ϧ䮼໘⧚ऩϔⱘ䫭䇃៪ᓖᐌˈ៪㗙ϔ㒘ࣙᣀ೼೚ᣀোݙⱘ䫭䇃/ᓖᐌDŽབᵰ≵᳝㒭ߎ䫭䇃៪ᓖᐌⱘৡ⿄ˈᅗӮ໘ ៥Ӏᡞ᠔᳝ৃ㛑ᓩথ䫭䇃ⱘ䇁হᬒ೼ try ഫЁˈ✊ৢ೼ except Ңহ/ഫЁ໘⧚᠔᳝ⱘ䫭䇃੠ᓖᐌDŽexcept Ңহৃҹ 11.2.1.1 ᅗᅗབԩᎹ԰ 'RQH (QWHUVRPHWKLQJ!3\WKRQLVH[FHSWLRQDO S\WKRQWU\BH[FHSWS\  :K\GLG\RXGRDQ(2)RQPH“ (QWHUVRPHWKLQJ! S\WKRQWU\BH[FHSWS\ 䕧ߎ  'RQH SULQW  KHUHZHDUHQRWH[LWLQJWKHSURJUDP  ?Q6RPHHUURUH[FHSWLRQRFFXUUHG SULQW H[FHSW  H[LWWKHSURJUDP V\VH[LW  ?Q:K\GLG\RXGRDQ(2)RQPH“ SULQW 66 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 H[FHSW(2)(UURU  (QWHUVRPHWKLQJ! V UDZBLQSXW WU\  LPSRUWV\V  WU\BH[FHSWS\)LOHQDPH XVUELQS\WKRQ ՟ 11.1 ໘⧚ᓖᐌ ഫЁDŽ ៥ӀৃҹՓ⫼ try..except 䇁হᴹ໘⧚ᓖᐌDŽ៥Ӏᡞ䗮ᐌⱘ䇁হᬒ೼ try-ഫЁˈ㗠ᡞ៥Ӏⱘ䫭䇃໘⧚䇁হᬒ೼ except- 11.2.1 ໘⧚ᓖᐌ ᥹ϟᴹˈ៥Ӏᇚᄺдབԩ໘⧚䖭ḋⱘ䫭䇃DŽ Python ᓩথњϔϾ⿄Ў EOFError ⱘ䫭䇃ˈ䖭Ͼ䫭䇃෎ᴀϞᛣੇⴔᅗথ⦄ϔϾϡᳳᳯⱘ ᭛ӊሒ ˄⬅ Ctrl-d 㸼⼎˅ (2)(UURU VWGLQ!OLQHLQ“)LOH  PRVWUHFHQWFDOOODVW (QWHUVRPHWKLQJ!7UDFHEDFN  (QWHUVRPHWKLQJ! !!!V UDZBLQSXW ៥Ӏᇱ䆩䇏প⫼᠋ⱘϔ↉䕧ܹDŽᣝ Ctrl-dˈⳟϔϟӮথ⫳ҔМDŽ 11.2 try..except ⱘᎹ԰DŽخ఼ ᠔ ៥Ӏৃҹ㾖ᆳࠄ᳝ϔϾ SyntaxError 㹿ᓩথˈᑊϨẔ⌟ࠄⱘ䫭䇃ԡ㕂г㹿ᠧॄњߎᴹDŽ䖭ᰃ䖭Ͼ䫭䇃ⱘ 䫭䇃໘⧚ +HOOR:RUOG  +HOOR:RUOG !!!SULQW  LQYDOLGV\QWD[6\QWD[(UURU A  +HOOR:RUOG 3ULQW VWGLQ!OLQH)LOH  +HOOR:RUOG !!!3ULQW བ៥Ӏᡞ print 䇃ᣐЎ Printˈ⊼ᛣ໻ݭˈ䖭ḋ Python Ӯ ᓩথ ϔϾ䇁⊩䫭䇃DŽ؛㗗㰥ϔϾㅔऩⱘ print 䇁হDŽ 11.1 䫭䫭䇃 65 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ᑣ䖤㸠ᕫ᜶ϔѯ˄Python ⬅Ѣ݊ᴀ䋼䗮ᐌ䖤㸠ᕫᕜᖿ˅DŽ೼⿟ᑣ䖤㸠ⱘᯊ׭ˈᣝ Ctrl-c Ёᮁ/প⍜⿟ᑣDŽ ⱘॳ಴ᰃ䅽⿟خ䩳DŽ䖭ḋ⾦ 2 ذ៥Ӏ䖯㸠䗮ᐌⱘ䇏᭛ӊᎹ԰ˈԚᰃ៥᳝ᛣ೼↣ᠧॄϔ㸠Пࠡ⫼ time.sleep ᮍ⊩᱖ 11.4.1.1 ᅗབԩᎹ԰ .H\ERDUG,QWHUUXSW   WLPHVOHHS )LOHILQDOO\S\OLQHLQ“  PRVWUHFHQWFDOOODVW 7UDFHEDFN &OHDQLQJXSFORVHGWKHILOH :KHQWKHZRUNLVGRQH 3URJUDPPLQJLVIXQ S\WKRQILQDOO\S\ 䕧ߎ  &OHDQLQJXSFORVHGWKHILOH SULQW  IFORVH ILQDOO\ SULQWOLQH   WLPHVOHHS EUHDN   OLQH LIOHQ  OLQH IUHDGOLQH RXUXVXDOILOHUHDGLQJLGLRPZKLOH7UXH  SRHPW[W I ILOH WU\  LPSRUWWLPH  ILQDOO\S\)LOHQDPH XVUELQS\WKRQ ՟ 11.3 Փ⫼ finally 11.4.1 ՓՓ⫼ finally ᡞϔϾጠܹ঺໪ϔϾDŽ ഫᴹᅠ៤DŽ⊼ᛣˈ೼ϔϾ try ഫϟˈԴৃҹৠᯊՓ⫼ except Ңহ੠ finally ഫDŽབᵰԴ㽕ৠᯊՓ⫼ᅗӀⱘ䆱ˈ䳔㽕 ਸ਼˛䖭ৃҹՓ⫼ finallyخϟ䛑݇䯁᭛ӊˈ䆹ᗢМމབԴ೼䇏ϔϾ᭛ӊⱘᯊ׭ˈᏠᳯ೼᮴䆎ᓖᐌথ⫳Ϣ৺ⱘᚙ؛ 11.4 try..finally 68 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ೼䖭Ͼ⡍߿ⱘ except ҢহЁˈ៥ӀՓ⫼ᓖᐌᇍ䈵ⱘ length ੠ atleast ඳᴹЎ⫼᠋ᠧॄϔϾᙄᔧⱘ⍜ᙃDŽ ೼ except ҢহЁˈ៥Ӏᦤկњ䫭䇃㉏੠⫼ᴹ㸼⼎䫭䇃/ᓖᐌᇍ䈵ⱘব䞣DŽ䖭Ϣߑ᭄䇗⫼Ёⱘᔶখ੠ᅲখὖᗉ㉏ԐDŽ ShortInputException ㉏DŽᅗ᳝ϸϾඳüülength ᰃ㒭ᅮ䕧ܹⱘ䭓ᑺˈatleast ߭ᰃ⿟ᑣᳳᳯⱘ᳔ᇣ䭓ᑺDŽ 䖭䞠ˈ៥Ӏ߯ᓎњ៥Ӏ㞾Ꮕⱘᓖᐌ㉏ൟˈ݊ᅲ៥ӀৃҹՓ⫼ӏԩ乘ᅮНⱘᓖᐌ/䫭䇃DŽ䖭Ͼᮄⱘᓖᐌ㉏ൟᰃ 11.3.1.1 ᅗᅗབԩᎹ԰ 1RH[FHSWLRQZDVUDLVHG (QWHUVRPHWKLQJ!DEF S\WKRQUDLVLQJS\  7KHLQSXWZDVRIOHQJWKZDVH[SHFWLQJDWOHDVW6KRUW,QSXW([FHSWLRQ (QWHUVRPHWKLQJ!DE S\WKRQUDLVLQJS\  :K\GLG\RXGRDQ(2)RQPH“ (QWHUVRPHWKLQJ! S\WKRQUDLVLQJS\ 䕧ߎ  1RH[FHSWLRQZDVUDLVHG SULQW HOVH  [OHQJWK[DWOHDVW  ZDVH[SHFWLQJDWOHDVWG 7KHLQSXWZDVRIOHQJWKG?6KRUW,QSXW([FHSWLRQ SULQW H[FHSW6KRUW,QSXW([FHSWLRQ[  ?Q:K\GLG\RXGRDQ(2)RQPH“ SULQW H[FHSW(2)(UURU 2WKHUZRUNFDQFRQWLQXHDVXVXDOKHUH   V OHQ UDLVH6KRUW,QSXW([FHSWLRQ  V LIOHQ  (QWHUVRPHWKLQJ! V UDZBLQSXW WU\  VHOIDWOHDVW DWOHDVW VHOIOHQJWK OHQJWK  VHOI ([FHSWLRQBBLQLWBB  VHOIOHQJWKDWOHDVW GHIBBLQLWBB  $XVHUGHILQHGH[FHSWLRQFODVV  67 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 :KHQWKHZRUNLVGRQH 3URJUDPPLQJLVIXQ S\WKRQFDWS\SRHPW[W  8QNQRZQRSWLRQ S\WKRQFDWS\QRQVHQVH  9HUVLRQ S\WKRQFDWS\YHUVLRQ  'LVSOD\WKLVKHOSKHOS 3ULQWVWKHYHUVLRQQXPEHUYHUVLRQ 2SWLRQVLQFOXGH $Q\QXPEHURIILOHVFDQEHVSHFLILHG 7KLVSURJUDPSULQWVILOHVWRWKHVWDQGDUGRXWSXW S\WKRQFDWS\KHOS  1RDFWLRQVSHFLILHG S\WKRQFDWS\ 䕧ߎ  ILOHQDPH UHDGILOH @IRUILOHQDPHLQV\VDUJY> HOVH  V\VH[LW  8QNQRZQRSWLRQ SULQW HOVH  'LVSOD\WKLVKHOSKHOS 3ULQWVWKHYHUVLRQQXPEHUYHUVLRQ 2SWLRQVLQFOXGH $Q\QXPEHURIILOHVFDQEHVSHFLILHG 7KLVSURJUDPSULQWVILOHVWRWKHVWDQGDUGRXWSXW ? SULQW  KHOS  HOLIRSWLRQ  9HUVLRQ SULQW  YHUVLRQ  LIRSWLRQ IHWFKV\VDUJY>@EXWZLWKRXWWKHILUVWWZRFKDUDFWHUV @RSWLRQ V\VDUJY>@> 70 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥   LIV\VDUJY>@VWDUWVZLWK   V\VH[LW  1RDFWLRQVSHFLILHG SULQW  V\VDUJY LIOHQ 6FULSWVWDUWVIURPKHUH   IFORVH SULQWOLQHQRWLFHFRPPD EUHDN   OLQH LIOHQ  OLQH IUHDGOLQH ZKLOH7UXH  ILOHQDPH I ILOH  3ULQWDILOHWRWKHVWDQGDUGRXWSXW   ILOHQDPH GHIUHDGILOH  LPSRUWV\V  FDWS\)LOHQDPH XVUELQS\WKRQ ՟ 12.1 Փ⫼ sys.argv 12.2.1 ੑҸ㸠খ᭄ sys ῵ഫࣙ৿㋏㒳ᇍᑨⱘࡳ㛑DŽ៥ӀᏆ㒣ᄺдњ sys.argv ߫㸼ˈᅗࣙ৿ੑҸ㸠খ᭄DŽ 12.2 sys ῵ഫ ᑧЁ᠔᳝῵ഫⱘᅠᭈݙᆍDŽޚ ៥ӀᏆ㒣ⷨおњϔѯ䖭ϾᑧЁⱘᐌ⫼῵ഫDŽԴৃҹ೼ Python 䰘ᏺᅝ㺙ⱘ᭛ḷⱘĀᑧখ㗗āϔ㡖Ёњ㾷 Python ᷛ DŽއᵰԴ❳ᙝ䖭ѯᑧЁⱘ῵ഫˈ䙷МԴⱘ໻໮᭄䯂乬䛑ৃҹㅔऩᖿ᥋ഄՓ⫼ᅗӀᴹ㾷 ᑧᰃकߚ䞡㽕ⱘˈ಴Ўབޚᑧᰃ䱣 Python 䰘ᏺᅝ㺙ⱘˈᅗࣙ৿໻䞣ᵕ᳝݊⫼ⱘ῵ഫDŽ❳ᙝ Python ᷛޚPython ᷛ 12.1 ㅔҟ ᑧޚ12. Python ᷛᷛ ӊ݇䯁 ៥Ӏৃҹ㾖ᆳࠄ KeyboardInterrupt ᓖᐌ㹿㾺থˈ⿟ᑣ䗔ߎDŽԚᰃ೼⿟ᑣ䗔ߎПࠡˈfinally Ңহҡ✊㹿ᠻ㸠ˈᡞ᭛ 69 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 ⹂DŽབᵰԴⱘ䇁হഫাࣙ৿ϔহ䇁হˈ䙷МԴৃҹ೼ᴵӊ䇁হ៪ᕾ⦃䇁হⱘৠϔ㸠ޚℷ⹂ⱘˈԚᰃᑊ䴲 100ˁⱘ ϟᰃމ⦄೼ˈԴᏆ㒣ᕜ⏅ࠏഄ⧚㾷њ↣ϔϾ䇁হഫᰃ䗮䖛ᅗⱘ㓽䖯ሖ⃵Ϣ݊ᅗഫऎߚᓔᴹⱘDŽ✊㗠䖭೼໻໮᭄ᚙ 13.2 ऩ䇁হഫ __len__(self) ᇍᑣ߫ᇍ䈵Փ⫼ݙᓎⱘ len()ߑ᭄ⱘᯊ׭䇗⫼DŽ __getitem__(self,key) Փ⫼ x[key]㋶ᓩ᪡԰ヺⱘᯊ׭䇗⫼DŽ 䛑᳝⡍⅞ⱘᮍ⊩DŽ __lt__(self,other) ᔧՓ⫼ ᇣѢ 䖤ㅫヺ˄<˅ⱘᯊ׭䇗⫼DŽ㉏ԐഄˈᇍѢ᠔᳝ⱘ䖤ㅫヺ˄+ˈ>ㄝㄝ˅ __str__(self) ೼៥Ӏᇍᇍ䈵Փ⫼ print 䇁হ៪ᰃՓ⫼ str()ⱘᯊ׭䇗⫼DŽ __del__(self) ᙄད೼ᇍ䈵㽕㹿ߴ䰸Пࠡ䇗⫼DŽ __init__(self,...) 䖭Ͼᮍ⊩೼ᮄᓎᇍ䈵ᙄད㽕㹿䖨ಲՓ⫼Пࠡ㹿䇗⫼DŽ ৡ⿄ 䇈ᯢ 㸼 13.1 ϔѯ⡍⅞ⱘᮍ⊩ ࠄϔϾᑲ໻ⱘ߫㸼DŽ ϟ䴶䖭Ͼ㸼Ё߫ߎњϔѯ᳝⫼ⱘ⡍⅞ᮍ⊩DŽབᵰԴᛇ㽕ⶹ䘧᠔᳝ⱘ⡍⅞ᮍ⊩ˈԴৃҹ೼ljPython খ㗗᠟ݠNJЁᡒ ⱘʽخ㒘ϔḋ˅ˈ䙷МԴা䳔㽕ᅲ⦄__getitem__()ᮍ⊩ህৃҹњDŽᛇϔϟˈPython ህᰃᇍ list ㉏䖭ḋܗ㸼੠ ߫ڣϔ㠀䇈ᴹˈ⡍⅞ⱘᮍ⊩䛑㹿⫼ᴹ῵ӓᶤϾ㸠ЎDŽ՟བˈབᵰԴᛇ㽕ЎԴⱘ㉏Փ⫼ x[key]䖭ḋⱘ㋶ᓩ᪡԰˄ህ ೼㉏Ё᳝ϔѯ⡍⅞ⱘᮍ⊩݋᳝⡍⅞ⱘᛣНˈ↨བ__init__੠__del__ᮍ⊩ˈᅗӀⱘ䞡㽕ᗻ៥ӀᏆ㒣ᄺд䖛њDŽ 13.1 ⡍⅞ⱘᮍ⊩ ⶹ䆚ˈҢ㗠Փ៥Ӏᇍ Python ⱘњ㾷᳈ࡴ ᅠᭈ DŽ ࠄⳂࠡЎℶˈ៥ӀᏆ㒣ᄺдњ㒱໻໮᭄ᐌ⫼ⱘ Python ⶹ䆚DŽ೼䖭ϔゴЁˈ៥Ӏᇚ㽕ᄺд঺໪ϔѯᮍ䴶ⱘ Python 13. ᳈᳈໮ Python ⱘݙᆍ ᭛ḷএ᥶㋶᳈໮᳝݇䖭ѯߑ᭄੠ব䞣ⱘ䆺㒚ⶹ䆚DŽԴгৃҹՓ⫼ help(sys)ㄝㄝDŽޚԴৃҹ߽⫼ Python ᷛ ᴹẔ偠㒭ߎⱘ䏃ᕘᰃ৺ⳳഄᄬ೼DŽ  os.path.isfile()੠ os.path.isdir()ߑ᭄ߚ߿Ẕ偠㒭ߎⱘ䏃ᕘᰃϔϾ᭛ӊ䖬ᰃⳂᔩDŽ㉏Ԑഄˈos.path.existe()ߑ᭄⫼  SRHPW[W  KRPHVZDURRSE\WHFRGH  KRPHVZDURRSE\WHFRGHSRHPW[W !!!RVSDWKVSOLW  os.path.split()ߑ᭄䖨ಲϔϾ䏃ᕘⱘⳂᔩৡ੠᭛ӊৡDŽ  os.linesep ᄫヺІ㒭ߎᔧࠡᑇৄՓ⫼ⱘ㸠㒜ℶヺDŽ՟བˈWindows Փ⫼'\r\n'ˈLinux Փ⫼'\n'㗠 Mac Փ⫼'\r'DŽ  os.system()ߑ᭄⫼ᴹ䖤㸠 shell ੑҸDŽ  os.remove()ߑ᭄⫼ᴹߴ䰸ϔϾ᭛ӊDŽ  os.listdir()䖨ಲᣛᅮⳂᔩϟⱘ᠔᳝᭛ӊ੠ⳂᔩৡDŽ  os.getenv()੠ os.putenv()ߑ᭄ߚ߿⫼ᴹ䇏প੠䆒㕂⦃๗ব䞣DŽ  os.getcwd()ߑ᭄ᕫࠄᔧࠡᎹ԰Ⳃᔩˈेᔧࠡ Python 㛮ᴀᎹ԰ⱘⳂᔩ䏃ᕘDŽ 72 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  os.name ᄫヺІᣛ⼎Դℷ೼Փ⫼ⱘᑇৄDŽ↨བᇍѢ Windowsˈᅗᰃ 'nt'ˈ㗠ᇍѢ Linux/Unix ⫼᠋ˈᅗᰃ'posix'DŽ ϟ䴶߫ߎњϔѯ೼ os ῵ഫЁ↨䕗᳝⫼ⱘ䚼ߚDŽᅗӀЁⱘ໻໮᭄䛑ㅔऩᯢњDŽ ᰃՓ⫼ os.sep ৃҹপҷ᪡԰㋏㒳⡍ᅮⱘ䏃ᕘߚࡆヺDŽ 䆌ϔϾ⿟ᑣ೼㓪ݭৢϡ䳔㽕ӏԩᬍࡼˈгϡӮথ⫳ӏԩ䯂乬ˈህৃҹ೼ Linux ੠ Windows ϟ䖤㸠DŽϔϾ՟ᄤህ ܕ䖭Ͼ῵ഫࣙ৿᱂䘡ⱘ᪡԰㋏㒳ࡳ㛑DŽབᵰԴᏠᳯԴⱘ⿟ᑣ㛑໳Ϣᑇৄ᮴݇ⱘ䆱ˈ䖭Ͼ῵ഫᰃᇸЎ䞡㽕ⱘDŽेᅗ 12.3 os ῵ഫ 䫭䇃⌕DŽޚ䕧ߎ੠ᷛޚ䕧ܹǃᷛޚᑣⱘᷛ ᇍѢ᳝㒣偠ⱘ⿟ᑣਬˈsys ῵ഫЁ݊ҪҸҎᛳ݈䍷ⱘ乍Ⳃ᳝ sys.stdinǃsys.stdout ੠ sys.stderr ᅗӀߚ߿ᇍᑨԴⱘ⿟   ILQDO  !!!V\VYHUVLRQBLQIR  IF @ 5HG +DW     ?Q>*&&    2FW  !!!V\VYHUVLRQ !!!LPSRUWV\V >VZDURRS#ORFDOKRVWFRGH@S\WKRQ ᑣ݋໛ Python ⠜ᴀ㽕∖ࡳ㛑DŽ 㒘߭ᦤկϔϾ᳈ㅔऩⱘᮍ⊩ᴹՓԴⱘ⿟ܗ sys.version ᄫヺІ㒭Դᦤկᅝ㺙ⱘ Python ⱘ⠜ᴀֵᙃDŽsys.version_info 12.2.2 ᳈໮ sys ⱘݙᆍ ᡞϸϾ៪ϸϾҹϞ᭛ӊ䖲᥹/㑻䖲೼ϔ䍋ᠧॄDŽ 乎֓䇈ϔϟˈৡ⿄ cat ᰃ concatenate ⱘ㓽ݭˈᅗ෎ᴀϞ㸼ᯢњ⿟ᑣⱘࡳ㛑üüᅗৃҹ೼䕧ߎᠧॄϔϾ᭛ӊ៪㗙 ᑣϔϾ᭛ӊ᥹ⴔϔϾ᭛ӊഄᠧॄDŽ བᵰ≵᳝ᣛᅮӏԩ䗝乍ˈ㗠ᰃЎ⿟ᑣᦤկ᭛ӊৡⱘ䆱ˈᅗህㅔऩഄᠧॄߎ↣Ͼ᭛ӊഄ↣ϔ㸠ˈᣝ✻ੑҸ㸠Ёⱘ乎 Դৃҹⳟϔϟ help(sys.exit)ᴹњ㾷᳈໮䆺ᚙDŽ ᵰᣛᅮњ--help 䗝乍ˈ៥Ӏᦤկϔѯ݇Ѣ⿟ᑣⱘ㾷䞞DŽ៥ӀՓ⫼ sys.exit ߑ᭄䗔ߎℷ೼䖤㸠ⱘ⿟ᑣDŽ੠ҹᕔϔḋˈ খ᭄ᴹẔ偠៥Ӏⱘ⿟ᑣᰃ৺㹿ᣛᅮњ䗝乍DŽབᵰՓ⫼њ--version 䗝乍ˈ⿟ᑣⱘ⠜ᴀোᇚ㹿ᠧॄߎᴹDŽ㉏Ԑഄˈབ ЎњՓ䖭Ͼ⿟ᑣᇍ⫼᠋᳈ࡴটདˈ៥Ӏᦤկњϔѯ⫼᠋ৃҹᣛᅮⱘ䗝乍ᴹњ㾷᳈໮⿟ᑣⱘݙᆍDŽ៥ӀՓ⫼㄀ϔϾ ⿟ᑣৡ⿄ˈ԰Ў sys.argv[0]˄⬅Ѣ Python Ң 0 ᓔྟ䅵᭄˅DŽ݊ҪⱘੑҸ㸠খ᭄೼䖭Ͼ乍ⳂПৢDŽ ೼ Python ⿟ᑣ䖤㸠ⱘᯊ׭ˈेϡᰃ೼ѸѦ῵ᓣϟˈ೼ sys.argv ߫㸼Ёᘏᰃ㟇ᇥ᳝ϔϾ乍ⳂDŽᅗህᰃᔧࠡ䖤㸠ⱘ 䕧ߎDŽ 䖭Ͼ⿟ᑣ⫼ᴹ῵㣗 Linux/Unix ⫼᠋❳ᙝⱘ cat ੑҸDŽԴা䳔㽕ᣛᯢᶤѯ᭛ᴀ᭛ӊⱘৡᄫˈ䖭Ͼ⿟ᑣӮᡞᅗӀᠧॄ 12.2.1.1 ᅗᅗབԩᎹ԰ XVH3\WKRQ LI\RXZDQQDPDNH\RXUZRUNDOVRIXQ 71 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 े֓ᰃ print 䇁হгϡ㛑⫼೼ lambda ᔶᓣЁˈা㛑Փ⫼㸼䖒ᓣDŽ ᴀ䋼Ϟˈlambda 䳔㽕ϔϾখ᭄ˈৢ䴶ҙ䎳ऩϾ㸼䖒ᓣ԰Ўߑ᭄ԧˈ㗠㸼䖒ᓣⱘؐ㹿䖭Ͼᮄᓎⱘߑ᭄䖨ಲDŽ⊼ᛣˈ 䖭䞠ˈ៥ӀՓ⫼њ make_repeater ߑ᭄೼䖤㸠ᯊ߯ᓎᮄⱘߑ᭄ᇍ䈵ˈᑊϨ䖨ಲᅗDŽlambda 䇁হ⫼ᴹ߯ᓎߑ᭄ᇍ䈵DŽ 13.5.1 ᅗབԩᎹ԰  ZRUGZRUG S\WKRQODPEGDS\ 䕧ߎ   SULQWWZLFH  ZRUG SULQWWZLFH    WZLFH PDNHBUHSHDWHU  Q VUHWXUQODPEGDV  Q GHIPDNHBUHSHDWHU  ODPEGDS\)LOHQDPH XVUELQS\WKRQ ՟ 13.2 Փ⫼ lambda ᔶᓣ lambda 䇁হ㹿⫼ᴹ߯ᓎᮄⱘߑ᭄ᇍ䈵ˈᑊϨ೼䖤㸠ᯊ䖨ಲᅗӀDŽ 13.5 lambda ᔶᔶᓣ ⱘখ᭄߭Ӯ㹿䅸ЎᰃϔϾᄫ݌ⱘ䬂/ؐᇍDŽ ೼ args ЁDŽབᵰՓ⫼ⱘᰃ**ࠡ㓔ˈ໮ԭټ㒘ᄬܗ⬅Ѣ೼ args ব䞣᳝ࠡ*ࠡ㓔ˈ᠔᳝໮ԭⱘߑ᭄খ᭄䛑Ӯ԰ЎϔϾ    !!!SRZHUVXP     !!!SRZHUVXP  UHWXUQWRWDO  LSRZHU WRWDO SRZ IRULLQDUJV  WRWDO  5HWXUQWKHVXPRIHDFKDUJXPHQWUDLVHGWRVSHFLILHGSRZHU  74 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥  DUJV SRZHU !!!GHISRZHUVXP 㽕㦋পৃব᭄䞣ⱘখ᭄ⱘᯊ׭⡍߿᳝⫼DŽ 㒘៪ᄫ݌ᔶᓣⱘখ᭄ⱘᯊ׭ˈ᳝ϔ⾡⡍⅞ⱘᮍ⊩ˈᅗߚ߿Փ⫼*੠**ࠡ㓔DŽ䖭⾡ᮍ⊩೼ߑ᭄䳔ܗᔧ㽕Փߑ᭄᥹ᬊ 㒘੠߫㸼ܗ13.4 ೼ߑ᭄Ё᥹ᬊ ⋕ǃ⏙Ἦⱘᮍ⊩ᅠ៤ⳌৠⱘᎹ԰DŽ ㋴ˈ㗠Փ⫼߫㸼㓐ড়ৃҹ⫼ϔ⾡᳈ࡴ㊒⹂ǃㅔܗ⫳ব࣪DŽ೼ᕜ໮ᯊ׭ˈ៥Ӏ䛑ᰃՓ⫼ᕾ⦃ᴹ໘⧚߫㸼Ёⱘ↣ϔϾ 䖭䞠៥ӀЎ⒵䎇ᴵӊ˄if i > 2˅ⱘ᭄ᣛᅮњϔϾ᪡԰˄2*i˅ˈҢ㗠ᇐߎϔϾᮄⱘ߫㸼DŽ⊼ᛣॳᴹⱘ߫㸼ᑊ≵᳝থ 13.3.1.1 ᅗབԩᎹ԰ >@ S\WKRQOLVWBFRPSUHKHQVLRQS\ 䕧ߎ SULQWOLVWWZR LIRULLQOLVWRQHLIL!@ OLVWWZR > OLVWRQH >@  OLVWBFRPSUHKHQVLRQS\)LOHQDPH XVUELQS\WKRQ ՟ 13.1 Փ⫼߫㸼㓐ড় 13.3.1 Փ⫼߫㸼㓐ড় DŽᇍѢ䖭⾡ᑨ⫼ˈ߫㸼㓐ড়ᰃ᳔⧚ᛇⱘᮍ⊩DŽס ߫㸼ˈՓ݊Ё᠔᳝໻Ѣ 2 ⱘ᭄䛑ᰃॳᴹⱘ 2 䗮䖛߫㸼㓐ড়ˈৃҹҢϔϾᏆ᳝ⱘ߫㸼ᇐߎϔϾᮄⱘ߫㸼DŽ՟བˈԴ᳝ϔϾ᭄ⱘ߫㸼ˈ㗠Դᛇ㽕ᕫࠄϔϾᇍᑨⱘ 13.3 ߫߫㸼㓐ড় ϔহ䕧ܹDŽ ៥Ӏᣝಲ䔺䬂⫼ᴹ⹂䅸䇁হᏆ㒣ᅠᭈњDŽ✊ৢˈPython ᅠ៤ᭈϾ䇁হⱘᠻ㸠ˈᑊϨ䖨ಲॳᴹⱘᦤ⼎ヺᑊϨㄝᕙϟ ϟˈމϾ՟ᄤЁˈᔧԴ䕧ܹњ݇䬂ᄫ if ПৢˈPython 㾷䞞఼ᡞᦤ⼎ヺᬍবЎ...ҹ㸼⼎䇁হ䖬≵᳝㒧ᴳDŽ೼䖭⾡ᚙ ঺໪ˈ⊼ᛣ೼Փ⫼ѸѦ῵ᓣⱘ Python 㾷䞞఼ⱘᯊ׭ˈᅗӮ䗮䖛ᙄᔧഄᬍবᦤ⼎ヺᴹᐂࡽԴ䕧ܹ䇁হDŽ೼Ϟ䴶䖭 㓽䖯ˈԴህৃҹᕜᮍ֓ഄ⏏ࡴϔϾ乱໪ⱘ䇁হDŽ Ԛᰃ䰸њẔ偠䫭䇃П໪៥ᔎ⚜ᓎ䆂Դϡ㽕Փ⫼䖭⾡㓽⬹ᮍ⊩DŽϡՓ⫼ᅗⱘϔϾЏ㽕ⱘ⧚⬅ᰃϔᮺԴՓ⫼њᙄᔧⱘ ҹՓԴⱘ⿟ᑣবᕫ ᇣϔѯ ˈৃخህབԴ᠔ⳟ㾕ⱘˈऩϾ䇁হ㹿Ⳉ᥹Փ⫼㗠ϡᰃ԰ЎϔϾ⣀ゟⱘഫՓ⫼DŽ㱑✊䖭ḋ   L !!!UHSU @ LWHP > !!!CLC  LWHP !!!LDSSHQG !!!L >@ eval(repr(object)) == objectDŽ repr ߑ᭄⫼ᴹপᕫᇍ䈵ⱘ㾘㣗ᄫヺІ㸼⼎DŽডᓩো˄г⿄䕀ᤶヺ˅ৃҹᅠ៤Ⳍৠⱘࡳ㛑DŽ⊼ᛣˈ೼໻໮᭄ᯊ׭᳝ 13.8 repr ߑ᭄ $VVHUWLRQ(UURU VWGLQ!OLQHLQ“)LOH  PRVWUHFHQWFDOOODVW 7UDFHEDFN  P\OLVW ! !!!DVVHUWOHQ  LWHP  !!!P\OLVWSRS  P\OLVW ! !!!DVVHUWOHQ @ LWHP !!!P\OLVW > ༅䋹ⱘᯊ׭ˈӮᓩথϔϾ AssertionErrorDŽ 偠䖭ϔ⚍ˈᑊϨ೼ᅗ䴲ⳳⱘᯊ׭ᓩথϔϾ䫭䇃ˈ䙷М assert 䇁হᰃᑨ⫼೼䖭⾡ᚙᔶϟⱘ⧚ᛇ䇁হDŽᔧ assert 䇁হ ㋴ˈ㗠Դᛇ㽕Ẕܗassert 䇁হ⫼ᴹໄᯢᶤϾᴵӊᰃⳳⱘDŽ՟བˈབᵰԴ䴲ᐌ⹂ֵᶤϾԴՓ⫼ⱘ߫㸼Ё㟇ᇥ᳝ϔϾ 13.7 assert 䇁হ     !!!HYDO ೼ᄫヺІЁⱘ᳝ᬜ Python 㸼䖒ᓣDŽϟ䴶ᰃϔϾㅔऩⱘ՟ᄤDŽټeval 䇁হ⫼ᴹ䅵ㅫᄬ +HOOR:RUOG  SULQW+HOOR:RUOG !!!H[HF ᄫヺІˈ✊ৢՓ⫼ exec 䇁হᠻ㸠䖭ѯ䇁হDŽϟ䴶ᰃϔϾㅔऩⱘ՟ᄤDŽ ᄬ೼ᄫヺІ៪᭛ӊЁⱘ Python 䇁হDŽ՟བˈ៥Ӏৃҹ೼䖤㸠ᯊ⫳៤ϔϾࣙ৿ Python ҷⷕⱘټexec 䇁হ⫼ᴹᠻ㸠 13.6 exec ੠੠ eval 䇁হ 75 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥 Ͼ䚂ӊ߫㸼Ёˈ䇏㗙ӮϢ៥䅼䆎བԩᬍ䖯ᴀкDŽ ࣙDŽབᵰԴᛇ㽕㸼䖒Դᇍ䖭ϾЏ乬ⱘᛣ㾕ˈ䇋ࡴܹ E\WHRIS\WKRQ 䚂ӊ߫㸼DŽ೼䖭 ៥ᠧㅫЎᴀк㓪ݭϔ៪ϸϾ݇Ѣ *8, 㓪⿟ⱘゴ㡖DŽ៥ৃ㛑Ӯ䗝ᢽ Z[3\WKRQ ԰ЎᎹ݋ ᳾ᴹⱘゴ㡖 ㄀ϝϾ㗗㰥಴㋴ḍ᥂Դᰃ Linux ϟⱘ KDE ⫼᠋䖬ᰃ GNOME ⫼᠋㗠ᅮDŽ ㋴ᰃԴᰃ৺ᜓᛣЎ GUI Ꮉ݋Ҭ䌍DŽ݊⃵㗗㰥ⱘᰃԴᰃᛇ䅽Դⱘ⿟ᑣ䖤㸠೼ Linux ϟǃWindows ϟ䖬ᰃϸ㗙䛑㽕DŽ ೼Ϟ䗄Ꮉ݋Ё䗝ᢽϔϾDŽ佪㽕㗗㰥ⱘ಴މPython GUI Ꮉ݋DŽ៥ᓎ䆂Դḍ᥂Դⱘᚙ ޚϡᑌⱘᰃˈᑊ≵᳝ऩϔⱘᷛ 14.1.1 GUI ᎹᎹ݋ὖᣀ 77 䞡ᮄᥦ⠜:shiiuen ܗ⋕≜ :ॳ԰˖Swaroop, C. H. 㗏䆥
还剩38页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 15 金币 [ 分享pdf获得金币 ] 5 人已下载

下载pdf

pdf贡献者

lishushan

贡献于2011-11-09

下载需要 15 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf