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
90
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
72
Let’s fix extras in Core Metadata 3.0
uranusjr
0
370
Python Packaging: Why Don’t You Just…?
uranusjr
1
200
這樣的開發環境沒問題嗎?
uranusjr
9
2.4k
Django After Web 2.0
uranusjr
3
1.9k
We Store Cheese in A Warehouse
uranusjr
1
400
The Python You Don’t Know
uranusjr
17
2.9k
Python and Asynchrony
uranusjr
0
330
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
Goのmultiple errorsについて (2024年4月版)
syumai
4
910
GitHub Actionsで泣かないためにやっておきたい設定 / Recommended GHA settings to avoid crying
pinkumohikan
3
540
Polars入門
daikikatsuragawa
1
100
Random\Randomizer クラスで日常のあれこれを解決しよう! / Random\Randomizer class solves familiar trouble
cocoeyes02
0
250
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
650
コーンフレークから始める モデリング会話入門
ogurotakayuki
0
370
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
27
8.3k
Apache Hive 4 on Treasure Data
ryukobayashi
0
330
2 週間で Twitter Bot を作ってみた
contour_gara
0
510
VSCodeでのDatabricks開発もお勧めしたい/I would also recommend Databricks development with VSCode.
kazumain
0
260
大規模UIKitベースアプリへのTCAの段階的導入/gradual-adoption-of-tca-in-a-large-scale-uikit-based-app
takehilo
1
180
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
260
Featured
See All Featured
Building Adaptive Systems
keathley
31
1.9k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
116
18k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.6k
Designing Experiences People Love
moore
136
23k
Building a Modern Day E-commerce SEO Strategy
aleyda
17
6.4k
Building an army of robots
kneath
300
41k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
9
8.3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
Automating Front-end Workflow
addyosmani
1356
200k
Fantastic passwords and where to find them - at NoRuKo
philnash
37
2.5k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
221
21k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
78
43k
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
‧ӹ,:*:‧\(ӟ)/‧:*‧°ӹ*