Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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
100
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
120
Let’s fix extras in Core Metadata 3.0
uranusjr
0
510
Python Packaging: Why Don’t You Just…?
uranusjr
1
220
這樣的開發環境沒問題嗎?
uranusjr
9
2.6k
Django After Web 2.0
uranusjr
3
2.1k
We Store Cheese in A Warehouse
uranusjr
1
450
The Python You Don’t Know
uranusjr
17
3.1k
Python and Asynchrony
uranusjr
0
360
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
複数のAWSアカウントから横断で 利用する Lambda Authorizer の作り方
tc3jp
0
110
Django NinjaによるAPI開発の効率化とリプレースの実践
kashewnuts
1
250
5分で理解する SOLID 原則 #phpcon_nagoya
shogogg
1
300
From the Wild into the Clouds - Laravel Meetup Talk
neverything
0
130
Go 1.24でジェネリックになった型エイリアスの紹介
syumai
2
280
Rubyと自由とAIと
yotii23
6
1.7k
15分で学ぶDuckDBの可愛い使い方 DuckDBの最近の更新
notrogue
3
490
パスキーのすべて ── 導入・UX設計・実装の紹介 / 20250213 パスキー開発者の集い
kuralab
3
880
Djangoにおける複数ユーザー種別認証の設計アプローチ@DjangoCongress JP 2025
delhi09
PRO
4
460
仕様変更に耐えるための"今の"DRY原則を考える
mkmk884
9
3.2k
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
nobu09
2
230
CSS Linter による Baseline サポートの仕組み
ryo_manba
1
150
Featured
See All Featured
Making Projects Easy
brettharned
116
6k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
370
Embracing the Ebb and Flow
colly
84
4.6k
We Have a Design System, Now What?
morganepeng
51
7.4k
Six Lessons from altMBA
skipperchong
27
3.6k
For a Future-Friendly Web
brad_frost
176
9.6k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.1k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Bootstrapping a Software Product
garrettdimon
PRO
306
110k
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
‧ӹ,:*:‧\(ӟ)/‧:*‧°ӹ*