麻瓜也能上手的爬說語:開源社 Python 實用入門 / Parseltongue for Muggles: Pratical Introduction of Python

B3ecf4287d58054135af5d7b4d741021?s=47 Poren Chiang
December 02, 2016

麻瓜也能上手的爬說語:開源社 Python 實用入門 / Parseltongue for Muggles: Pratical Introduction of Python

“Parseltongue for Muggles: Pratical Introduction of Python”
2016/12/2 Talk @ NTU Open Source Community, Taipei, Taiwan.

B3ecf4287d58054135af5d7b4d741021?s=128

Poren Chiang

December 02, 2016
Tweet

Transcript

  1. 讔 絤 犖 胼 Ӥ ಋ ጱ 粖藯承 շ荩㣐Ꟛ彂爢Python㻜欽ⰅꟌո 2016/12/2

    RSChiang @ NTUOSC / CC BY-SA 4.0
  2. ߢ㾏
 ౯ฎভຣ犨կ Photo by Hiram Huang on Flickr, CC BY-SA

    2.0 岄傀य़䋊ဩ஌䋊羬ࢥଙ羷 岄य़樄რᐒ咳蚏Ո膑玊ێ SITCON 2017 者ݟ ݢ犥ࣁ rs@ntuosc.org ౲ฎ poren.tw 膏౯肥媒牐
  3. 樄তԏ獮 • 嘦ਧ૪妿蕕অԧ Python 3.4+
 → 螭䷱伛猋অጱՈ牧纸盅䨝൉׀ݱଘݣጱਞ蕕ොୗ • 蝡ฎ樄硯ୗጱ抓纷牧֦ݢ犥ࣁ樄რᐒ Slack物

    • ܨ碻൉㺔ҁ哴ᓕ tag @RSChiang҂ • ᥝ穩᯿蕦Ӟ螁牏ے盠౲仂眸蝧ଶ • ૶磭౯氃क़䌘礓犚蟂獤狶愆獅
  4. Python • 1991 ଙڊ匍ጱ纷ୗ承᥺ • ̿ग़᯿َ塅̀ҁmulti-paradigm҂
 → 篷抷ࡅ稭Ջ讕觓໒᮷胼Ӥಋ (ɾТɾ) •

    ݱଘݣӤ᮷胼ֵአ牧ᘒӬ犋襑ᥝ翥捌
  5. For Windows 獮ஃ python.org牧ӥ斉磧碝粚๜ጱ Python 3牪 ೲᆙ瞲纈ਠ౮ਞ蕕牐

  6. For Mac • 羬翄獉ୌጱ Python 粚๜穉斃膑 • brew install pyenv

    • echo 'eval "$(pyenv init -)"' >> ~/.bash_profile • pyenv install 3.5.2 • 蝚螂 pyenv shell 獥矦 Python 粚๜
  7. For Linux • ֵአ python3 瞲犤珸㵕 Python 3
 Ubuntu 14.04+

    ૪妿獉ୌ Python ԧ • 蝚螂 python3 --version 䲒礚褰ᴫጱ粚๜ • sudo apt-get upgrade python3
 ݢ犥Ӥ翕ತತ֦ጱ咳ᤈ粚螕አጱॺկݷ圸
  8. ತک Python OS X: Spotlight → 奰ᒒ秚 Linux: Ctrl +

    Alt + Tҁ憙咳ᤈ粚ᘒਧ҂ Windows: ℂ纷ୗڜ蔭疨ತ IDLE
  9. ಑㮆೗޷މ牦

  10. print("ࡣ牧Ӯኴ牦") ޞ藗 Python ౯㮉ᥝ狶Ջ讕牧 蝡愊ฎ玢ڊ䩚ᥜ㬵牐 ೡ皭䨝಩ಅ磪ᥝڜڊ㬵ጱ虻懱۱֘牐 疰猟ࣁ䌃෈ᒍӞ䰬牧 櫕୚蒈ޞ藗 Python 蝡ฎӞྦྷ෈ਁ牐

    㻌୚蒈犖ݢ犥አ牐
  11. ು㮆㫂碍

  12. import random 藶क़ᶎጱ纷ୗ ਠ౮Ӟ犚౯㮉犋䨝ጱۑ胼牐 ౯㮉ᥝತጱक़蟂纷ୗฎ̿random̀

  13. random.randint(1, 49) ̿ጱ̀ random integer ጱ婘䌃牧 蝡㮆獢ୗ䨝叨ኞՕෝ妔ਧ塅瑻獉ጱ碉碍牐 㴄㴄㶓獈ጱ̿random̀ ℂ 1

    ک 49
  14. 蒂ቘ虻碘

  15. restaurants = ["ၚय़ᛔۗ訇", …] 蚤碍䋊Ӥጱ̿ፘ缛̀犋Ӟ䰬牧 蝡愊ฎ̿瞲窔̀ጱ఺௏牐 ොೡ皭ޞ藗 Python 蝡ฎӞ㮆檋ڜ牐 ਧ嬝Ӟ㮆ݷਁ牧

    蝡愊ฎ̿訇皣㮉̀牐 愊ᶎݢ犥ਂ硯 犋ྊӞྦྷ෈ਁ牐
  16. 纸֢狕硬

  17. restaurants.append("議澕ੜ犊") restaurants += ["ੜᐰ", "ݣᑀय़"] 植㖈restaurants酭꬗僽⦐ꤏ⴪涸չ暟⟝պ ౯㮉ݢ犥ᥝ檋ڜᴫےӞྦྷ෈ਁک磧ੲᒒ 殹搭⛳〳⟃♧妄꣡⸈㥪㢵媯a = a

    + b " a += b
  18. restaurants[0] len(restaurants) 犖ݢ犥አᔱ୚翥蒈玲独牧ℂ 0 樄ত碍 玲஑檋ڜጱ裾ଶ

  19. 㬵ುඪ壆މ牦

  20. ٌ䋿磪讨य़๭ੜአԧ 㬵㵕讨 Python ጱ፥໒

  21. from urllib.request import urlopen ইຎ֦螭ࣁአ膑粚๜ (2.7)牧಩ਙ矦౮ urllib2

  22. urlopen("http://orange.tw").read() ಑樄翕࣎ҁURL҂ 獊蟂捝ڊ㬵

  23. 匍ࣁ牧૪Ꭳአ箛ԧ牦

  24. 秇奲 (modules) • 磪ԧ瞲䠁牧者ᥝ磪禼ಋ಍胼虏 Ի段禼㿁㵕蚏㬵牦 • ℂ媑瑽ک碍硁獤ຉ牧犋ݶ秇奲 ݢ犥㶧ۗਠ౮犋ݶጱ犨率牐 • Ӟ㮆

    Python 䲆 (.py) 犖ฎӞ㮆 加缏ጱ秇奲߶牐
  25. XKCD: Python, CC BY-NC 2.5

  26. کߺ愊ತ秇奲牫 • 獉ୌጱ秇奲ݢ犥ℂ Python ਥො翕ᒊ礚扇ک • 皃㮆秇奲ݢ犥ᤩ۱౮Ӟ㮆ॺկ (package)
 猟禼ṛጱ硌獅۱牧蕕Ӥ݄ԏ盅疰磪碝ದ胼ݢ犥አԧ •

    ᐒᗭ揙糽ጱॺկ䨝Ӥ㯽ᛗ pypi.python.org • ݢ犥蝚螂ॺկᓕቘ纷ୗ pip ਞ蕕碝ጱॺկ
  27. অአጱ秇奲㮉 膐皃㮆纷ୗӾଉአጱֺৼ

  28. import base64 base64.b64encode("樄რᐒ".encode()) 磪岉蚕ጱ扖ݢ犥 Google Ӟӥ牧 ਙ胼಩犨֜虻碘旉౮ݢ氥纈ጱ෈ਁ ض翥嘨౮ԫ蝱֖໒ୗ

  29. import smtplib client = smtplib.SMTP_SSL(
 "mail.ntu.edu.tw", port=587) client.sendmail(…) 盄ग़Ո᮷犋Ꭳ螇牧 ٌ䋿ݝᥝӣᤈ纷ୗ疰ݢ犥ੀӞ੗

    email
  30. import re re.match(r"\d+:\d+", "12:00") ྋ憒蔭螈ୗ (regular expression) ݢ犥አ㬵穉䌘牏玲դ෈ਁ 硯蝱౯㮉ᥝ䲒涢ጱਁԀ獉਻牧
 狶襎扖蒈嘨涢挨ጱ碻狡盄অአ߶牐

    矟蝄ᥝ穉䌘ጱฎ̿碍ਁ (\d) Ӟ㮆犥Ӥ (+) 牏 ٚӞ㮆ٛ蒈牏ٚ碍ਁӞ㮆犥Ӥ̀蝡䰬ጱ໒ୗ ”raw string” 虏 Python ஺ኼ愊ᶎጱ粬ྛᒧ蒈
  31. XKCD: Regular Expressions, CC BY-NC 2.5

  32. Ӟ犚ྋ憒蔭螈ୗ • 薫ೡ皭 [] 䨝穉䌘̿ೡ皭愊犨֜Ӟ㮆ਁ̀ • ူ窚ೡ皭 {n} դ蔭̿者و磪 n

    㮆̀ • 磪犚ଉአጱ䨝磪婘䌃牧ֺই藯 [0-9] ݢ犥䌃౮ \d • 膐蛪獤挨ਁ蒈傶ֺ牧[A-Z][12][0-9]{8}
  33. 膐ֺ物አ re 螂筪Ի矦䋊໊

  34. 䋿瑿娞聜 蝡ฎӞ皰 COSCUP 2016 ጱᆙ粙牐
 ইຎ౯㮉૶磭አ Python 疥ਙ纸֢獶᤮牧扗ெ讕旰珌牫 Photo by

    य़ۗ @ COSCUP on Flickr, CC BY-SA 2.0
  35. Pillow Python ୽猟蒂ቘ獢ୗ䓚 (PIL) ጱ獤ඪ粚๜牧ฎ磧অӤಋጱ ୽猟蒂ቘॺկԏӞ牪 ࣁ pillow.readthedocs.org 磪 托奞ጱֵአ෈կ牐

  36. Cheat sheet [sudo] pip install Pillow from PIL import Image

    photo = Image.open('Desktop/AARRR/ਡਡ.jpg') ਡਡ = photo.crop((0, 0, ୽猟.width * 2 / 3, 
 ̴̴ photo.height * 2 / 3)).resize((1024, 768)) 箜ਡਡ = ਡਡ.convert('L') 箜ਡਡ.save('Desktop/AARRR/БББ.jpg')
  37. य़ۑޞ౮牦 匍ࣁ֦૪妿䨝च器ጱ୽猟蒂ቘԧ牧֢傶ᛔ౯౮裾ጱ娞聜牧
 Ꮈ绗獉ୌ獢ୗ os.listdir()牧瞙稞಩虻碘䄸愊ጱᆙ粙婘ੜމ牐

  38. ޾ C++ ጱ䌘穉 愆獅硽๭

  39. #include <iostream> using namespace std; int main(int argc, char* argv)

    { cout << “Hi NTUOSC!”; return 0; } print(“Hi NTUOSC!”) $ 1ZUIPO ٌ䋿Ӟᤈ疰胼蔭螈竃༩ 纷ୗᥝ狶Ջ讕ԧ牐 蚤 Python ጱ print() ఺௏ฎፘݶጱ牐
  40. string a = “ࠡ”;
 string b = “”; for (int

    i = 1; i <= 5; i++) { b += a; } cout << b; print(“ࠡ” * 5) $ 1ZUIPO ෈ਁᥝ虋Բ狝牫 ፗ矑犔犥 5 疰অԧ牧 犋஠᪒ࢧ݄䢏槑Ꭺ牐
  41. for (int i = 1; i <= 5; i++) {

    cout << “#” << i << “\n”; } for i in range(5): print(“#” + str(i)) $ 1ZUIPO 懿஑ᥝአ绚໒婘矎߶牐
  42. Questions? RSChiang @ NTUOSC / CC BY-SA 4.0 *抓盅娞聜ጱ綡櫞褾ዩ牧 ᮷ݢ犥ک樄რᐒ

    Slack 扇㺔߶牐