Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
95
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
470
Python Packaging: Why Don’t You Just…?
uranusjr
1
210
這樣的開發環境沒問題嗎?
uranusjr
9
2.5k
Django After Web 2.0
uranusjr
3
2k
We Store Cheese in A Warehouse
uranusjr
1
440
The Python You Don’t Know
uranusjr
17
3k
Python and Asynchrony
uranusjr
0
350
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
as(型アサーション)を書く前にできること
marokanatani
10
3k
Djangoの開発環境で工夫したこと - pre-commit / DevContainer
hiroki_yod
1
550
Gestaltung digitaler Lösungen – Produktions- oder Designprozess?
techstories
0
120
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
340
romajip: 日本の住所CSVデータを活用した英語住所変換ライブラリを作った話
sangunkang
0
2.3k
Discord Bot with AI -for English learners-
xin9le
0
110
Java 23の概要とJava Web Frameworkの現状 / Java 23 and Java web framework
kishida
2
370
eBPF Deep Dive: Architecture and Safety Mechanisms
takehaya
12
1.1k
MoQとか勉強会#2 発表資料
yuki_uchida
1
120
我々のデザインシステムは Chakra v3 にアップデートします
shunya078
2
1.5k
Leverage LLMs in Java with LangChain4j and Quarkus
hollycummins
0
110
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
8
3.4k
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
44
13k
Optimizing for Happiness
mojombo
376
70k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Documentation Writing (for coders)
carmenintech
65
4.5k
Building Your Own Lightsaber
phodgson
103
6.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Visualization
eitanlees
145
15k
Site-Speed That Sticks
csswizardry
0
88
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
150
Teambox: Starting and Learning
jrom
133
8.8k
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
‧ӹ,:*:‧\(ӟ)/‧:*‧°ӹ*