Slide 1

Slide 1 text

讔 絤 犖 胼 Ӥ ಋ ጱ 粖藯承 շ荩㣐Ꟛ彂爢Python㻜欽ⰅꟌո 2016/12/2 RSChiang @ NTUOSC / CC BY-SA 4.0

Slide 2

Slide 2 text

ߢ㾏
 ౯ฎভຣ犨կ Photo by Hiram Huang on Flickr, CC BY-SA 2.0 岄傀य़䋊ဩ஌䋊羬ࢥଙ羷 岄य़樄რᐒ咳蚏Ո膑玊ێ SITCON 2017 者ݟ ݢ犥ࣁ [email protected] ౲ฎ poren.tw 膏౯肥媒牐

Slide 3

Slide 3 text

樄তԏ獮 • 嘦ਧ૪妿蕕অԧ Python 3.4+
 → 螭䷱伛猋অጱՈ牧纸盅䨝൉׀ݱଘݣጱਞ蕕ොୗ • 蝡ฎ樄硯ୗጱ抓纷牧֦ݢ犥ࣁ樄რᐒ Slack物 • ܨ碻൉㺔ҁ哴ᓕ tag @RSChiang҂ • ᥝ穩᯿蕦Ӟ螁牏ے盠౲仂眸蝧ଶ • ૶磭౯氃क़䌘礓犚蟂獤狶愆獅

Slide 4

Slide 4 text

Python • 1991 ଙڊ匍ጱ纷ୗ承᥺ • ̿ग़᯿َ塅̀ҁmulti-paradigm҂
 → 篷抷ࡅ稭Ջ讕觓໒᮷胼Ӥಋ (ɾТɾ) • ݱଘݣӤ᮷胼ֵአ牧ᘒӬ犋襑ᥝ翥捌

Slide 5

Slide 5 text

For Windows 獮ஃ python.org牧ӥ斉磧碝粚๜ጱ Python 3牪 ೲᆙ瞲纈ਠ౮ਞ蕕牐

Slide 6

Slide 6 text

For Mac • 羬翄獉ୌጱ Python 粚๜穉斃膑 • brew install pyenv • echo 'eval "$(pyenv init -)"' >> ~/.bash_profile • pyenv install 3.5.2 • 蝚螂 pyenv shell 獥矦 Python 粚๜

Slide 7

Slide 7 text

For Linux • ֵአ python3 瞲犤珸㵕 Python 3
 Ubuntu 14.04+ ૪妿獉ୌ Python ԧ • 蝚螂 python3 --version 䲒礚褰ᴫጱ粚๜ • sudo apt-get upgrade python3
 ݢ犥Ӥ翕ತತ֦ጱ咳ᤈ粚螕አጱॺկݷ圸

Slide 8

Slide 8 text

ತک Python OS X: Spotlight → 奰ᒒ秚 Linux: Ctrl + Alt + Tҁ憙咳ᤈ粚ᘒਧ҂ Windows: ℂ纷ୗڜ蔭疨ತ IDLE

Slide 9

Slide 9 text

಑㮆೗޷މ牦

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

ು㮆㫂碍

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

蒂ቘ虻碘

Slide 15

Slide 15 text

restaurants = ["ၚय़ᛔۗ訇", …] 蚤碍䋊Ӥጱ̿ፘ缛̀犋Ӟ䰬牧 蝡愊ฎ̿瞲窔̀ጱ఺௏牐 ොೡ皭ޞ藗 Python 蝡ฎӞ㮆檋ڜ牐 ਧ嬝Ӟ㮆ݷਁ牧 蝡愊ฎ̿訇皣㮉̀牐 愊ᶎݢ犥ਂ硯 犋ྊӞྦྷ෈ਁ牐

Slide 16

Slide 16 text

纸֢狕硬

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

restaurants[0] len(restaurants) 犖ݢ犥አᔱ୚翥蒈玲独牧ℂ 0 樄ত碍 玲஑檋ڜጱ裾ଶ

Slide 19

Slide 19 text

㬵ುඪ壆މ牦

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

匍ࣁ牧૪Ꭳአ箛ԧ牦

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

XKCD: Python, CC BY-NC 2.5

Slide 26

Slide 26 text

کߺ愊ತ秇奲牫 • 獉ୌጱ秇奲ݢ犥ℂ Python ਥො翕ᒊ礚扇ک • 皃㮆秇奲ݢ犥ᤩ۱౮Ӟ㮆ॺկ (package)
 猟禼ṛጱ硌獅۱牧蕕Ӥ݄ԏ盅疰磪碝ದ胼ݢ犥አԧ • ᐒᗭ揙糽ጱॺկ䨝Ӥ㯽ᛗ pypi.python.org • ݢ犥蝚螂ॺկᓕቘ纷ୗ pip ਞ蕕碝ጱॺկ

Slide 27

Slide 27 text

অአጱ秇奲㮉 膐皃㮆纷ୗӾଉአጱֺৼ

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

import re re.match(r"\d+:\d+", "12:00") ྋ憒蔭螈ୗ (regular expression) ݢ犥አ㬵穉䌘牏玲դ෈ਁ 硯蝱౯㮉ᥝ䲒涢ጱਁԀ獉਻牧
 狶襎扖蒈嘨涢挨ጱ碻狡盄অአ߶牐 矟蝄ᥝ穉䌘ጱฎ̿碍ਁ (\d) Ӟ㮆犥Ӥ (+) 牏 ٚӞ㮆ٛ蒈牏ٚ碍ਁӞ㮆犥Ӥ̀蝡䰬ጱ໒ୗ ”raw string” 虏 Python ஺ኼ愊ᶎጱ粬ྛᒧ蒈

Slide 31

Slide 31 text

XKCD: Regular Expressions, CC BY-NC 2.5

Slide 32

Slide 32 text

Ӟ犚ྋ憒蔭螈ୗ • 薫ೡ皭 [] 䨝穉䌘̿ೡ皭愊犨֜Ӟ㮆ਁ̀ • ူ窚ೡ皭 {n} դ蔭̿者و磪 n 㮆̀ • 磪犚ଉአጱ䨝磪婘䌃牧ֺই藯 [0-9] ݢ犥䌃౮ \d • 膐蛪獤挨ਁ蒈傶ֺ牧[A-Z][12][0-9]{8}

Slide 33

Slide 33 text

膐ֺ物አ re 螂筪Ի矦䋊໊

Slide 34

Slide 34 text

䋿瑿娞聜 蝡ฎӞ皰 COSCUP 2016 ጱᆙ粙牐
 ইຎ౯㮉૶磭አ Python 疥ਙ纸֢獶᤮牧扗ெ讕旰珌牫 Photo by य़ۗ @ COSCUP on Flickr, CC BY-SA 2.0

Slide 35

Slide 35 text

Pillow Python ୽猟蒂ቘ獢ୗ䓚 (PIL) ጱ獤ඪ粚๜牧ฎ磧অӤಋጱ ୽猟蒂ቘॺկԏӞ牪 ࣁ pillow.readthedocs.org 磪 托奞ጱֵአ෈կ牐

Slide 36

Slide 36 text

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')

Slide 37

Slide 37 text

य़ۑޞ౮牦 匍ࣁ֦૪妿䨝च器ጱ୽猟蒂ቘԧ牧֢傶ᛔ౯౮裾ጱ娞聜牧
 Ꮈ绗獉ୌ獢ୗ os.listdir()牧瞙稞಩虻碘䄸愊ጱᆙ粙婘ੜމ牐

Slide 38

Slide 38 text

޾ C++ ጱ䌘穉 愆獅硽๭

Slide 39

Slide 39 text

#include using namespace std; int main(int argc, char* argv) { cout << “Hi NTUOSC!”; return 0; } print(“Hi NTUOSC!”) $ 1ZUIPO ٌ䋿Ӟᤈ疰胼蔭螈竃༩ 纷ୗᥝ狶Ջ讕ԧ牐 蚤 Python ጱ print() ఺௏ฎፘݶጱ牐

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

for (int i = 1; i <= 5; i++) { cout << “#” << i << “\n”; } for i in range(5): print(“#” + str(i)) $ 1ZUIPO 懿஑ᥝአ绚໒婘矎߶牐

Slide 42

Slide 42 text

Questions? RSChiang @ NTUOSC / CC BY-SA 4.0 *抓盅娞聜ጱ綡櫞褾ዩ牧 ᮷ݢ犥ک樄რᐒ Slack 扇㺔߶牐