$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Django Girls Taipei: Python
Search
Tzu-ping Chung
May 30, 2015
Programming
1
110
Django Girls Taipei: Python
Python 教學(用於 Django Girls Taipei)
Tzu-ping Chung
May 30, 2015
Tweet
Share
More Decks by Tzu-ping Chung
See All by Tzu-ping Chung
Datasets: What it is, and how it was made
uranusjr
0
170
Let’s fix extras in Core Metadata 3.0
uranusjr
0
570
Python Packaging: Why Don’t You Just…?
uranusjr
1
250
這樣的開發環境沒問題嗎?
uranusjr
9
2.7k
Django After Web 2.0
uranusjr
3
2.2k
We Store Cheese in A Warehouse
uranusjr
1
480
The Python You Don’t Know
uranusjr
17
3.2k
Python and Asynchrony
uranusjr
0
410
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
6
2.4k
WebRTC と Rust と8K 60fps
tnoho
2
1.9k
認証・認可の基本を学ぼう前編
kouyuume
0
190
sbt 2
xuwei_k
0
260
MAP, Jigsaw, Code Golf 振り返り会 by 関東Kaggler会|Jigsaw 15th Solution
hasibirok0
0
230
テストやOSS開発に役立つSetup PHP Action
matsuo_atsushi
0
150
Rediscover the Console - SymfonyCon Amsterdam 2025
chalasr
2
160
AIコードレビューがチームの"文脈"を 読めるようになるまで
marutaku
0
350
AWS CDKの推しポイントN選
akihisaikeda
1
240
Level up your Gemini CLI - D&D Style!
palladius
1
180
まだ間に合う!Claude Code元年をふりかえる
nogu66
3
460
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
120
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
46
7.8k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.1k
Java REST API Framework Comparison - PWX 2021
mraible
34
9k
Designing for Performance
lara
610
69k
A designer walks into a library…
pauljervisheath
210
24k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Visualization
eitanlees
150
16k
Optimizing for Happiness
mojombo
379
70k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.2k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Thoughts on Productivity
jonyablonski
73
5k
Build The Right Thing And Hit Your Dates
maggiecrowley
38
3k
Transcript
Python djangogirlstaipei.herokuapp.com/tutorials/python/
(٥̫٥)א 锞㢵䭸來 Python
C:\Users\Tzu-‐ping Chung>python3 Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014,
10:38:22) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
uranusjr@ubuntu:~$ python3 Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
uranusjr:~ uranusjr$ python3 Python 3.4.1 (default, Aug 24 2014,
21:32:40) [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-‐503.0.40)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
>>> 2 + 3 5
(٥̫٥) 5 2 + 3
>>> 4 * 5 ??? >>> 40 /
(7 -‐ 3) * 2 ??? >>> 2 ** 3 ???
>>> 'I am a Django Girl' 'I am a
Django Girl' >>> 'Hello ' + 'Django' 'Hello Django' >>> 'Django ' * 3 'Django Django Django ' >>> len('Django') 6
>>> len('Django') 6 ⴅ䒭
ⴄ䒭涸せ㶵(「鑁)
>>> len(304023) Traceback (most recent call last):
File "<stdin>", line 1, in <module> TypeError: object of type 'int' has no len()
>>> len(304023) Traceback (most recent call last):
File "<stdin>", line 1, in <module> TypeError: object of type 'int' has no len()
>>> len(str(304023)) 6
>>> str(304023) '304023' >>> int('123') 123
>>> int('Hello!') ???
>>> int('Hello!') Traceback (most recent call last):
File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'Hello!'
>>> pet = 'Kitty' >>> 岣䠐鸍儗⦪湫陻㐻չ♶剙պ㔐⫄湫〪剙 랾랾䪾隵侶涸⧩鏣饯⢵կ
'Kitty' pet
>>> pet = 'Kitty' >>> pet 'Kitty'
>>> len(pet) 5 >>> seven = 7 >>> six = 6 >>> answer = seven * six >>> answer 42
>>> pet = 'Kitty' >>> pat ???
(٥̫٥) 'Kitty' pet 'Kitty' pet
(խ̫)? ???? pat 'Kitty' pet
>>> pet = 'Kitty' >>> pat Traceback (most
recent call last): File "<stdin>", line 1, in <module> NameError: name 'pat' is not defined
>>> [] [] >>> lottery = [59, 42,
30, 19, 12, 3] >>> len(lottery) 6
>>> lottery[0] 59 >>> lottery[1] 42
>>> lottery[-‐1] ??? >>> lottery[1000] ???
>>> lottery = [59, 42, 30, 19, 12, 3]
>>> print(lottery[1000]) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range
>>> lottery = [59, 42, 30, 19, 12, 3]
>>> lottery[1] = 22 >>> lottery [59, 22, 30, 19, 12, 3]
>>> {} {} >>> my_information = {
... 'name': 'Pusheen the Cat', ... 'country': 'USA', ... 'favorite_numbers': [42, 105]} >>> len(my_information) 3
>>> my_information['name'] Pusheen the Cat >>> my_information['age']
???
>>> my_information['age'] Traceback (most recent call last):
File "<stdin>", line 1, in <module> KeyError: 'age'
>>> my_information {'name': 'Pusheen the Cat', 'country': 'USA',
'favorite_numbers': [42, 105]} >>> my_information['color'] = 'gray' >>> len(my_information) 4 >>> del my_information['farovite_numbers'] >>> my_information {'name': 'Pusheen the Cat', 'country': 'USA', 'color': 'gray'}
>>> my_information['name'] = 'Pusheen' >>> my_information {'name': 'Pusheen',
'country': 'USA', 'color': 'gray'}
>>> 5 > 2 True >>> 3 <
1 False >>> 5 > 2 * 3 False >>> 1 == 1 True
>>> 6 >= 12 / 2 True >>>
3 <= 2 False >>> 6 > 2 and 2 < 3 True >>> 3 > 2 and 2 < 1 False >>> 3 > 2 or 2 < 1 True
>>> 'Sherlock' > 'John' ??? >>> 1 <
'Django' ???
>>> 'Sherlock' > 'John' True >>> 1 <
'Django' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: int() < str()
• NameError • ValueError • IndexError •
KeyError • TypeError
>>> is_cool = True >>> is_cool True
>>> can_go_home = 2 > 5 >>> can_go_home False
>>> True and True ??? >>> False and
True ??? >>> True or 1 == 1 ??? >>> 1 != 2 ???
>>> pet = 'Pusheen' >>> pet 'Pusheen'
>>> print(pet) Pusheen
(٥̫٥) 'Pusheen' QFU酭僽➊ 랃 'Pusheen' pet
(٥̫٥)א ⽫ⴀQFU涸Ⰹ㺂 Pusheen 'Pusheen' pet
(٥̫٥) 6 TJY酭僽➊랃 6 six
(٥̫٥)א ⽫ⴀTJY涸Ⰹ㺂 6 six 6
None
$ python3 my_program.py 'I am a Django Girl'
㛂遤鸏❉䭸⟂ (٥̫٥)א 穡卓
if 3 > 2:
$ python3 my_program.py File "my_program.py", line 2
^ SyntaxError: unexpected EOF while parsing
if 3 > 2: print('It works!')
㔋⦐瑠涯 侄箺銲岣䠐♧♴㷷㆞涸箟鰿㐻剣鏣㹀䧭 tab with spaces ㄤ tab size = 4
$ python3 my_program.py It works!
if 2 > 5: print('2 >
5') else: print('2 <= 5') $ python3 my_program.py 2 <= 5
if 2 > 5: print('2 >
5') elif 2 < 5: print('2 < 5') else: print('2 == 5') $ python3 my_program.py 2 < 5
def hi(): print('Hi there!')
print('How are you?') $ python3 my_program.py
def hi(): print('Hi there!')
print('How are you?') hi() $ python3 my_program.py Hi there! How are you?
def hi(name): print('Hi' + name +'!')
print('How are you?') hi()
$ python3 my_program.py Traceback (most recent call last):
File "my_program.py", line 4, in hi() TypeError: hi() missing 1 required positional argument: 'name'
def hi(name): print('Hi' + name +'!')
print('How are you?') hi('Pusheen') $ python3 my_program.py Hi Pusheen! How are you?
㋿
class Cat: def meow(self):
print('Meow!')
class Cat: def meow(self):
print('Meow!') pusheen = Cat() pusheen.meow() $ python3 cat.py Meow!
class Cat: def __init__(self, name):
self.name = name def meow(self): print(self.name + ' Meow!') pusheen = Cat() pusheen.meow()
$ python3 cat.py Traceback (most recent call last):
File "cat.py", line 8, in pusheen = Cat() TypeError: __init__() missing 1 required positional argument: 'name'
class Cat: def __init__(self, name):
self.name = name def meow(self): print(self.name + ' Meow!') pusheen = Cat() pusheen.meow()
class Cat: def __init__(self, name):
self.name = name def meow(self): print(self.name + ' Meow!') pusheen = Cat('Pusheen') pusheen.meow() $ python3 cat.py Pusheen Meow!
class Cat: def __init__(self, name):
self.name = name def meow(self): print(self.name + ' Meow!') pusheen = Cat('Pusheen') pusheen.meow() print(pusheen) $ python3 cat.py Pusheen Meow! <__main__.Cat object at 0x1006f15c0>
class Cat: def __init__(self, name):
self.name = name def __str__(self): return 'Cat: ' + self.name def meow(self): print(self.name + ' Meow!') pusheen = Cat('Pusheen') pusheen.meow() print(pusheen)
$ python3 cat.py Pusheen Meow! Cat: Pusheen
def hi(name): print('Hi ' + name
+ '!') girls = ['Michelle', 'Ola', 'you'] for name in girls: hi(name) print('Next girl')
$ python3 say_hi.py Hi Michelle! Next girl
Hi Ola! Next girl Hi you! Next girl
class Cat: def __init__(self, name):
self.name = name def __str__(self): return 'Cat: ' + self.name def meow(self): print(self.name + ' Meow!') pet.py
import pet cats = [
pet.Cat('Kitty'), pet.Cat('Pusheen'), pet.Cat('Doraemon'), ] for cat in cats: cat.meow() say_meow.py
$ ls pet.py say_meow.py $ python3
say_meow.py Kitty Meow! Pusheen Meow! Doraemon Meow!
# 䪾 pet 垷穈Ⰵ import pet # 䪾
pet 酬涸 Cat 겲ⴽⰅ from pet import Cat
姻鋊邍爚䒭 3FHVMBS&YQSFTTJPO
〵⻍䋑⥌纏⼦匡➋騟贫垜 넞꧆䋑뛥㚆⼦⚥姻㔋騟贫 㾓匌簖惐䊝ꔩꆄꤰ遳贫 倞畾簖畾⻍䋑⯕僈Ⱉ騟匌♧媯贫
〵⻍䋑⥌纏⼦匡➋騟贫垜 넞꧆䋑뛥㚆⼦⚥姻㔋騟贫 㾓匌簖惐䊝ꔩꆄꤰ遳贫 倞畾簖畾⻍䋑⯕僈Ⱉ騟匌♧媯贫
import re addresses = [
'台北市信義區松仁路7號8樓', '高雄市鹽埕區中正四路274號', '屏東縣潮州鎮金陵街31號', '新竹縣竹北市光明六路東一段183號', ] for addr in addresses: print(re.search(r'\w+市', addr)) address.py
import re addresses = [
'台北市信義區松仁路7號8樓', '高雄市鹽埕區中正四路274號', '屏東縣潮州鎮金陵街31號', '新竹縣竹北市光明六路東一段183號', ] for addr in addresses: print(re.search(r'\w+市', addr)) address.py
\w+䋐 ♧⦐㶶 涸匌銯 ♧⦐⟃♳ չ䋑պ鸏⦐㶶
for addr in addresses: match =
re.search(r'(?P<city>\w+市)', addr) if match: print(match.group('city')) address.py $ python3 address.py 台北市 高雄市 新竹縣竹北市
for addr in addresses: match =
re.search(r'(?P<city>\w{2}市)', addr) if match: print(match.group('city')) address.py $ python3 address.py 台北市 高雄市 竹北市
for addr in addresses: match =
re.search(r'^(?P<city>\w{2}市)', addr) if match: print(match.group('city')) address.py $ python3 address.py 台北市 高雄市
〵⻍䋑⥌纏⼦匡➋騟贫垜 넞꧆䋑뛥㚆⼦⚥姻㔋騟贫 㾓匌簖惐䊝ꔩꆄꤰ遳贫 倞畾簖畾⻍䋑⯕僈Ⱉ騟匌♧媯贫
〵⻍䋑⥌纏⼦匡➋騟贫垜 넞꧆䋑뛥㚆⼦⚥姻㔋騟贫 㾓匌簖惐䊝ꔩꆄꤰ遳贫 倞畾簖畾⻍䋑⯕僈Ⱉ騟匌♧媯贫
for addr in addresses: match =
re.search( r'(?P<no>\d+號)$', addr, ) if match: print(match.group('no')) address.py $ python3 address.py 274號 31號 183號
for addr in addresses: match =
re.search( r'(?P<no>\d+號(\d+樓)?)$', addr, ) if match: print(match.group('no')) address.py $ python3 address.py 7號8樓 274號 31號 183號
^ 걧 $ 穡㽵 \w ♧⦐㶶 \d ♧⦐䬘⠭侸㶶 + 涸匌銯♧⦐⟃♳
{num} 涸匌銯剤num⦐ ? 涸匌銯ꨫ䧴♧⦐ ( ... ) 䪾匌銯穉ざ㖈♧饱 (?P<name> ...) 䪾匌銯穉ざ㖈♧饱⚛《そ䧭name
‧ӹ,:*:‧\(ӟ)/‧:*‧°ӹ*