SlideShare a Scribd company logo
1 of 356
Download to read offline
Python 
ධ㛛 
Cisco 
Systems, 
Japan 
TAC 
ఀ⸨ 
⿱୍ 
(twi7er: 
@yuichi110)
ᮏ䝇䝷䜲䝗䛿 
Cisco 
Japan 
♫ෆ䛷㛤ദ䛧䛯 
TAC 
(䝖䝷䝤䝹 
䝅䝳䞊䝔䜱䞁䜾䜢ᑓ㛛䛸䛩䜛䝛䝑䝖䝽䞊䜽䜶䞁䝆䝙䜰) 
ྥ 
䛡䛾㻌python 
䝖䝺䞊䝙䞁䜾䜢୍㒊ಟṇ䛧䛯䜒䛾䛷䛩䚹 
₇⩦㎸䜏䛷㻌4᫬㛫 
x 
4᪥㻌䛷㛤ദ䛧䛶䛔䜎䛩
䝖䝺䞊䝙䞁䜾䛾┠ⓗ 
• Python 
䜢౑䛘䜛䜘䛖䛻䛺䜛 
• 䝥䝻䜾䝷䝭䞁䜾䛾ᴫᛕ䜢⌮ゎ 
• 䝋䞊䝇䝁䞊䝗(diff䛺䛹)䜢ㄞ䜐 
• SDN㛵㐃䛾SR䜢䛥䜀䛟 
• python䛷ゎᯒ䝒䞊䝹䜢స䛳䛶䝏䞊䝮䛻㈉⊩ 
Python 
ධ㛛 
3
䝖䝺䞊䝙䞁䜾䛾ᑐ㇟⪅ 
• Windows䛺䜚Mac䛺䜚䜢౑䛔䛣䛺䛫䜛䝴䞊䝄 
• 䝥䝻䜾䝷䝭䞁䜾䛻⯆࿡䛜䛒䜛 
• 䝖䝺䞊䝙䞁䜾䛾᫬㛫 
+ 
⮬⩦᫬㛫䜢ᣢ䛶䜛 
• 䝛䝑䝖䝽䞊䜽䜶䞁䝆䝙䜰 
Python 
ධ㛛 
4
⿵㊊ 
䝖䝺䞊䝙䞁䜾䛾ෆᐜ䛿 
Alan 
Gauld 
䛥䜣䛾 
Learning 
to 
proguram 
ver 
2 
䜢ཧ↷䛧䛶సᡂ䚹 
䛾䛂ึ䜑䛶䛾Python䛃䜒ཧ↷䛧䛶 
Python 
ධ㛛 
5 
䜎䛯䚸O’REILLY 
䛔䜎䛩䚹 
Learning 
to 
Program 
h7p://www.alan-­‐g.me.uk/
䝥䝻䜾䝷䝭䞁䜾䛸䛿 
Python 
ධ㛛 
6
䝥䝻䜾䝷䝭䞁䜾䛸䛿 
• 㟁Ꮚᶵჾ䜢䛂⮬ศ䛜ពᅗ䛩䜛㏻䜚䛃䛻ື䛛䛩ᡭẁ 
• ไᚚᩥ䛷ฎ⌮䛾ὶ䜜䜢Ỵ䜑䜛 
• ྛฎ⌮䛿㐃⥆䛧䛯࿨௧䛷ᡂ䜚❧䛴 
Python 
ධ㛛 
7
Ping 
䛾ᣲື 
• ICMP 
echo 
䜢㻌cisco.com 
䛻3ᅇ㏦ಙ䛧䚸䛭䛾 
reply 
䛜ᒆ䛔䛯๭ྜ䛸RTT䜢⟬ฟ 
Python 
ධ㛛 
8 
yuichi$ 
ping 
-­‐c 
3 
cisco.com 
PING 
cisco.com 
(72.163.4.161): 
56 
data 
bytes 
64 
bytes 
from 
72.163.4.161: 
icmp_seq=0 
7l=238 
cme=149.949 
ms 
64 
bytes 
from 
72.163.4.161: 
icmp_seq=1 
7l=238 
cme=149.783 
ms 
64 
bytes 
from 
72.163.4.161: 
icmp_seq=2 
7l=238 
cme=149.826 
ms 
-­‐-­‐-­‐ 
cisco.com 
ping 
stacsccs 
-­‐-­‐-­‐ 
3 
packets 
transmi4ed, 
3 
packets 
received, 
0.0% 
packet 
loss 
round-­‐trip 
min/avg/max/stddev 
= 
149.783/149.853/149.949/0.070 
ms
Ping䛾ᐇ⿦ 
(1)ᐄඛ䜰䝗䝺䝇䜢ධຊ(cisco.com) 
(2)ᐄඛ䛻ྥ䛡䛶 
ICMP 
echo 
request 
䜢ᡴ䛴 
(3)⤖ᯝ䜢⾲♧ 
-­‐ 
ICMP 
echo 
reply 
䛜䛛䛘䛳䛶䛝䛯ሙྜ 
฿╔᫬้ 
-­‐ 
㏦ಙ᫬้㻌䛾 
RTT 
䜢⾲♧ 
-­‐ ICMP 
echo 
reply 
䛜ᖐ䛳䛶䛣䛺䛛䛳䛯ሙྜ 
lost 
䛧䛯䛣䛸䜢⾲♧ 
(4) 
(2)-­‐(3) 
䜢䛒䛸2ᅇ⧞䜚㏉䛩 
(5)3ᅇ䛾ICMP䛾⤖ᯝ䜢㞟ィ䛧䚸⾲♧ 
PC 
S 
D 
1ᅇ┠⾲♧ 
2ᅇ┠⾲♧ 
3ᅇ┠⾲♧ 
3ᅇ䛾⤖ᯝ䜢⾲♧ 
Python 
ධ㛛 
9 
echo 
request 
echo 
reply 
䝃䞊䝞 
ping 
䛾ᐇ⿦
䝥䝻䜾䝷䝮䜢⤌䜐䛸䛔䛖䛣䛸 
• 䝁䞊䝗䛻䜘䜛ᘓ⠏సᴗ 
• ⡆༢䛺䝰䝜䛺䜙ᑡ䛺䛔▱㆑䛷స䜜䜛 
• ᕧ኱䛺䝰䝜䛿㧗ᗘ䛺ᑓ㛛▱㆑䛜ᚲせ 
Python 
ධ㛛 
10 
䛸䜚䛒䛘䛪䛾┠ᶆᆅⅬ
PYTHON 
Python 
ධ㛛 
11
䝥䝻䜾䝷䝭䞁䜾ゝㄒ 
• 䝥䝻䜾䝷䝮䜢グ㏙䛩䜛䛯䜑䛾ゝㄒ 
• 䝁䞁䝢䝳䞊䝍(ᶵᲔㄒ)䛸ே㛫(⮬↛ゝㄒ)䛾௰௓ゝㄒ 
• 䛔䛟䛴䛛䛾✀㢮䛜䛒䜛 
Python 
ධ㛛 
12 
'hello 
world'䜢⾲♧䛥䛫䜛 
print('hello 
world') 
00100101001101010 
ே䛜䜔䜚䛯䛔䛣䛸 
䝥䝻䜾䝷䝭䞁䜾ゝㄒ 
ᶵ఍䛜ゎ㔘ྍ⬟䛺࿨௧
䛺䛬㻌Python 
? 
• ᩥἲ䛜䝅䞁䝥䝹䛷ึᚰ⪅ྥ䛡 
• 䝟䝽䝣䝹䛺ゝㄒ 
• 䜸䝤䝆䜵䜽䝖ᆺᣦྥゝㄒ 
Python 
ධ㛛 
13
Python 
௨እ䛾㑅ᢥ⫥ 
• C: 
䝝䞊䝗䛻㏆䛔ᴫᛕ䜢ព㆑䛩䜛ᚲせ䛜䛒䜛 
OS䛻㏆䛔䛣䛸䜢䛧䛺䛔䛸㛤Ⓨ䛜㠀ຠ⋡ 
• Java: 
Ⰻ䛔ゝㄒ䛰䛜䚸ぬ䛘䜛䛣䛸䛜ከ䛔 
Python 
䛾䜋䛖䛜⡆༢ 
• 䛭䛾௚Script⣔ゝㄒ: 
ciscoᶵჾ䛜㠀䝃䝫䞊䝖 
• Web⣔ゝㄒ: 
cisco 
䛷㔜せᗘ䛜ప䛔 
• 䛭䛾௚: 
୰⣭⪅-­‐> 
ୖ⣭⪅㻌䛻䛺䜚䛯䛔ேྥ䛡 
Python 
ධ㛛 
14
C, 
Java, 
Python 
䛾ẚ㍑ 
• ⌧ᅾ䛾䝕䜱䝺䜽䝖䝸௨ୗ䛾ᵓ㐀䜢᭩䛝ฟ䛩䝥䝻䜾䝷䝮 
Python 
ධ㛛 
[/Users/yuichi/Documents/_TRAINING] 
CCIE 
Java 
listFile.py 
python 
[/Users/yuichi/Documents/_TRAINING/CCIE] 
ccie-­‐lab.key 
[/Users/yuichi/Documents/_TRAINING/Java] 
Java 
Basic 
Training 
Day 
2.pptx 
Java 
Basic 
Training 
Day 
3.pptx 
Java 
Basic 
Training 
Day 
4.pptx 
Java 
Basic 
Training 
Day1.pptx 
[/Users/yuichi/Documents/_TRAINING/python] 
adfunc.py 
dict-­‐test.py 
excepcon.py 
func.py 
15 
_TRAINING 
CCIE 
Java 
Python 
ccie-­‐lab.key 
Java 
Basic 
Training 
Day2 
..... 
adfunc.py 
..... 
lismile.py 
䝥䝻䜾䝷䝮䛾ฟຊ
C 
ゝㄒ䛷䛾౛ 
Python 
ධ㛛 
16
Java䛷䛾౛ 
Python 
ධ㛛 
17
Python 
䛷䛾౛ 
Python 
ධ㛛 
18 
IDLE
ẚ㍑⤖ᯝ 
Python 
ධ㛛 
19 
• Python 
䛿⡆༢䛻䝥䝻䜾䝷䝮䛜᭩䛡䜛
Python 
ධ㛛 
20
Agenda 
• Day1: 
䜲䞁䝇䝖䞊䝹䚸ᇶᮏᩥἲ䚸䝕䞊䝍ᆺ 
• Day2: 
⥆䝕䞊䝍ᆺ䚸䝰䝆䝳䞊䝹䛸㛵ᩘ 
• Day3: 
䜸䝤䝆䜵䜽䝖ᣦྥ䚸䜰䝹䝂䝸䝈䝮 
• Day4: 
ṇつ⾲⌧䚸䜶䝷䞊ฎ⌮䚸䝕䞊䝍䝧䞊䝇 
Python 
ධ㛛 
21
Agenda 
Day 
1 
• Python 
䛾䜲䞁䝇䝖䞊䝹 
• 䝥䝻䞁䝥䝖䛸IDLE䛾౑䛔᪉ 
• ᆺ䛸ኚᩘ 
• ᮲௳ศᒱ 
• 䝹䞊䝥 
• ᶆ‽ධຊ 
• 㛵ᩘ䛸䝰䝆䝳䞊䝹䛾฼⏝ 
• ₇⩦ 
Python 
ධ㛛 
22
PYTHON 
䛾䜲䞁䝇䝖䞊䝹 
Python 
ධ㛛 
23
Python 
Installacon 
• Mac䛿䝍䞊䝭䝘䝹䛷᭱ึ䛛䜙౑䛘䜛 
• 㛤Ⓨ⎔ቃ䜢స䜛䛯䜑䛻䝞䜲䝘䝸䛛䜙䜲䞁䝇䝖䞊䝹 
• 䝞䞊䝆䝵䞁䛿 
2.7 
⣔䛾᭱᪂∧ 
Python 
බᘧ䝨䞊䝆 
Python 
ධ㛛 
24
Python 
䛾䝞䞊䝆䝵䞁 
• Version 
3.3.x: 
᭱᪂∧䛾䝞䞊䝆䝵䞁 
• Version 
2.7.x: 
䜎䛰䜎䛰୺ὶ䛾䝞䞊䝆䝵䞁 
• Cisco: 
2.7.x 
䜢฼⏝ 
Python 
ධ㛛 
25
Windows 
䜈䛾䜲䞁䝇䝖䞊䝹 
Python 
ධ㛛 
26
Mac 
䜈䛾䜲䞁䝇䝖䞊䝹 
Python 
ධ㛛 
27
PROMPT 
䛸 
IDLE 
䛾฼⏝ 
Python 
ධ㛛 
28
Prompt 
䛷 
python 
䜢࿧䜃ฟ䛩 
• Windows: 
Start 
-­‐> 
python2.7 
(command 
line) 
• Mac: 
terminal 
䛷 
python2.7 
䛸ᡴ䛴 
Python 
ධ㛛 
29
Prompt 
䛷 
python 
䜢࿧䜃ฟ䛩 
• Windows: 
Start 
-­‐> 
python2.7 
(command 
line) 
• Mac: 
terminal 
䛷 
python2.7 
䛸ᡴ䛴 
Python 
ධ㛛 
30
Prompt 
䛾฼⏝ 
• Prompt 
䛷௨ୗ䛾䜘䛖䛺䝁䝬䞁䝗䜢Ⓨ⾜䛧䛶䜏䜛 
Python 
ධ㛛 
31 
print('hello 
world') 
6+1*3 
(6+1)*3 
5/2 
5/2.0 
5/0 
print("{} 
+ 
{} 
= 
{}".format(2,3,5)) 
print 
"""hello 
I 
love 
Cisco 
I 
love 
Python 
too 
""" 
import 
sys 
sys.exit()
䝁䝬䞁䝗䛷 
script 
䝣䜯䜲䝹䜢ᐇ⾜ 
• 䝣䜯䜲䝹䛻ฎ⌮䜢グ㏙䛧䚸䛭䜜䜢䝁䝬䞁䝗䛷ᐇ⾜ 
• Python 
䛾䝇䜽䝸䝥䝖䝣䜯䜲䝹䛿ᣑᙇᏊ䛜 
.py 
• Windows 
䛿ᚲせ䛒䜜䜀python 
䜢䝟䝇ᣦᐃ䛩䜛 
Python 
ධ㛛 
32 
print 
“hello 
Cisco” 
print 
“hello 
Python” 
print 
“done!” 
test.py 
Mac 
䛾ᐇ⾜⏬㠃 
$ 
python 
䝣䜯䜲䝹ྡ
䝁䝬䞁䝗䛷 
script 
䝣䜯䜲䝹䜢ᐇ⾜ 
• 䝣䜯䜲䝹䛻ฎ⌮䜢グ㏙䛧䚸䛭䜜䜢䝁䝬䞁䝗ᐇ⾜ 
• Python 
䛾䝇䜽䝸䝥䝖䝣䜯䜲䝹䛿ᣑᙇᏊ䛜 
.py 
• Windows 
䛿ᚲせ䛒䜜䜀python 
䜢䝟䝇ᣦᐃ䛩䜛 
䈜 
᫬㛫䛾䛒䜛䛸䛝䛻䝟䝇䛾タᐃ䜢ຍ䛘䛶䛚䛟䛸ᴦ 
Python 
ධ㛛 
33 
Windows 
䛾ᐇ⾜⏬㠃
IDLE 
• 䛣䛾䝖䝺䞊䝙䞁䜾䛾䝯䜲䞁䛾㛤Ⓨ⎔ቃ 
• Windows: 
startup 
-­‐> 
python 
-­‐> 
idle 
• Mac: 
applicacon 
-­‐> 
python 
-­‐> 
idle 
Python 
ධ㛛 
34 
IDLE 
䝥䝻䞁䝥䝖⏬㠃
IDLE 
䛾฼⏝ 
• IDLE 
䛾䜶䝕䜱䝍䜢౑䛳䛯㛤Ⓨ 
• 䝯䝙䝳䞊䜘䜚ඛ䜋䛹స䛳䛯㻌test.py 
䜢㛤䛟 
• 䜶䝕䜱䝍⏬㠃䜢㑅ᢥ䛧䚸F5 
䜢ᢲ䛩䛸ᐇ⾜ 
Python 
ධ㛛 
35
䝁䝯䞁䝖䜰䜴䝖 
• 䝁䝯䞁䝖: 
䝥䝻䜾䝷䝮䛾ὀ㔘䚹ฎ⌮⣔䛻↓ど䛥䜜䜛 
• ୍⾜䛾䝁䝯䞁䝖䜰䜴䝖: 
# 
䝁䝯䞁䝖ᩥ 
• 」ᩘᴗ䛾䝁䝯䞁䝖䜰䜴䝖: 
""" 
」ᩘ⾜ᣦᐃྍ⬟ 
""" 
Python 
ධ㛛 
36
ᆺ䛸ኚᩘ 
Python 
ධ㛛 
37
ᆺ 
• 䝕䞊䝍䛻䛿ᆺ(✀㢮)䛜䛒䜛 
• ᩚᩘᆺ(Integer): 
1,5,-­‐1 
• ᩥᏐิᆺ(String): 
"Cisco", 
"Python" 
Python 
ධ㛛 
38
ᆺ䛸ไᚚ 
• ᆺ䛸䝥䝻䜾䝷䝮䛾ไᚚ䛻䛿ᐦ᥋䛺㛵ಀ䛜䛒䜛 
– ᩥᏐิ䜢ᩘᏐ䛷๭䜛䛣䛸䛿䛷䛝䛺䛔 
– ᩥᏐิ䛸ᩘᏐ䜢⤖ྜ䛷䛝䛺䛔 
• 䜘䛟䛒䜛ฎ⌮䛿ᡭ⥆䛝᪉ἲ䜢ぬ䛘䜛 
– ᩥᏐิ䛸ᩘᏐ䛾⤖ྜ䛿䚸ᩘᏐ䜢ᩥᏐิ䛻ኚ᥮䛧 
䛶䛛䜙⤖ྜ䛩䜛 
Python 
ධ㛛 
39
ኚᩘ 
• ኚᩘ: 
Data䜢ಖᏑ䛩䜛ᐜჾ 
Python 
ධ㛛 
40 
5 
print(5) 
=> 
5 
x 
= 
5 
print(x) 
=> 
5 
5 
ኚᩘ 
X 
䝕䞊䝍(ᐃᩘ 
5) 
௦ධ 
print 
( 
5 
) 
print 
( 
5 
)
ኚᩘ䛸ᆺ 
• ᆺᐉゝ: 
䛣䛾ኚᩘ䛿䛣䛾ᆺ䜢᱁⣡䛩䜛䛸䛔䛖ᐉゝ 
• Python 
䛾ኚᩘ䛻䛿ᆺᐉゝ䛜䛺䛔 
• ኚᩘ䛻䛹䛾䜘䛖䛺ᆺ䛷䜒䛔䜜䜙䜜䜛 
Python 
ධ㛛 
41 
int 
x 
= 
5; 
x 
= 
"Java" 
//Error 
x 
= 
5; 
print(x) 
x 
= 
"Python" 
# 
OK 
Java 
print(x) 
Python
Python 
䛾ᆺ 
• ⤌䜏㎸䜏ᆺ 
– ᩘᏐ: 
int, 
float 
䛺䛹 
– ᩥᏐิ: 
ⱥㄒ䚸᪥ᮏㄒ 
– 䝸䝇䝖: 
㓄ิ䛻ఝ䛶䛔䜛 
– Bool: 
True 
or 
False 
– 㛵ᩘ: 
Python 
䛷䛿㛵ᩘ䜒ᆺ䛾୍䛴 
– 䛭䛾௚ 
• 䝴䞊䝄䛜ᐃ⩏䛧䛯ᆺ(Class) 
Python 
ධ㛛 
42
ᩘᏐ 
• ᩚᩘ(int)䛸ᐇᩘ(float)䚸」⣲ᩘ䛜䛒䜛 
• ୖ㝈䛿Ẽ䛻䛧䛺䛟䛶䜘䛔 
Python 
ධ㛛 
>>> 
1234567 
* 
3456789 
4267637625363 
(42඙) 
43 
౑䛘䜛₇⟬Ꮚ 
ㄝ᫂ 
M 
+ 
N 
㊊䛧⟬ 
M 
– 
N 
ᘬ䛝⟬ 
M 
* 
N 
䛛䛡⟬ 
M 
/ 
N 
๭䜚⟬ 
M 
% 
N 
๫వ(䛒䜎䜚) 
M**N 
䜉䛝஌ 
(M 
* 
M 
* 
M 
… 
䜢Nᅇ)
ᩘᏐ 
• 䜲䞁䜽䝸䝯䞁䝖䛿౑䛘䛺䛔 
• ௦ධ₇⟬Ꮚ䛷௦⏝ 
Python 
ධ㛛 
44 
₇⟬Ꮚ 
ㄝ᫂ 
M 
+= 
N 
M 
= 
M 
+ 
N 
M 
-­‐= 
N 
M 
= 
M 
– 
N 
M 
*= 
N 
M 
= 
M 
* 
N 
M 
/= 
N 
M 
= 
M 
/ 
N 
M 
%= 
N 
M 
= 
M 
% 
N 
Java 
int 
i 
= 
0 
i++ 
Python 
i 
= 
0 
i 
+= 
1 
ß 
i 
= 
i+1
ᩥᏐิᆺ 
• 3✀㢮䛾グ㏙᪉ᘧ 
Python 
ධ㛛 
45 
䝅䞁䜾䝹䜽䜸䝔䞊䝅䝵䞁 
string 
= 
‘ 
Hello 
Python’ 
䝎䝤䝹䜽䜸䝔䞊䝅䝵䞁 
string 
= 
“Hello 
Python” 
䝖䝸䝥䝹䞉䝎䝤䝹䜽䜸䝔䞊䝅䝵䞁 
string 
= 
“””Hello 
World 
Hello 
Python 
# 
this 
isn’t 
comment 
Hello 
CIsco””” 
ᨵ⾜䜔≉Ṧグྕ䜒 
䛭䛾䜎䜎ᩥᏐิ䛻䚹
ᩥᏐิᆺ 
• ᩥᏐิ䜒₇⟬グྕ䜢౑䛘䜛 
Python 
ධ㛛 
46 
print 
“I 
love 
“ 
+ 
“python” 
=> 
“I 
love 
Python” 
print 
“hello 
“ 
* 
3 
=> 
“hello 
hello 
hello 
“ 
print 
“I 
say 
“ 
+ 
(“hello 
“ 
* 
3) 
=> 
“I 
say 
hello 
hello 
hello” 
x 
= 
“I 
love 
“ 
y 
= 
“Python” 
print(x+y) 
=> 
"I 
love 
Python"
Boolᆺ 
• 䛔䜟䜖䜛 
True, 
False 
䜢ᣢ䛴ᆺ 
• If 
ᩥ䛺䛹䛾ไᚚศ䛷䜘䛟฼⏝䛥䜜䜛 
Python 
ධ㛛 
47 
₇⟬Ꮚ 
ព࿡ 
A 
and 
B 
A 
䜒 
B 
䜒 
True 
䛺䜙 
True 
A 
or 
B 
A 
䛛 
B 
䛜 
True 
䛺䜙 
True 
A 
== 
B 
A 
䛸 
B 
䛜ྠ䛨䛺䜙 
True 
A 
!= 
B 
A 
䛸 
B 
䛜㐪䛘䜀 
True 
A 
<> 
B 
ྠୖ 
not 
A 
A 
䛜 
False 
䛺䜙 
True
Boolᆺ 
• 䛔䜟䜖䜛 
True, 
False 
䜢ᣢ䛴ᆺ 
• If 
ᩥ䛺䛹䛾ไᚚศ䛷䜘䛟฼⏝䛥䜜䜛 
Python 
ධ㛛 
48 
₇⟬Ꮚ 
ព࿡ 
A 
> 
B 
A 
䛜 
B 
䜘䜚኱䛝䛡䜜䜀 
True 
A 
>= 
B 
A 
䛜 
B 
௨ୖ䛺䜙 
True 
A 
< 
B 
A 
䛜 
B 
䜘䜚ᑠ䛥䛡䜜䜀 
True 
A 
<= 
B 
A 
䛜 
B 
௨ୗ䛺䜙 
True
Listᆺ 
• ᣑᙇ䛜䛷䛝䜛㓄ิ 
[x,y,z] 
䛸䛔䛖ᙧᘧ䛷సᡂ 
• ₇⟬Ꮚ 
[x] 
䛷䚸㓄ิ䛾 
x 
␒┠䛻䜰䜽䝉䝇䛩䜛 
• ఱ␒┠䛛(index)䛾ᣦᐃ䛿0䛛䜙ᩘ䛘䜛 
Python 
ධ㛛 
49 
list1 
= 
[3,4,5] 
print(list1) 
=> 
[3, 
4, 
5] 
list2 
= 
[“I”, 
“Love”, 
“Python”] 
print(list2[2]) 
=> 
“Python” 
list3 
= 
[[1,2], 
['A',”B”]] 
print(list3) 
=> 
[[1, 
2], 
[“A”, 
“B”]] 
print(list3[1][1]) 
=> 
“B” 
print(list3[5][0]) 
=> 
# 
index 
out 
of 
range 
䛾 
Error 
⾲♧
Listᆺ 
• 㓄ิ㛗䜢ྲྀᚓ: 
len(X) 
• 䜸䝤䝆䜵䜽䝖䛾 
index 
䜢ྲྀᚓ: 
index(X) 
• 㓄ิ䛻䜸䝤䝆䜵䜽䝖䜢㏣ຍ: 
append(x) 
• 㓄ิ䛛䜙䜸䝤䝆䜵䜽䝖䜢๐㝖: 
del 
Python 
ධ㛛 
50 
list1 
= 
[1,2,3,4,5] 
print(list1.index(3)) 
=> 
2 
print(len(list1)) 
=> 
5 
list1.append(6) 
print(list) 
=> 
[1,2,3,4,5,6] 
del 
list2[1] 
print(list2) 
=> 
[1,3,4,5,6]
䛭䛾௚䛾ᆺ(ᚋ᪥) 
• Tuple 
• Dicconary 
• File 
• Dates, 
Times 
• 䝴䞊䝄ᐃ⩏䛾ᆺ(䜽䝷䝇) 
• 㛵ᩘ 
Python 
ධ㛛 
51
₇⩦1 
Python 
ධ㛛 
52
₇⩦ 
(₇⟬) 
• ௨ୗ䛾ᅗᙧ䛾㠃✚䜢䝥䝻䜾䝷䝮䛷ィ⟬䛧䛶䛟䛰䛥 
䛔 
• వ⿱䛜䛒䜜䜀䚸import 
௨እ䛿୍⾜(䝽䞁䝷䜲䝘 
䞊)䛸䛧䛶䛟䛰䛥䛔 
Python 
ධ㛛 
53 
4cm 
3cm 
10cm 
5cm 
2cm 
8cm 
༙ᚄ 
2cm 
䈜 
෇࿘⋡䛿 
math 
module 
䛾 
π 
䜢 
౑䛳䛶䛟䛰䛥䛔 
>>> 
import 
math 
>>> 
math.pi 
3.141592653589793
₇⩦ 
(䝗䜻䝳䝯䞁䝖) 
• ᩘᏐ 
1984 
䛸ᩥᏐิ 
“cisco” 
䜢ᩥᏐิ⤖ྜ䛧䚸⾲ 
♧䛧䛶䛟䛰䛥䛔 
(䛭䛾䜎䜎⤖ྜ䛩䜛䛸䜶䝷䞊䛻䛺䜛 
䛾䛷䚸ᩘᏐ䜢ᩥᏐิ䛻ኚ᥮䛩䜛ᚲせ䛒䜚) 
• 䜾䜾䜛䛾䛷䛿䛺䛟䚸௨ୗ䛾䝨䞊䝆䛛䜙ᚲせ䛺䛂⤌ 
䜏㎸䜏㛵ᩘ䛃䜢᥈䛧䛶䛟䛰䛥䛔 
Python 
ධ㛛 
54 
Python 
ᶆ‽䝷䜲䝤䝷䝸 
h7p://docs.python.jp/2/library/index.html 
(ⱥ) 
h7p://docs.python.org/2.7/library/index.html#python
ไᚚᵓ㐀 
Python 
ධ㛛 
55
ไᚚᵓ㐀 
• ⡆༢䛺䝞䝑䝏ฎ⌮: 
ୖ䛛䜙ୗ䛻୍⾜䛪䛴ᐇ⾜䛧䛶䛔䛟 
• 䝥䝻䜾䝷䝭䞁䜾 
– ୖ䛛䜙ୗ䛻ᐇ⾜䛧䛶䛔䛟 
– Ỵ䜑䜙䜜䛯ᩥἲ䛸༢ㄒ(ண⣙ㄒ)䛷ฎ⌮᪉ἲ䜢グ㏙ 
– ifᩥ䚸loopᩥ䛺䛹䛷䝥䝻䜾䝷䝮䛾ὶ䜜䜢ไᚚ䛩䜛 
– 䜽䝷䝇䜔㛵ᩘ䛺䛹䜢౑䛳䛶ฎ⌮䜢䜎䛸䜑䜛 
Python 
ධ㛛 
56
䝥䝻䜾䝷䝮䛾ฎ⌮䛾ὶ䜜 
• 䝥䝻䜾䝷䝮䛾ᇶᮏⓗ䛺ὶ䜜䛿ୖ䛛䜙ୗ䜈 
Python 
ධ㛛 
57 
Step1 
Step2 
Step3
ண⣙ㄒ 
• ✵ⓑ䛷༊ษ䜙䜜䛯༢ㄒ䛾㐃⥆䛷䝥䝻䜾䝷䝮䜢グ㏙䛩䜛 
• ண⣙ㄒ: 
䝥䝻䜾䝷䝮䛾ไᚚ䛾䛯䜑䛾䛂≉ู䛃䛺༢ㄒ 
• ண⣙ㄒ௨እ: 
ᐃᩘ䚸ኚᩘ䚸㛵ᩘ䚸䜽䝷䝇ྡ䛺䛹⮬⏤䛻䛴 
䛡䜙䜜䜛ྡ๓ 
Python 
ධ㛛 
58 
䝥䝻䜾䝷䝮䛾ไᚚ 
if, 
for, 
while 
ᐉゝ 
class, 
def 
ண⣙ㄒ䛾౛
䝁䞊䝗䝤䝻䝑䜽䛸䜲䞁䝕䞁䝖 
• C,Java䛺䛹: 
{} 
䛸䛔䛖グྕ䛷ฎ⌮䜢䜎䛸䜑䜛 
• Python: 
䜲䞁䝕䞁䝖(Ꮠୗ䛢)䛷ฎ⌮䜢䜎䛸䜑䜛 
Python 
ධ㛛 
59 
for(int 
i=0; 
i10; 
i++){ 
if(i%2 
== 
0){ 
System.out.println(i 
+ 
 
is 
even); 
}else{ 
System.out.println(i 
+ 
 
is 
odd); 
} 
} 
System.out.println(done); 
for 
i 
in 
range(10): 
if(i%2 
== 
0): 
print({} 
is 
even.format(i)) 
else: 
print({} 
is 
odd.format(i)) 
print(done) 
Java 
Python
᮲௳ศᒱ 
Python 
ධ㛛 
60
᮲௳ศᒱ 
IF 
• ≉ᐃ䛾᮲௳䜢‶䛯䛩ሙྜ䛾䜏ฎ⌮䜢⾜䛖 
Python 
ධ㛛 
61 
Step1 
Test 
Condicon 
Path1 
Path2
᮲௳ศᒱ 
IF 
• ≉ᐃ䛾᮲௳䜢‶䛯䛩ሙྜ䛾䜏ฎ⌮䜢⾜䛖 
Python 
ධ㛛 
62 
if(᮲௳A): 
᮲௳A䛜True䛺䜙ᐇ⾜ 
…. 
elif(᮲௳B): 
᮲௳A䛜False䛛䛴᮲௳B䛜True䛺䜙ᐇ⾜ 
…. 
else: 
᮲௳A䜒B䜒False䛺䜙ᐇ⾜ 
…. 
if 
䛾᮲௳ศᒱ䛾⤖ᯝ䛻㛵䜟䜙䛪ᐇ⾜ 
….
᮲௳ศᒱ 
IF 
• Python 
䛾䝁䞊䝗 
Python 
ධ㛛 
63 
i 
= 
5 
if 
(i 
 
10): 
print 
“This 
is 
never 
printed” 
else: 
print 
“This 
might 
be 
printed”
(⿵㊊)᮲௳ศᒱ 
SWITCH-­‐CASE 
• ᐇ⿦䛥䜜䛶䛔䛺䛔䚹Ifᩥ䛷స䜛 
Python 
ධ㛛 
64 
switch(᮲௳){ 
case 
A: 
᮲௳䛜A䛾ሙྜ䛾ฎ⌮ 
break; 
case 
B: 
᮲௳䛜B䛾ሙྜ䛾ฎ⌮ 
break 
default: 
䛭䜜௨እ䛾ሙྜ䛾ฎ⌮ 
} 
if(᮲௳ 
== 
A): 
᮲௳䛜A䛾ሙྜ䛾ฎ⌮ 
elif(᮲௳ 
== 
B): 
᮲௳䛜B䛾ሙྜ䛾ฎ⌮ 
else: 
䛭䜜௨እ䛾ሙྜ䛾ฎ⌮ 
Java 
Python
䝹䞊䝥ฎ⌮ 
Python 
ධ㛛 
65
䝹䞊䝥ฎ⌮ 
for 
• ≉ᐃ䛾ฎ⌮䜢Ỵ䜑䜙䜜䛯ᅇᩘ䛣䛺䛩 
• Python䛷䛿䝸䝇䝖䛾㉮ᰝ䛻౑⏝ 
Python 
ධ㛛 
66 
Repeated 
Steps 
Test 
Condicon
䝸䝇䝖䛸for 
• C䜔Java䛺䛹䛾for䛸䛿ู≀ 
• C䜔Java䛸ྠ䛨䜘䛖䛺౑䛔᪉䜒䛷䛝䜛 
Python 
ධ㛛 
67 
䝸䝇䝖 
A 
B 
C 
D 
(1) 
A䜢౑䛳䛶ฎ⌮䜢ᐇ⾜ 
(2) 
B䜢౑䛳䛶ฎ⌮䜢ᐇ⾜ 
(3) 
C䜢౑䛳䛶ฎ⌮䜢ᐇ⾜ 
(4) 
D䜢౑䛳䛶ฎ⌮䜢ᐇ⾜
forᩥ䛾᭩ᘧ 
Python 
ධ㛛 
68 
for 
ኚᩘ 
in 
䝸䝇䝖: 
ᐇ⾜䛩䜛ฎ⌮㻌 
䝸䝇䝖䛾0␒┠䜢ኚᩘ䛻௦ධ䛧䛶ฎ⌮䜢ᐇ⾜ 
䝸䝇䝖䛾1␒┠䜢ኚᩘ䛻௦ධ䛧䛶ฎ⌮䜢ᐇ⾜ 
..... 
string 
= 
 
for 
char 
in 
[H,e,l,l,o]: 
string 
+= 
char 
print(string) 
 
Hello
for䛻䜘䜛୍ᐃᅇᩘ䛾ฎ⌮ 
• ୍ᐃᅇᩘ䛾ฎ⌮䛻䛿 
range 
㛵ᩘ䜢ే⏝ 
• range(x): 
0 
䛛䜙 
x-­‐1 
䜎䛷䛾㐃␒䛾䝸䝇䝖䜢㏉䛩 
• range(x,y): 
x 
䛛䜙 
y-­‐1 
䜎䛷䛾㐃␒䛾䝸䝇䝖䜢㏉䛩 
Python 
ධ㛛 
69 
for(int 
i=0; 
i10; 
i++){ 
System.out.println(i) 
} 
for 
i 
in 
range(10): 
print(i) 
Java 
Python
(⿵㊊)䜲䝔䝺䞊䝍 
• python 
䛾㻌for 
䛿 
iterator 
䛸࿧䜀䜜䜛௙⤌䜏 
• 䝍䝥䝹䜔䚸ᩥᏐิ䚸㎡᭩䛺䛹䜒 
key 
䛸䛧䛶౑䛘䜛 
Python 
ධ㛛 
70 
for 
char 
in 
hello: 
print(char) 
= 
h 
e 
l 
l 
o
䝹䞊䝥ฎ⌮ 
while 
• ≉ᐃ᮲௳䜢‶䛯䛩䜎䛷䝹䞊䝥䜢⧞䜚㏉䛩 
• 䛂ࠥ 
䛾㛫䛿䛪䛳䛸䝹䞊䝥䛃䛸䛔䛖䜘䛖䛺䜲䝯䞊䝆 
• For 
䜋䛹䛿౑䜟䜜䛺䛔 
Python 
ධ㛛 
71
while 
䛻㐺䛧䛯䝹䞊䝥ฎ⌮ 
• ఱ࿘䛩䜜䜀䛔䛔䛛ศ䛛䜙䛺䛔ฎ⌮䛻౑䛖 
• ↓㝈䝹䞊䝥䛻䛺䜙䛺䛔䜘䛖䛻ὀព䛜ᚲせ 
୚䛘䜙䜜䛯ᩘ䜢2㐍ᩘ䛷⾲⌧䛩䜛䛾䛻ᚲせ䛺᱆ᩘ䜢ồ䜑䜛 
Python 
ධ㛛 
72 
x 
= 
6789329 
i 
= 
1 
while(2**i 
 
x): 
i 
+= 
1 
print(2 
^ 
{} 
 
{}.format(i,x)) 
2 
^ 
23 
 
6789329
₇⩦2 
Python 
ධ㛛 
73
₇⩦ 
(᮲௳ศᒱ) 
• 1 
– 
100 
䜎䛷䛾അᩘ䛾್䜢ᣢ䛴 
List 
䜢సᡂ䛩䜛 
• 1 
– 
10000 
䜎䛷䛾⣲ᩘ䜢⾲♧䛩䜛䚹 
䚷䚷䚷㻌⣲ᩘ: 
1䛸⮬ศ௨እ䛾ᩚᩘ䛷๭䜜䛺䛔ᩚᩘ 
• Fizz 
buzz 
䜢 
100 
䜎䛷⾜䛖 
1-­‐100䜎䛷䜢⾲♧䚹 
3䛷๭䜚ษ䜜䜛᫬䛿 
fizz, 
5䛷๭䜚ษ䜜䜛᫬䛿 
buzz 
䛸⾲♧ 
3䛷䜒5䛷䜒๭䜚ษ䜜䜛᫬䛿 
fizz 
buzz 
䛸⾲♧䛩䜛 
Python 
ධ㛛 
74
㛵ᩘ䛸䝰䝆䝳䞊䝹䛾฼⏝ 
Python 
ධ㛛 
75
㛵ᩘ䛾࿧䜃ฟ䛧 
• ≉ᐃ䛾ฎ⌮䜢⾜䛖䛯䜑䛾࿧䜃ฟ䛧ཱྀ 
• 㛵ᩘྡ䛻ᘬᩘ䜢୚䛘䛶࿧䜃ฟ䛩 
• 㛵ᩘ䛿㏉䜚್䜢㏉䛩 
Python 
ධ㛛 
76 
㛵ᩘྡ(ᘬᩘ) 
㏉䜚್ 
 
length 
= 
len([1,2,3,4,5]) 
 
print(length) 
5
㛵ᩘ䛷」㞧䛺ฎ⌮䜢㞃䛩 
• 㛵ᩘ䜢౑䛖䛣䛸䛷䝁䞊䝗䛜䜟䛛䜚䜔䛩䛟䛺䜛 
Python 
ධ㛛 
77 
i 
= 
-­‐5 
if(i0): 
i 
= 
-­‐i 
print 
i 
= 
5 
䜁䛳䛸ぢ䚸ఱ䜢䜔䛳䛶 
䛔䜛䛛ศ䛛䜙䛺䛔 
i 
= 
-­‐5 
i 
= 
abs(i) 
print 
i 
= 
5 
㛵ᩘྡ䛷䛩䛠䛂⤯ᑐᩘ䜢 
ᚓ䜛䝥䝻䜾䝷䝮䛃䛸䜟䛛䜛
㛵ᩘ䛷䝁䞊䝗䛾㔜」䜢ῶ䜙䛩 
• 䝁䞊䝗䛾㔜」䜢䛺䛟䛧䚸ಖᏲᛶ䜢ྥୖ䛥䛫䜛 
Python 
ධ㛛 
78 
i 
= 
-­‐5 
j 
= 
8 
If(i0): 
i 
= 
-­‐i 
If(j0): 
j 
= 
-­‐j 
print(i) 
= 
5 
print(j) 
= 
8 
i 
= 
-­‐5 
j 
= 
8 
㛵ᩘ 
abs 
䛾ᐃ⩏ 
print(abs(i)) 
= 
5 
print(abs(j)) 
= 
8
䝰䝆䝳䞊䝹 
• 䝰䝆䝳䞊䝹䛿䝥䝻䜾䝷䝮䛾ᩚ⌮ᩚ㡻ᡭἲ 
Python 
ධ㛛 
79 
vs 
䝰䝆䝳䞊䝹䛜䛺䛔 
䝰䝆䝳䞊䝹䜢฼⏝
䝰䝆䝳䞊䝹䛻䜘䜛㝵ᒙ໬ 
• ᶵ⬟䛤䛸䛻䜘䜛䝥䝻䜾䝷䝮䛾ᩚ⌮ 
Python 
ධ㛛 
80 
math 
file 
system 
os 
⮬ศ䛜సᡂ 
⤌䜏㎸䜏 
(䝕䝣䜷䝹䝖䛷 
౑䛘䜛)
䝰䝆䝳䞊䝹䜈䛾䜰䜽䝉䝇 
• 䝰䝆䝳䞊䝹䜢฼⏝䛩䜛䛻䛿 
import 
䛩䜛ᚲせ䛜䛒䜛 
Python 
ධ㛛 
81 
 
random() 
Traceback 
(most 
recent 
call 
last): 
File 
“stdin”, 
line 
1, 
in 
module 
NameError: 
name 
'random' 
is 
not 
defined 
 
import 
random 
 
random.random() 
0.6019003331149728 
import 
䛧䛶䛔䛺䛔䛸 
฼⏝䛷䛝䛪䛻䜶䝷䞊 
䛂䝰䝆䝳䞊䝹ྡ.㛵ᩘ䛃 
䛷䝰䝆䝳䞊䝹䛾㛵ᩘ䜢฼⏝
㛵ᩘ䛸䝰䝆䝳䞊䝹䛾సᡂ 
• సᡂ䛾௙᪉䛺䛹䛻䛴䛔䛶䛿 
day2 
䛷ᢅ䛖 
Python 
ධ㛛 
82
ᶆ‽ධຊ 
Python 
ධ㛛 
83
ᶆ‽ධຊ 
• 䝴䞊䝄䛛䜙䛾ධຊ䜢ཷ䛡ྲྀ䜛 
䛸ධຊ䛥䜜䜛 
ධຊᚅ䛱䛜ゎ㝖䛥䜜䜛 
Python 
ධ㛛 
84 
input 
= 
raw_input() 
䝴䞊䝄䛛䜙䛾ධຊ䜢ᚅ䛴 
“hello” 
print(input) 
= 
“hello”
ᶆ‽ධຊ 
• ↓㝈 
echo 
䝥䝻䜾䝷䝮 
Python 
ධ㛛 
85 
while(True): 
print 
“please 
input:” 
input 
= 
raw_input() 
print 
input 
䈜䚷Ctr-­‐C 
䛷ᢤ䛡䜛㻌
䝁䝬䞁䝗䝷䜲䞁ᘬᩘ 
Python 
ධ㛛 
86
䝁䝬䞁䝗䝷䜲䞁ᘬᩘ 
• 䝇䜽䝸䝥䝖䜢䛂䝟䝷䝯䞊䝍䛃䛴䛝䛷㉳ື䛩䜛 
• 䝇䜽䝸䝥䝖ෆ䛻┤᥋᭩䛝㎸䜐䜘䜚⼥㏻ᛶ䛜䛒䜛 
Python 
ධ㛛 
87 
ping䛾䝥䝻䜾䝷䝮 
(1) 
ᐄඛ䜢ᘬᩘ䛷ཷ䛡ྲྀ䜛 
(1) 
ᐄඛ䜈㻌icmp 
echo 
request 
䜢㏦䜛 
(2) 
ᐄඛ䛛䜙㻌icmp 
echo 
reply 
䜢ཷ䛡ྲྀ䜛 
(3) 
ཷಙ⤖ᯝ䜢⾲♧ 
ping.py 
cisco.com 
python.org 
#python 
ping.py 
cisco.com 
#python 
ping.py 
python.org
sys.argv 
• 䝁䝬䞁䝗䝷䜲䞁ᘬᩘ䛻䜰䜽䝉䝇䛩䜛䛻䛿 
sys 
䝰䝆䝳䞊 
䝹䜢 
import 
䛩䜛 
• ᘬᩘ䛜argv䛻䝸䝇䝖䛸䛧䛶᱁⣡䛥䜜䛶䛔䜛 
Python 
ධ㛛 
88 
import 
sys 
print(-­‐-­‐-­‐-­‐ 
args 
-­‐-­‐-­‐-­‐) 
print(len(sys.argv)) 
print(sys.argv[0]) 
print(sys.argv[1]) 
print(sys.argv[2]) 
print(-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐) 
a 
= 
int(sys.argv[1]) 
b 
= 
int(sys.argv[2]) 
print({} 
+ 
{} 
= 
{}.format(a,b,a+b)) 
$ 
python 
test.py 
1 
5 
-­‐-­‐-­‐-­‐ 
args 
-­‐-­‐-­‐-­‐ 
3 
test.py 
1 
5 
-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐ 
1 
+ 
5 
= 
6 
test.py 
ฟຊ
₇⩦3 
Python 
ධ㛛 
89
₇⩦(ᶆ‽ධຊ) 
• 䝴䞊䝄䛜ධຊ䛧䛯ᩥᏐิ䛾ᩥᏐᩘ䜢⾲♧䛩 
䜛䝥䝻䜾䝷䝮䜢సᡂ 
• ධຊ䛿ఱᗘ䛷䜒ཷ䛡௜䛡䜛䛜䚸ᩥᏐิ 
exit 
䜢 
ཷ䛡ྲྀ䜛䛸⤊஢䛩䜛䛣䛸 
Python 
ධ㛛 
90
ḟᅇண࿌ 
• 㛵ᩘ䜢స䜛 
• 㧗ᗘ䛺ᆺ䜢౑䛖 
• 䝣䜯䜲䝹ฎ⌮ 
Python 
ධ㛛 
91 
• 䛷䛝䜛䜘䛖䛻䛺䜛䛣䛸 
– 䝔䜻䝇䝖ฎ⌮䛻䜘䜛䝻䜾䛾ゎᯒ 
– OS䜢᧯స䛩䜛䛯䜑䛾䝅䜵䝹䝇䜽䝸䝥䝖䛾సᡂ
Python 
ධ㛛 
92
Python 
ධ㛛 
Learning 
to 
Program 
For 
Cisco 
Network 
Engineers 
Day 
2/4 
Japan 
TAC 
DC 
Team. 
Yuichi 
Ito
Agenda 
Day 
2 
• Python 
• ⥆䝕䞊䝍ᆺ 
• 㛵ᩘ䛾సᡂ 
• 䝰䝆䝳䞊䝹䛾సᡂ 
• ₇⩦1(Map) 
• ᩥᏐิฎ⌮ 
• 䝣䜯䜲䝹ฎ⌮ 
• OS䝁䝬䞁䝗䛾฼⏝ 
• ₇⩦2(䝻䜾ゎᯒ䚸Linux䛾䝇䜽䝸䝥䝖) 
Python 
ධ㛛 
94
PYTHON䛾Ṕྐ 
Python 
ධ㛛 
95
Python 
• Ⓩሙ᫬ᮇ: 
1990ᖺ 
• タィ⪅: 
䜾䜲䝗䞉䞂䜯䞁䝻䝑䝃䝮 
• ᆺ௜䛡: 
ᙉ䛔ືⓗᆺ௜䛡 
Python 
ධ㛛 
96 
• 1.x: 
1994ᖺ 
• 2.x: 
2000ᖺ䚹䝯䝆䝱䞊ゝㄒ໬ 
• 3.x: 
2008ᖺ䚹ᚋ᪉஫᥮ᛶ䛺䛧
⥆䝕䞊䝍ᆺ 
Python 
ධ㛛 
97
Python 
䛾ᆺ 
• 䝴䞊䝄ᐃ⩏䛾ᆺ(Class) 
• ⤌䜏㎸䜏ᆺ(python䛜ᥦ౪) 
Python 
ධ㛛 
98 
ᩘ್ᆺ 
-­‐ 
ᩚᩘ 
-­‐ 
㛗〇ᩘ 
-­‐ 
ᾋືᑠᩘⅬ 
-­‐ 
」⣲ᩘ 
䝅䞊䜿䞁䝇ᆺ 
-­‐ 
ᩥᏐิ 
-­‐ 
䝸䝇䝖 
-­‐ 
䝍䝥䝹 
-­‐ 
xrangeᆺ 
䝬䝑䝥ᆺ 
-­‐ 
㎡᭩ 
䝣䜯䜲䝹ᆺ 
-­‐ 
䝣䜯䜲䝹䜸䝤䝆䜵䜽䝖
⏝ㄒ 
• 䜸䝤䝆䜵䜽䝖 
• 䜲䞁䝕䝑䜽䝇 
• 䜶䝺䝯䞁䝖 
• 䝸䝔䝷䝹 
• ௦ධ 
• ᘬᩘ 
• ㏉䜚್ 
Python 
ධ㛛 
99
㎡᭩ᆺ 
• Key 
䛸 
Value 
䛾䝨䜰䜢ಖᣢ䛩䜛䝕䞊䝍ᆺ 
• Key䛛䜙Value䜢ྲྀᚓ䛩䜛 
Dicconary 
Python 
ධ㛛 
㏣ຍ 
100 
Apple 
: 
Red 
Lemon 
: 
Yellow 
Grape 
: 
Purple 
Apple䛾Ⰽ䛿? 
Red 
!! 
X 
: 
Y 
ྲྀᚓ
㎡᭩䛾᧯స 
• ㎡᭩䛾ᙧᘧ: 
{key1:value1, 
key2:value2} 
• ್䛾ྲྀᚓ: 
㎡᭩䜸䝤䝆䜵䜽䝖[key] 
• ್䛾㏣ຍ(᭦᪂): 
㎡᭩䜸䝤䝆䜵䜽䝖[key] 
= 
value 
• ್䛾๐㝖: 
del 
㎡᭩䜸䝤䝆䜵䜽䝖[key] 
• key୍ぴ䛾ྲྀᚓ: 
㎡᭩䜸䝤䝆䜵䜽䝖.keys() 
• key䜢ᣢ䛳䛶䛔䜛䛛: 
㎡᭩䜸䝤䝆䜵䜽䝖.has_key(key) 
Python 
ධ㛛 
101
㎡᭩ᆺ䛾᧯స 
# 
㎡᭩䜸䝤䝆䜵䜽䝖䜢సᡂ 
 
d 
= 
{Apple:Red, 
Lemon:Yellow} 
# 
key 
䛛䜙 
Value 
䜢ྲྀᚓ 
 
d[Apple] 
'Red' 
# 
㎡᭩䜸䝤䝆䜵䜽䝖䛻せ⣲䜢㏣ຍ 
 
d[Grape] 
= 
Purple 
 
d 
{'Grape': 
'Purple', 
'Lemon': 
'Yellow', 
'Apple': 
'Red'} 
# 
㎡᭩䜸䝤䝆䜵䜽䝖䛾せ⣲䜢๐㝖 
 
del 
d[Apple] 
 
d 
{'Grape': 
'Purple', 
'Lemon': 
'Yellow'} 
Python 
ධ㛛 
102
㎡᭩䛸䝸䝇䝖䛾㐪䛔 
• 䝸䝇䝖ᆺ䛿⥺ᙧ䛻䝕䞊䝍ಖᣢ 
• ㎡᭩ᆺ䛿䝝䝑䝅䝳䜢฼⏝䛧䛶䝕䞊䝍ಖᣢ 
• せ⣲ᩘ䛜ቑ䛘䛯ሙྜ䛾᳨⣴䛜㧗㏿ 
Python 
ධ㛛 
103 
䝝䝑䝅䝳䛾௙⤌䜏
㎡᭩ᆺ䛾䝃䞁䝥䝹 
• Arp 
Table 
䜢 
List 
䛸㻌㎡᭩䛷ᐇ⿦(䜽䝷䝇䛺䛧) 
Python 
ධ㛛 
104 
ipList 
= 
[] 
macList 
= 
[] 
def 
registerMAC1(ip, 
mac): 
if 
ip 
in 
ipList: 
index 
= 
ipList.index(ip) 
macList[index] 
= 
mac 
else: 
ipList.append(ip) 
macList.append(mac) 
def 
getMAC1(ip): 
if 
ip 
in 
ipList: 
index 
= 
ipList.index(ip) 
return 
macList[index] 
else: 
return 
None 
arpTable 
= 
{} 
def 
registerMAC2(ip, 
mac): 
arpTable[ip] 
= 
mac 
def 
getMAC2(ip): 
return 
arpTable.get(ip) 
List䛻䜘䜛ᐇ⿦: 
᥈⣴䝁䝇䝖 
Order(N) 
㎡᭩䛻䜘䜛ᐇ⿦: 
᥈⣴䝁䝇䝖 
Order(1) 
List䛻䜘䜛ᐇ⿦ 
㎡᭩䛻䜘䜛ᐇ⿦
䝉䝑䝖 
• 㔜」䚸㡰ᗎ䛾䛺䛔」ᩘ䛾䝕䞊䝍䜢ಖᣢ䛩䜛ᆺ 
• Value 
䛾䛺䛔 
Map 
䛻㏆䛔 
Python 
ධ㛛 
105 
 
a 
= 
set([1,2,3,4,5,3,2]) 
 
print(a) 
set([1, 
2, 
3, 
4, 
5]) 
 
a 
= 
set(hello) 
 
b 
= 
set(world) 
 
print(a) 
set(['h', 
'e', 
'l', 
'o']) 
 
print(b) 
set(['d', 
'r', 
'o', 
'w', 
'l']) 
 
print(a 
 
b) 
set(['l', 
'o'])
䝉䝑䝖䛾᧯స 
• ึᮇ໬: 
set(䝅䞊䜿䞁䝇ᆺ䛾䜸䝤䝆䜵䜽䝖) 
• ㏣ຍ: 
set䜸䝤䝆䜵䜽䝖.add(value) 
• ๐㝖: 
set䜸䝤䝆䜵䜽䝖.discard(value) 
Python 
ධ㛛 
106
䝍䝥䝹ᆺ 
• ୙ྍኚ䛺䝕䞊䝍ᵓ㐀 
• せ⣲䛾ᩘ䛜Ỵ䜎䛳䛯䛂୍⤌䛾䝕䞊䝍䛃 
Python 
ධ㛛 
107 
# 
show 
logging 
Link 
down 
OSPF 
Neighbor 
XXX 
down 
Admin 
up 
Link 
up 
OSPF 
Neighbor 
XXX 
up 
.... 
⾜ᩘ(せ⣲ᩘ)䛿ఙ䜃䜛 
= 
Listᆺ 
♫ဨ᝟ሗ 
Yuichi 
Ito 
2011-­‐04-­‐01 
Tokyo 
TAC 
⾜ᩘ(せ⣲ᩘ)䛿ఙ䜃䛺䛔 
㡰୙ྠ 
= 
Tuple 
ᆺ
䝍䝥䝹ᆺ䛾᧯స 
• Tuple 
䛾సᡂ: 
tupleObject 
= 
(elem1, 
elem2,...) 
• せ⣲䛾ྲྀᚓ: 
item 
= 
tupleObject[index] 
• 」ᩘ䛾せ⣲䛾ྲྀᚓ: 
• せ⣲䛾᭦᪂: 
䛷䛝䛺䛔 
Python 
ධ㛛 
108
䝍䝥䝹ᆺ䛾᧯స 
# 
Tuple 
䛾సᡂ 
 
yuiito 
= 
(Yuichi, 
Ito, 
2011) 
 
yuiito 
('Yuichi', 
'Ito', 
2011) 
# 
せ⣲䛾ཧ↷ 
 
yuiito[1] 
'Ito' 
 
yuiito 
= 
(Yuichi, 
Ito, 
2011) 
 
(name, 
famName, 
year) 
= 
yuiito 
 
year 
2011 
# 
せ⣲䛾᭦᪂ 
 
yuiito[2] 
= 
2009 
Traceback 
(most 
recent 
call 
last): 
File 
stdin, 
line 
1, 
in 
module 
TypeError: 
'tuple' 
object 
does 
not 
support 
item 
assignment 
Python 
ධ㛛 
109
䝍䝥䝹ᆺ䝃䞁䝥䝹 
• 䝕䞊䝍ᵓ㐀䜢సᡂྍ⬟(䜽䝷䝇䛾⡆᫆∧) 
• ୙ྍኚ䛺䜸䝤䝆䜵䜽䝖 
Python 
ධ㛛 
110 
def 
getMinMax(numList): 
minNum 
= 
numList[0] 
maxNum 
= 
numList[0] 
for 
n 
in 
numList: 
if 
n 
 
minNum: 
minNum 
= 
n 
if 
n 
 
maxNum: 
maxNum 
= 
n 
return 
(minNum, 
maxNum)
None 
• ఱ䜒䛺䛔䛣䛸䜢᫂♧䛩䜛䛯䜑䛾≉Ṧ䛺ᆺ 
• C, 
Java 
䛾 
NULL 
䛸䛿㐪䛖䛜䚸䜋䜌ྠ䛨 
Python 
ධ㛛 
111
㛵ᩘ䛾ᴫᛕ 
Python 
ධ㛛 
112
㛵ᩘ 
• ≉ᐃ䛾ฎ⌮䜢⾜䛖ཷ௜❆ཱྀ 
• ⣽䛛䛔ฎ⌮䜢㞃ⶸ 
Python 
ධ㛛 
113 
䝥䝻䜾䝷䝮(䝟䞁䛾సᡂ㛵ᩘ) 
ᡂᯝ≀(䝟䞁) 
䛒䜣䜁䜣 
䜹䝺䞊䝟䞁
䛒䜣䜁䜣䛾〇㐀ᕤ⛬ 
Python 
ධ㛛 
114 
䛣䛽䜛 
ᮦᩱ䜢⏝ព 
⢊䛺䛹䜢䜎䛬䜛 
䛣䛽䜛 
Ⓨ㓝 
⏕ᆅ䜢ᩚᙧ 
ᨺ⨨ 
䛒䜣䜁䜣䛾ᙧస䜚 
⏕ᆅ䜢㐺㔞䛸䜛 
㤆䜢䛔䜜䜛 
୸䜑䜛 
↝䛟 
䜸䞊䝤䞁䜢 䜑䜛 
䝟䞁䜢䛔䜜䜛 
୍ᐃ᫬㛫ᚅ䛴 
ྲྀ䜚ฟ䛩
䜹䝺䞊䝟䞁䛾〇㐀ᕤ⛬ 
Python 
ධ㛛 
115 
䛣䛽䜛 
ᮦᩱ䜢⏝ព 
⢊䛺䛹䜢䜎䛬䜛 
䛣䛽䜛 
Ⓨ㓝 
⏕ᆅ䜢ᩚᙧ 
ᨺ⨨ 
䜹䝺䞊䝟䞁䛾ᙧస䜚 
⏕ᆅ䜢㐺㔞䛸䜛 
䜹䝺䞊䜢䛔䜜䜛 
୸䜑䜛 
ᥭ䛢䜛 
Ἔ䜢 䜑䜛 
䝟䞁䜢䛔䜜䜛 
୍ᐃ᫬㛫ᚅ䛴 
ྲྀ䜚ฟ䛩
㛵ᩘ䜢౑䜟䛺䛔䝧䝍᭩䛝 
• ฎ⌮䛾༊ษ䜚䛜ศ䛛䜚䛵䜙䛟䚸㔜」䛜ከ䛔 
Python 
ධ㛛 
116 
ᮦᩱ䜢⏝ព 
⢊䜢ΰ䛬䜛 
䛣䛽䜛 
⏕ᆅ䜢ᩚᙧ 
ᨺ⨨ 
⏕ᆅ䜢㐺㔞䛸䜛 
㤆䜢ධ䜜䜛 
ᩚᙧ 
䜸䞊䝤䞁䜢 䜑䜛 
䝟䞁䜢䛔䜜䜛 
୍ᐃ᫬㛫ᚅ䛴 
ྲྀ䜚ฟ䛩 
AnPan.py 
ᮦᩱ䜢⏝ព 
⢊䜢ΰ䛬䜛 
䛣䛽䜛 
⏕ᆅ䜢ᩚᙧ 
ᨺ⨨ 
⏕ᆅ䜢㐺㔞䛸䜛 
䜹䝺䞊䜢ධ䜜䜛 
ᩚᙧ 
Ἔ䜢 䜑䜛 
䝟䞁䜢䛔䜜䜛 
୍ᐃ᫬㛫ᚅ䛴 
ྲྀ䜚ฟ䛩 
CurryPan.py
㛵ᩘ䛻䜘䜛ฎ⌮༢఩䛾ᩚ⌮ 
Python 
ධ㛛 
117 
䛣䛽䜛 
Ⓨ㓝 
䛒䜣䜁䜣ᙧస䜚 
↝䛟 
䜹䝺䞊䝟䞁ᙧస䜚 
ᥭ䛢䜛 
㔜」䛾᤼㝖
㛵ᩘ䛻䜘䜛䝁䞊䝗䛾ᩚ⌮ 
Python 
ධ㛛 
118 
㛵ᩘ: 
䛣䛽䜛 
ᮦᩱ䜢⏝ព 
⢊䜢ΰ䛬䜛 
䛣䛽䜛 
㛵ᩘ: 
Ⓨ㓝 
⏕ᆅ䜢ᩚᙧ 
ᨺ⨨ 
㛵ᩘ: 
䛒䜣䜁䜣ᙧస䜚 
⏕ᆅ䜢㐺㔞䛸䜛 
㤆䜢ධ䜜䜛 
ᩚᙧ 
㛵ᩘ: 
↝䛟 
䜸䞊䝤䞁䜢 䜑䜛 
䝟䞁䜢䛔䜜䜛 
୍ᐃ᫬㛫ᚅ䛴 
ྲྀ䜚ฟ䛩 
௨ୗ┬␎ 
Pan.py 
(䛴䛵䛝) 
㛵ᩘ: 
make䛒䜣䜁䜣 
䛣䛽䜛 
Ⓨ㓝 
䛒䜣䜁䜣ᙧస䜚 
↝䛟 
㛵ᩘ:make䜹䝺䞊䝟䞁 
䛣䛽䜛 
Ⓨ㓝 
䜹䝺䞊䝟䞁ᙧస䜚 
ᥭ䛢䜛 
….. 
Pan.py
㛵ᩘྡ 
• 㛵ᩘ䜢༊ู䛩䜛䛯䜑䛾ྡ๓ 
• ⤌䜏㎸䜏㛵ᩘ䛸⿕䜙䛺䛔䜘䛖䛻ὀព䛜ᚲせ 
Python 
ධ㛛 
119
ᘬᩘ 
• 㛵ᩘ䜢࿧䜃ฟ䛩㝿䛻㛵ᩘ䛻୚䛘䜛್ 
• ௬ᘬᩘ(㛵ᩘ䛾ᐃ⩏ഃ䛷ཷ䛡ྲྀ䜛ኚᩘ) 
• ᐇᘬᩘ(㛵ᩘ䛾࿧䜃ฟ䛧ഃ䛷୚䛘䜛್) 
Python 
ධ㛛 
120 
def 
㛵ᩘྡ(௬ᘬᩘ䠅 
㛵ᩘෆ䛾ฎ⌮ 
㛵ᩘྡ(ᐇᘬᩘ) 
ᐃ⩏ 
࿧䜃ฟ䛧
㏉䜚್ 
• 㛵ᩘ䛷ฎ⌮䛥䜜䛯䝕䞊䝍䜢࿧䜃ฟ䛧ඖ䛻㏉䛩 
• ㏉䜚್䛜䛺䛔ሙྜ䜒䛒䜛(๪స⏝䛾䜏䛾㛵ᩘ) 
Python 
ධ㛛 
121 
length 
= 
len([1,2,3,4,5]) 
length 
len([1,2,3,4,5]) 
5 
㏉䜚್
㛵ᩘ䛾ᐇ⿦ 
Python 
ධ㛛 
122
㛵ᩘసᡂ䛾᭩ᘧ1 
• ᘬᩘ䚸㏉䜚್䛺䛧䛾᭩ᘧ 
• ㏉䜚್䜢᫂♧䛧䛺䛔ሙྜ䛿 
None 
䛜㏉䜛 
Python 
ධ㛛 
123 
def 
㛵ᩘྡ(): 
ฎ⌮ 
def 
printHello(): 
print(hello) 
printHello() 
= 
hello
㛵ᩘసᡂ䛾᭩ᘧ2 
• ᘬᩘ䚸㏉䜚್䛒䜚 
• ᘬᩘ䛿㛵ᩘྡ䛾ᚋ䛾()䛻ᚲせ䛺ᩘ䛰䛡ᣦᐃ 
• ㏉䜚್䛿 
return 
䛷᫂♧ⓗ䛻᭩䛟 
Python 
ධ㛛 
124 
def 
㛵ᩘྡ(ᘬᩘ): 
ฎ⌮ 
return 
㏉䜚್ 
def 
adder(a, 
b): 
return 
a 
+ 
b 
print(adder(3,4)) 
= 
7
㛵ᩘ䛾䝃䞁䝥䝹 
def 
power(x): 
return 
x*x 
def 
absolute(x): 
if(x0): 
return 
-­‐x 
else: 
return 
x 
print(power(6)) 
-­‐ 
36 
print(absolute(-­‐5)) 
-­‐ 
5 
Python 
ධ㛛 
125
ᘬᩘ䛾䝕䝣䜷䝹䝖್ 
• ᣦᐃ䛧䛺䛔ሙྜ䛻タᐃ䛥䜜䜛ᘬᩘ 
Python 
ධ㛛 
126 
def 
func(a, 
b=1) 
print(a) 
print(b) 
func(5,6) 
-­‐ 
5 
6 
func(5) 
-­‐ 
5 
5
global 
• 㛵ᩘෆ䛷䜾䝻䞊䝞䝹ኚᩘ䛻䜰䜽䝉䝇䛩䜛䛯䜑䛾ᐉゝ 
Python 
ධ㛛 
127 
gv 
= 
0 
def 
func1(): 
gv 
= 
1 
def 
func2(): 
global 
gv 
gv 
= 
2 
print(gv) 
-­‐ 
0 
func1() 
print(gv) 
-­‐ 
0 
func2() 
print(gv) 
-­‐ 
2
₇⩦1 
Python 
ධ㛛 
128
₇⩦ 
• day2-­‐1.py.zip 
䜢䝎䜴䞁䝻䞊䝗䛧ゎ෾ 
• text 
= 
ࠥ 
䛸䛧䛶஦๓ᐃ⩏䛥䜜䛶䛔䜛ᩥᏐิ䛾 
༢ㄒฟ⌧ᩘ䜢䜹䜴䞁䝖䛩䜛䝥䝻䜾䝷䝮䜢సᡂ 
• 䛂༢ㄒ:ฟ⌧ᩘ䛃䛸䛔䛖䝣䜷䞊䝬䝑䝖䛷ฟຊ䛩䜛 
• ᩥᏐิ䛾ศ๭: 
ᩥᏐิ.split() 
-­‐ 
[༢ㄒ䛾㓄ิ] 
Ⓨᒎ⦅ 
Python 
ධ㛛 
dog:1 
cat:2 
... 
129 
• ฟ⌧ᩘ䛜ከ䛔༢ㄒ㡰䛻䝋䞊䝖䛧䛶⾲♧䛩䜛 
• 䝠䞁䝖䛿ḟ䝨䞊䝆
₇⩦⿵㊊ 
• 䝸䝇䝖䜢౑䛳䛯⡆᫆䝋䞊䝖䜰䝹䝂䝸䝈䝮 
• sort㛵ᩘ䜢౑䛳䛶䜒ᐇ⿦ྍ⬟䛰䛜䚸䜎䛰ᩍ䛘䛶䛺䛔 
䝔䜽䝙䝑䜽䛜ᚲせ 
Python 
ධ㛛 
130 
list1 
= 
[3,6,2,9,0,1,5] 
list2 
= 
[] 
while(0 
!= 
len(list1)): 
maxValue 
= 
max(list1) 
index 
= 
list1.index(maxValue) 
list2.append(maxValue) 
del 
list1[index] 
print(list2) 
-­‐ 
[9, 
6, 
5, 
3, 
2, 
1, 
0]
䝰䝆䝳䞊䝹 
Python 
ධ㛛 
131
䝰䝆䝳䞊䝹䛾ᚲせᛶ 
• 䝰䝆䝳䞊䝹 
• 䝴䞊䝄సᡂ䝰䝆䝳䞊䝹: 
1䛴䛾python䝣䜯䜲䝹 
Python 
ධ㛛 
132
䝰䝆䝳䞊䝹䜈䛾䜰䜽䝉䝇 
• ᶵ⬟䛤䛸䛻䜘䜛䝥䝻䜾䝷䝮䛾ᩚ⌮ 
Python 
ධ㛛 
133 
math 
file 
system 
os 
⮬ศ䛜సᡂ 
⤌䜏㎸䜏 
(䝕䝣䜷䝹䝖䛷 
౑䛘䜛)
䝴䞊䝄䛻䜘䜛䝰䝆䝳䞊䝹䛾సᡂ 
• 䝥䝻䜾䝷䝮䛾㛤Ⓨ䛻䜘䜚䝁䞊䝗䛜ᕧ኱䛻䚹 
Python 
ධ㛛 
134 
䝣䜯䜲䝹 
200⾜ 
䝣䜯䜲䝹 
50000⾜
䝰䝆䝳䞊䝹䛾ศ๭ 
• ᕧ኱䛺䝣䜯䜲䝹䛿䛂䛹䛣䛻ఱ䛜䛒䜛䛛䛃䜟䛛䜚䛻䛟䛔 
• 㐺ษ䛺⢏ᗘ䛷䝣䜯䜲䝹䜢ศ๭䛩䜛 
Python 
ධ㛛 
135 
010101010101010101010101 
010101010010101010101010 
101010101010101010010101 
010101010101010101010101 
010010101010101010101010 
101010101010010101010101 
010101010101010101010010 
101010101010101010101010 
101010010101010101010101 
010101010101010010101010 
010101010 
101010101 
010101010 
101010010 
101010101 
010101010 
101010101 
010101010 
101010010 
101010101 
010101010 
101010101 
010101010 
101010010 
101010101 
010101010 
101010101 
010101010 
101010010 
101010101 
ᕧ኱䛺䝣䜯䜲䝹 
」ᩘ䛾ᑠ䛥䛺䝣䜯䜲䝹
䝰䝆䝳䞊䝹䛾ཧ↷ 
Python 
ධ㛛 
136 
010101010 
101010101 
010101010 
101010010 
101010101 
010101010 
101010101 
010101010 
101010010 
101010101 
010101010 
101010101 
010101010 
101010010 
101010101 
010101010 
101010101 
010101010 
101010010 
101010101 
010101010 
101010101 
010101010 
101010010 
101010101 
010101010 
10101010䠍 
䝰䝆䝳䞊䝹䜢౑䛖䝥䝻䜾䝷䝮 
඲య䛾ὶ䜜䜢グ㏙ 
䝰䝆䝳䞊䝹䜢౑䛖䝥䝻䜾䝷䝮 
඲య䛾ὶ䜜䜢グ㏙
import 
• import 
䛩䜛䛣䛸䛷䝰䝆䝳䞊䝹䛜౑䛘䜛 
Python 
import 
mymath 
print(mymath.add(2,3)) 
# 
-­‐ 
5 
print(mymath.minus(2,3)) 
# 
-­‐ 
-­‐1 
print(mymath.mulcply(2,3)) 
# 
-­‐ 
6 
print(divide(2,3)) 
#ERROR 
HAPPEN 
Traceback 
(most 
recent 
call 
last): 
File 
~/main.py, 
line 
6, 
in 
module 
print(divide(2,3)) 
NameError: 
name 
'divide' 
is 
not 
defined 
ධ㛛 
137 
def 
add(a,b): 
return 
a 
+ 
b 
def 
minus(a, 
b): 
return 
a 
-­‐ 
b 
def 
mulcply(a, 
b): 
return 
a 
* 
b 
def 
divide(a, 
b): 
return 
a 
/ 
b
from 
• from 
䝣䜯䜲䝹ྡ(䝟䝑䜿䞊䝆ྡ) 
import 
* 
䛷䝣䜯䜲䝹ྡ 
䛾ᣦᐃ䛺䛧䛻࿧䜃ฟ䛧䛜ྍ⬟䛻䛺䜛 
Python 
ධ㛛 
138 
def 
add(a,b): 
return 
a 
+ 
b 
def 
minus(a, 
b): 
return 
a 
-­‐ 
b 
def 
mulcply(a, 
b): 
return 
a 
* 
b 
def 
divide(a, 
b): 
return 
a 
/ 
b 
from 
mymath 
import 
* 
print(add(2,3)) 
# 
-­‐ 
5 
print(minus(2,3)) 
# 
-­‐ 
-­‐1 
print(mulcply(2,3)) 
# 
-­‐ 
6 
print(mymath.divide(2,3)) 
# 
ERROR 
HAPPEN 
Traceback 
(most 
recent 
call 
last): 
File 
~/main.py, 
line 
6, 
in 
module 
print(divide(2,3)) 
NameError: 
name 
'mymath' 
is 
not 
defined
(⿵㊊) 
ᚠ⎔ཧ↷ 
• 䝣䜯䜲䝹A䛸B䛜஫䛔䛻ཧ↷䛧䛒䛳䛶䛔䜛 
• ᮃ䜎䛧䛟䛺䛟䚸䜶䝷䞊䛻䛺䜛ሙྜ䛜䛒䜛 
Python 
ධ㛛 
139 
import 
file2 
print(file1) 
import 
file1 
print(file2) 
file1.py 
$ 
python 
file1.py 
file1 
file2 
file1 
file2.py 
䛺䜣䛷2ᅇ?
ᩥᏐิ 
Python 
ධ㛛 
140
ᩥᏐิ䛾ฎ⌮1 
• ᩥᏐิ䛾సᡂ: 
str1=ᩥᏐิ, 
str2=' 
ᩥᏐิ' 
• ᩥᏐิ䛾⤖ྜ: 
ᩥᏐิ 
+ 
ᩥᏐิ 
• 䜶䝇䜿䞊䝥グྕ: 
 
䛻⥆䛡䛶≉ᐃ䛾ᩥᏐ 
• 㛗䛥䛾ྲྀᚓ: 
len(ᩥᏐิ) 
Python 
ධ㛛 
141
䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮1 
# 
ᩥᏐิ䛾సᡂ 
 
str1 
= 
hello 
 
str2 
= 
' 
world' 
# 
⤖ྜ 
 
print(str1 
+ 
str2) 
hello 
world 
# 
䜶䝇䜿䞊䝥 
 
print(str1 
+ 
t 
+ 
str2 
+ 
ncisco) 
hello 
world 
cisco 
# 
㛗䛥䛾ྲྀᚓ 
 
len(str1) 
5 
Python 
ධ㛛 
142
ᩥᏐิ䛾ฎ⌮2 
• ⧞䜚㏉䛧: 
ᩥᏐิ 
* 
⧞䜚㏉䛧ᅇᩘ 
• ᩥᏐิ໬: 
str(ᩥᏐิ௨እ䛾䜸䝤䝆䜵䜽䝖) 
• ᩥᏐิ䛾୍ᩥᏐ䜢ྲྀᚓ㻌ᩥᏐิ[index] 
• ᩥᏐิ䛾⠊ᅖྲྀᚓ: 
ᩥᏐิ[start:end] 
Python 
ධ㛛 
143
䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮2 
# 
⧞䜚㏉䛧 
 
print(cisco 
* 
3) 
ciscociscocisco 
# 
ᩥᏐิ໬ 
 
print(value: 
 
+ 
str(5)) 
# 
㠀ᩥᏐิ䜢ᩥᏐิ໬䛧䛶䛛䜙⤖ྜ 
value: 
5 
# 
ᩥᏐิ䛾୍ᩥᏐ䜢ྲྀᚓ 
 
hello 
world[4] 
'o' 
# 
ᩥᏐิ䛾୍㒊䜢ྲྀᚓ 
 
hello 
world[4:8] 
'o 
wo' 
Python 
ධ㛛 
144
ᩥᏐิ䛾ฎ⌮3 
• ⨨䛝᥮䛘: 
ᩥᏐิ.replace(old, 
new) 
• ศ๭: 
ᩥᏐิ.split(༊ษ䜚ᩥᏐ) 
• ྵ䜐䛛: 
᥈䛩ᩥᏐิ 
in 
᥈䛥䜜䜛ᩥᏐิ 
• ఩⨨: 
ᩥᏐิ.find(᥈䛩ᩥᏐ) 
Python 
ධ㛛 
145
䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮3 
# 
⨨䛝᥮䛘 
 
hello 
world.replace(world, 
python) 
'hello 
python' 
# 
ศ๭ 
 
hello 
world.split( 
) 
['hello', 
'world'] 
# 
ྵ䜐䛛 
 
hell 
in 
hello 
world 
True 
# 
఩⨨ 
 
hello 
world.find(o) 
4 
 
hello 
world.find(X) 
-­‐1 
# 
ᣦᐃ䛧䛯ᩥᏐิ䛜ྵ䜎䜜䛶䛔䛺䛔ሙྜ䛿 
-­‐1 
Python 
ධ㛛 
146
ṇつ⾲⌧ 
Python 
ධ㛛 
147
ṇつ⾲⌧䛸䛿 
• ᩥᏐิ䝟䝍䞊䞁䛾ᐃ⩏ 
• ᰂ㌾䛻ᩥᏐิ䛾䝟䝍䞊䞁䝬䝑䝏䞁䜾䜢⾜䛖䛣 
䛸䛜䛷䛝䜛 
– 䝟䝍䞊䞁䝬䝑䝏䞁䜾䛻ヱᙜ䛩䜛䛛 
– ヱᙜ䛩䜛ᩥᏐิ䛾ྲྀᚓ 
Python 
ධ㛛 
148
ṇつ⾲⌧䛾᭷ຠᛶ 
• ᩥᏐิ䛜ᩚᩘ䛛䛹䛖䛛ุᐃ䛩䜛䛯䜑䛾䝥䝻 
䜾䝷䝮䜢సᡂ䛩䜛 
• ṇつ⾲⌧䛾᭷↓䛷䝁䞊䝗㔞䛜኱ᖜ䛻㐪䛖 
Python 
ධ㛛 
149 
def 
isIntegerString(string): 
numberList 
= 
['1','2','3','4','5','6','7','8','9','0'] 
if 
len(string) 
== 
0: 
return 
false 
for 
char 
in 
string: 
if 
char 
not 
in 
numberList: 
return 
False 
return 
True 
import 
re 
def 
isIntegerString2(string): 
return 
re.match('^[d]*$', 
string) 
!= 
None 
ṇつ⾲⌧䛺䛧 
ṇつ⾲⌧䛒䜚
python 
䛾ṇつ⾲⌧సἲ 
• re: 
ṇつ⾲⌧䛾䝟䝑䜿䞊䝆 
import 
re 
• match 
: 
ྜ⮴䛩䜛ᩥᏐิ⩌䜢ᢤ䛝ฟ䛩 
re.match(䝟䝍䞊䞁ᩥᏐิ, 
ᩥᏐิ) 
• search: 
ྜ⮴䛩䜛ᩥᏐิ䛾఩⨨䜢㏉䛩 
re.search(䝟䝍䞊䞁ᩥᏐิ䚸ᩥᏐิ) 
Python 
ධ㛛 
150
ṇつ⾲⌧ 
Python 
ධ㛛 
151 
 
m 
= 
re.match(^[d]*$, 
12345) 
 
print(m) 
_sre.SRE_Match 
object 
at 
0x1005bdbf8 
 
m.group() 
'12345' 
 
m.start(), 
m.end() 
(0, 
5)
䝣䜯䜲䝹ฎ⌮ 
Python 
ධ㛛 
152
䝣䜯䜲䝹ฎ⌮䛾ὶ䜜 
• 䝣䜯䜲䝹ฎ⌮䛾ὶ䜜 
1. file䜢䜸䞊䝥䞁䛧䛶 
file䜸䝤䝆䜵䜽䝖䜢ྲྀᚓ 
2. file䜸䝤䝆䜵䜽䝖䛻ᑐ䛧䛶ฎ⌮䜢⾜䛖 
3. file䜢䜽䝻䞊䝈䛩䜛 
Python 
ධ㛛 
153
䝣䜯䜲䝹䜸䝤䝆䜵䜽䝖 
• 䝣䜯䜲䝹䜸䝤䝆䜵䜽䝖䛾సᡂ 
open(䝣䜯䜲䝹䝟䝇, 
䜸䞊䝥䞁䛩䜛䝰䞊䝗) 
– 䝣䜯䜲䝹䝟䝇: 
┦ᑐ 
or 
⤯ᑐ 
– 䝰䞊䝗(䝕䝣䜷䝹䝖䛿r) 
– r: 
ㄞ䜏㎸䜏䝰䞊䝗 
– w: 
᪂つ᭩䛝㎸䜏䝰䞊䝗 
– a: 
㏣ຍ᭩䛝㎸䜏䝰䞊䝗 
• 䝣䜯䜲䝹䜸䝤䝆䜵䜽䝖䛾䜽䝻䞊䝈 
f.close() 
Python 
ධ㛛 
154
ㄞ䜏㎸䜏ฎ⌮ 
• Read 
– readline(): 
୍⾜ㄞ䜏㎸䜏 
– readlines(): 
඲䛶䛾⾜䜢 
list 
䛸䛧䛶ㄞ䜏㎸䜏 
Python 
ධ㛛 
155 
 
f 
= 
open(file.txt, 
r) 
 
f.readline() 
'hello 
world1n' 
 
f.readline() 
'hello 
world2n' 
 
f.readlines() 
['hello 
world3n', 
'hello 
world4n'] 
hello 
world1 
hello 
world2 
hello 
world3 
hello 
world4 
file.txt
᭩䛝㎸䜏ฎ⌮ 
• Write 
– write(ᩥᏐิ) 
: 
ᩥᏐิ䜢᭩䛝㎸䜏 
– writelines(ᩥᏐิ䛾䝸䝇䝖) 
: 
䝸䝇䝖䜢᭩䛝㎸䜏 
– flush() 
: 
䝞䝑䝣䜯䜢䝕䜱䝇䜽䛻䝣䝷䝑䝅䝳 
Python 
ධ㛛 
156 
 
f2 
= 
open(file2.txt, 
w) 
 
f2.write(hello) 
 
f2.write(world) 
 
f2.write(n) 
 
wlist 
= 
[abc, 
def, 
ghi] 
 
f2.writelines(wlist) 
 
f2.flush() 
 
f2.close() 
helloworld 
abcdefghi 
file2.txt
ୖ᭩䛝䛸㏣グ 
• ୖ᭩䛝: 
䝣䜯䜲䝹䛾ෆᐜ䜢ᾘ䛧䛶䛛䜙᭩䛝㎸䜐 
• ㏣グ: 
䝣䜯䜲䝹䛾ᮎᑿ䛻㏣ຍ䛷᭩䛝㎸䜐 
Python 
ධ㛛 
157
PICKEL 
Python 
ධ㛛 
158
Pickel 
• 䝥䝻䜾䝷䝮䛾ᐇ⾜≧ែ䛿䝥䝻䜾䝷䝮䜢೵Ṇ䛩䜛䛸ᾘኻ 
• Pickel 
䛿䜸䝤䝆䜵䜽䝖䜢䝣䜯䜲䝹䛻Ọ⥆ⓗ䛻ಖᏑ䛩䜛ᡭἲ 
• Pickel 
– dump: 
䜸䝤䝆䜵䜽䝖䜢䝣䜯䜲䝹䛻᭩䛝ฟ䛧 
– load: 
䝣䜯䜲䝹䛛䜙䜸䝤䝆䜵䜽䝖䜢ㄞ䜏ฟ䛧 
Python 
ධ㛛 
159
Pickel 
Python 
ධ㛛 
160 
yuichi$ 
python 
 
data 
= 
[hello, 
world, 
1, 
2, 
3] 
 
import 
pickle 
 
f 
= 
open(dump.txt, 
w) 
 
pickle.dump(data, 
f) 
 
f.close() 
 
exit() 
(lp0 
S'hello' 
p1 
aS'world' 
p2 
aI1 
aI2 
aI3 
a. 
$ 
python 
 
f 
= 
open(dump.txt) 
 
import 
pickle 
 
data 
= 
pickle.load(f) 
 
print(data) 
['hello', 
'world', 
1, 
2, 
3] 
dump.txt
PATH 
Python 
ධ㛛 
161
䝟䝇 
• OSୖ䛾䝣䜯䜲䝹䜔䝕䜱䝺䜽䝖䝸䛾ᡤᅾᆅ䜢♧䛩 
Python 
ධ㛛 
162 
/Users/yuichi/Documents/python.pptx 
/ 
Users/ 
yuichi/ 
Documents/ 
python.pptx 
etc/ 
var/ 
Shared/ 
Desktop/ 
Pictures/ 
java.pptx 
VM/
⤯ᑐ䝟䝇䛸┦ᑐ䝟䝇 
• ⤯ᑐ䝟䝇: 
䝹䞊䝖䛛䜙㎺䜛䝟䝇 
• ┦ᑐ䝟䝇: 
⌧ᅾ䛾⮬ศ䛾఩⨨䛛䜙㎺䜛䝟䝇 
– ./ 
: 
⮬ศ䛜䛔䜛䝕䜱䝺䜽䝖䝸 
– ../ 
: 
⮬ศ䛾ୖ䛾㝵ᒙ䛾䝕䜱䝺䜽䝖䝸 
Python 
ධ㛛 
163 
/ 
Users/ 
yuichi/ 
Documents/ 
python.pptx 
etc/ 
var/ 
Shared/ 
Desktop/ 
test.txt 
java.pptx 
VM/ 
/Users/yuichi/test.txt 
../test.txt
䝟䝇䛾ྲྀᚓ 
• ⌧ᅾ䛾䜹䝺䞁䝖䝕䜱䝺䜽䝖䝸䜢ྲྀᚓ: 
os.getcwd() 
• 䝕䜱䝺䜽䝖䝸䛾⛣ື: 
os.chdir(⛣ືඛ䛾䝟䝇) 
• ⤯ᑐ䝟䝇䛾ྲྀᚓ: 
os.path.abspath(path) 
• OS㠀౫Ꮡ䛾䝟䝇䛾ྲྀᚓ᪉ἲ㻌os.path.join(TUPLE) 
Python 
ධ㛛 
164 
yuichi$ 
ls 
| 
grep 
Documents 
Documents 
yuichi$ 
python 
 
import 
os 
 
os.getcwd() 
'/Users/yuichi' 
 
os.path.abspath(Documents) 
'/Users/yuichi/Documents' 
 
os.path.join(/,dir1,dir2,file1) 
'/dir1/dir2/file1'
䝟䝇䛻㛵䛩䜛㛵ᩘ 
• 䝣䜯䜲䝹㻌or 
䝕䜱䝺䜽䝖䝸䛾Ꮡᅾ☜ㄆ: 
os.path.exists(䝟䝇) 
• 䝕䜱䝺䜽䝖䝸䛛: 
os.path.isdir(䝟䝇) 
• 䝣䜯䜲䝹䛛: 
os.path.isfile(䝟䝇) 
Python 
ධ㛛 
165
䝟䝇䛾᧯స㛵ᩘ 
Python 
ධ㛛 
166 
 
os.path.exists(/Users/yuichi) 
True 
 
os.path.isfile(/Users/yuichi) 
False 
 
os.listdir(./) 
['dir1', 
'file1'] 
 
os.mkdir(dir2) 
 
os.rmdir(dir2)
䝕䜱䝺䜽䝖䝸䛾᧯స㛵ᩘ 
• 䝕䜱䝺䜽䝖䝸䛾సᡂ: 
os.mkdir(䝕䜱䝺䜽䝖䝸䛾䝟䝇) 
• 䝕䜱䝺䜽䝖䝸䛾๐㝖: 
os.rmdir(䝕䜱䝺䜽䝖䝸䛾䝟䝇) 
• 䝕䜱䝺䜽䝖䝸䛾୰㌟୍ぴ: 
os.listdir(䝟䝇) 
• 䝣䜯䜲䝹䚸䝕䜱䝺䜽䝖䝸䛾๐㝖: 
os.remove(䝟䝇) 
• 䝕䜱䝺䜽䝖䝸䛾෌ᖐⓗ๐㝖: 
os.removedirs(䝟䝇) 
Python 
ධ㛛 
167
PYTHON 
AS 
䝅䜵䝹䝇䜽䝸䝥䝖 
Python 
ධ㛛 
168
䝅䜵䝹䝇䜽䝸䝥䝖 
• 䝥䝻䜾䝷䝭䞁䜾: 
C 
䜔 
Java 
䛾䜘䛖䛺䝅䜵䝹䜢ព㆑䛧䛺 
䛔䝇䝍䜲䝹 
• 䝅䜵䝹䝇䜽䝸䝥䝖: 
䝅䜵䝹⮬య䜢᧯స䛩䜛䝥䝻䜾䝷䝭䞁 
䜾䝇䝍䜲䝹 
Python 
ධ㛛 
169 
import 
os 
currentDir 
= 
os.getcwd() 
files 
= 
os.listdir(currentDir) 
for 
fname 
in 
files: 
print(fname) 
import 
commands 
files 
= 
commands.getoutput(ls).split() 
for 
fname 
in 
files: 
print(fname) 
㏻ᖖ䛾䝥䝻䜾䝷䝭䞁䜾䝇䝍䜲䝹 
䝅䜵䝹䝇䜽䝸䝥䝖䛾䝇䝍䜲䝹
䝅䜵䝹䝇䜽䝸䝥䝖グ㏙䛾䝁䝒 
• ྍ⬟䛺㝈䜚᪤Ꮡ䛾䝁䝬䞁䝗䜢౑䛖 
• 䝁䝬䞁䝗䛸䝁䝬䞁䝗䛾᥋⥆䛻 
python 
䜢౑䛖 
Python 
ධ㛛 
170 
(1) 
⤖ᯝ 
= 
䝁䝬䞁䝗䛾Ⓨ⾜ 
(2) 
⤖ᯝ䜢ฎ⌮ 
(3) 
ฎ⌮⤖ᯝ䛻䜒䛸䛵䛔䛶ḟ䛾䝁䝬䞁䝗䜢సᡂ 
(4) 
(1)䜈
䝁䝬䞁䝗䛾࿧䜃ฟ䛧 
Python 
ධ㛛 
171 
• os.system(䝁䝬䞁䝗) 
: 
ฟຊ䛜୙せ䛺ሙྜ 
• commands.getoutput(䝁䝬䞁䝗) 
: 
ฟຊ䛜ᚲせ䛺ሙྜ 
 
commands.getoutput(ls).split(n) 
['CONFIG_FILE', 
'Cisco 
Mac 
Support.webloc', 
'Desktop', 
'Documents', 
'Downloads', 
'Dropbox', 
'Library', 
'MATERIAL', 
'Movies', 
'Music', 
'Pictures', 
'Public', 
'SR', 
'STP-­‐BA-­‐Dispute_Japan-­‐ 
RSVT.pptx', 
'VM', 
'appProperces', 
'bingo.py', 
'bingo.pyc', 
'dump.txt', 
'file.txt', 
'file2.txt', 
'get-­‐fpath-­‐info.py', 
'mki-­‐fp-­‐ 
capture.zip', 
'tcpdump', 
'tcpdump.zip', 
'test', 
'unctled 
folder']
command.getoutput() 
Python 
ධ㛛 
172 
import 
commands 
result 
= 
commands.getoutput(ping 
-­‐c 
5 
cisco.com) 
lines 
= 
result.split(n) 
for 
line 
in 
lines: 
print(line) 
PING 
cisco.com 
(72.163.4.161): 
56 
data 
bytes 
64 
bytes 
from 
72.163.4.161: 
icmp_seq=0 
7l=239 
cme=195.865 
ms 
64 
bytes 
from 
72.163.4.161: 
icmp_seq=1 
7l=239 
cme=200.497 
ms 
64 
bytes 
from 
72.163.4.161: 
icmp_seq=2 
7l=239 
cme=197.125 
ms 
64 
bytes 
from 
72.163.4.161: 
icmp_seq=3 
7l=239 
cme=197.620 
ms 
64 
bytes 
from 
72.163.4.161: 
icmp_seq=4 
7l=239 
cme=197.553 
ms 
-­‐-­‐-­‐ 
cisco.com 
ping 
stacsccs 
-­‐-­‐-­‐ 
5 
packets 
transmi7ed, 
5 
packets 
received, 
0.0% 
packet 
loss 
round-­‐trip 
min/avg/max/stddev 
= 
195.865/197.732/200.497/1.519 
ms
exping 
䛾ᐇ⿦ 
• 」ᩘ䛾ᐄඛ䛾ping฿㐩⋡䜢 ᐃ 
Python 
ධ㛛 
173 
import 
commands 
def 
ping(dest): 
result 
= 
commands.getoutput(ping 
-­‐c 
5 
{}.format(dest)) 
lines 
= 
result.split(n) 
length 
= 
len(lines) 
packetLoss 
= 
lines[length 
-­‐2].split()[6] 
r7 
= 
lines[length 
-­‐1].split()[3].split(/)[1] 
return 
(dest, 
packetLoss, 
r7) 
dests 
= 
[cisco.com, 
google.com, 
yahoo.com] 
for 
dest 
in 
dests: 
print(ping(dest)) 
('cisco.com', 
'0.0%', 
'196.756') 
('google.com', 
'0.0%', 
'13.317') 
('yahoo.com', 
'0.0%', 
'262.060')
Linux䝩䝇䝖ୖ䛷䛾ping 
• ≉ᐃIP䜰䝗䝺䝇䛾䝩䝇䝖㛫䛾䜏㏻ಙ䛷䛝䛺䛔 
• ஦㇟Ⓨ⏕☜❧䛿㻌1/16 
⛬ᗘ 
VLAN 
X 
VLAN 
Y 
Nexus5500 
Primary 
Nexus5500 
Secondary 
Python 
ධ㛛 
174 
Nexus2000 
FEX 
L2 
Switch 
Source 
Dest 
VPC(VSS) 
䝩䝇䝖50ྎ 
䝩䝇䝖50ྎ 
50 
x 
50 
-­‐ 
2500 
flow
ping 
฿㐩⋡䛾 ᐃ䝥䝻䜾䝷䝮 
Python 
ධ㛛 
175
₇⩦ 
Python 
ධ㛛 
176
₇⩦ 
• ᮍ᏶ᡂ䛾䝻䜾ᢳฟ䝥䝻䜾䝷䝮䜢᏶ᡂ䛥䛫䜛 
• 䝞䜾䛿3⟠ᡤ䛻䛒䜛 
yuichi$ 
python 
log.py 
-­‐f 
log.log 
-­‐s 
2014 
Jan 
28 
12:30:50 
-­‐e 
2014 
Jan 
29 
16:06:15 
-­‐k 
port-­‐channel 
-­‐f 
: 
Python 
ධ㛛 
177 
䝣䜯䜲䝹ྡ 
-­‐s 
: 
䝇䝍䞊䝖᫬้ 
-­‐e 
: 
⤊஢᫬้ 
-­‐k 
: 
䜻䞊䝽䞊䝗 
dhcp-­‐10-­‐141-­‐56-­‐250:Documents 
yuichi$ 
python 
log.py 
-­‐f 
log.log 
-­‐s 
2014 
Jan 
28 
12:30:50 
-­‐e 
2014 
Jan 
29 
16:06:15 
-­‐k 
port-­‐channel 
2014 
Jan 
29 
12:14:20 
N6K-­‐2 
%ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: 
isis_fabricpath-­‐default 
[5150] 
P2P 
adj 
L1 
n7kb-­‐MKI-­‐FP 
over 
port-­‐channel50 
-­‐ 
DOWN 
(New) 
on 
MT-­‐0 
2014 
Jan 
29 
12:14:20 
N6K-­‐2 
%ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: 
isis_fabricpath-­‐default 
[5150] 
P2P 
adj 
L1 
n7kb-­‐MKI-­‐FP 
over 
port-­‐channel50 
-­‐ 
INIT 
on 
MT-­‐0 
2014 
Jan 
29 
12:14:20 
N6K-­‐2 
%ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: 
isis_fabricpath-­‐default 
[5150] 
P2P 
adj 
L1 
n7kb-­‐MKI-­‐FP 
over 
port-­‐channel50 
-­‐ 
UP 
on 
MT-­‐0 
2014 
Jan 
29 
12:24:58 
N6K-­‐2 
%ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: 
isis_fabricpath-­‐default 
[5150] 
P2P 
adj 
L1 
n7kb-­‐MKI-­‐FP 
over 
port-­‐channel50 
-­‐ 
DOWN 
(Delete 
All) 
on 
MT-­‐0 
2014 
Jan 
29 
12:27:01 
N6K-­‐2 
%ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: 
isis_fabricpath-­‐default 
[5150] 
P2P 
adj 
L1 
n7kb-­‐MKI-­‐FP 
over 
port-­‐channel50 
-­‐ 
DOWN 
(New) 
on 
MT-­‐0 
2014 
Jan 
29 
12:27:01 
N6K-­‐2 
%ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: 
isis_fabricpath-­‐default 
[5150] 
P2P 
adj 
L1 
n7kb-­‐MKI-­‐FP 
over 
port-­‐channel50 
-­‐ 
INIT 
on 
MT-­‐0 
2014 
Jan 
29 
12:27:01 
N6K-­‐2 
%ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: 
isis_fabricpath-­‐default 
[5150] 
P2P 
adj 
L1 
n7kb-­‐MKI-­‐FP 
over 
port-­‐channel50 
-­‐ 
UP 
on 
MT-­‐0
䝁䞊䝗(䝞䜾䛿2⟠ᡤ) 
Python 
ධ㛛 
178 
import 
sys, 
os 
def 
getTimeString(line): 
return 
line[0:20] 
def 
parseTime(string): 
monthDict 
= 
{Jan:1, 
Feb:2, 
Mar:3, 
Apr:4, 
May:5, 
Jun:6, 
Jul:7, 
Aug:8, 
Sep:9, 
Oct:10, 
Nov:11, 
Dec:12} 
d1 
= 
string.split( 
) 
d2 
= 
d1[3].split(:) 
return 
(int(d1[0]), 
monthDict[d1[1]], 
int(d1[2]), 
int(d2[0]), 
int(d2[1]), 
int(d2[2])) 
def 
printFile(fname, 
start, 
end, 
keyword): 
if(not 
os.path.isfile(fname)): 
print(File 
{} 
isn't 
exist. 
exit.format(fname)) 
sys.exit() 
f 
= 
open(fname) 
for 
line 
in 
f.readlines(): 
line 
= 
line.rstrip() 
t 
= 
parseTime(getTimeString(line)) 
within 
= 
(start 
= 
t) 
and 
(t 
= 
end) 
contains 
= 
keyword 
in 
line 
if(within 
and 
contains): 
print(line) 
䝁䝬䞁䝗ᘬᩘ䛾ึᮇ್ 
fname 
= 
 
start 
= 
1900 
Jan 
1 
00:00:00 
end 
= 
2100 
Jan 
1 
00:00:00 
keyword 
= 
 
for 
index 
in 
range(len(sys.argv)): 
arg 
= 
sys.argv[index] 
if(arg 
== 
-­‐f): 
fname 
= 
sys.argv[index 
+ 
1] 
elif(arg 
== 
-­‐s): 
start 
= 
sys.argv[index 
+ 
1] 
elif(arg 
== 
-­‐k): 
keyword 
= 
sys.argv[index 
+ 
1] 
if(fname 
== 
): 
s1 
= 
syntax: 
python 
log.py 
-­‐f 
FILENAME 
-­‐s 
 
s2 
= 
START_TIME 
-­‐e 
END_TIME 
-­‐k 
KEYWORD 
print(s1 
+ 
s2) 
print(START_TIME, 
END_TIME, 
KEYWORD 
are 
opcon.) 
else: 
printFile(fname, 
start, 
end, 
keyword) 
䝻䜾䛾୍⾜䛛䜙᫬㛫䛾ᩥᏐิ䜢ᢳฟ 
ᢳฟ䛧䛯ᩥᏐิ䜢ẚ㍑ྍ⬟䛺䝍䝥䝹䛻ኚ᥮ 
᮲௳䛻䝬䝑䝏䛧䛯⾜䜢printฟຊ 
䝁䝬䞁䝗ᘬᩘ䜢ㄞ䜏ྲྀ䜛 
䝣䜯䜲䝹ྡ䛜୚䛘䜙䜜䛶䛔䛯䜙䝥䝻䜾䝷䝮䜢ᐇ⾜
₇⩦ 
Python 
ධ㛛 
179 
Ⓨᒎ⦅ 
䜻䞊䝽䞊䝗䜢ṇつ⾲⌧䛷ᣦᐃ䛷䛝䜛䜘䛖䛻䛩䜛 
ṇつ⾲⌧䛻䝬䝑䝏䛧䛯ሙྜ䛾䜏⾲♧䜢⾜䛖 
䜸䝥䝅䝵䞁 
-­‐r 
䛻⥆䛡䛶ṇつ⾲⌧䜢ᣦᐃ
₇⩦ 
• 䝷䝪䛾Linux(10.71.224.172)䛻䝻䜾䜲䞁(guest, 
c1sco123)䛧䚸௨ୗ䛾䝇䜽䝸䝥䝖䜢సᡂ䛧ᐇ⾜䛩䜛 
• 䝣䜯䜲䝹ྡ䛿 
cecid.py 
䛸䛩䜛 
Python 
ධ㛛 
180 
௨ୗ䛾ᶵჾ䛾㛤䛔䛶䛔䜛䝫䞊䝖୍ぴ䜢ྲྀᚓ䛩䜛 
-­‐ 
rws1 
(1.0.0.1) 
-­‐ 
gateway 
(1.0.0.100) 
-­‐ 
linux( 
1.110.120.173) 
-­‐ 
esxi 
( 
10.71.224.170) 
-­‐ 
Nexus7000 
(1.110.8.1)
฼⏝䛩䜛䝁䝬䞁䝗 
Python 
ධ㛛 
181 
yuichi$ 
ssh 
-­‐l 
guest 
10.71.224.172 
pass: 
c1sco123 
[guest@fedora172 
~]$ 
nmap 
127.0.0.1 
Starcng 
Nmap 
6.01 
( 
h7p://nmap.org 
) 
at 
2014-­‐02-­‐08 
13:05 
EST 
Nmap 
scan 
report 
for 
fedora172.localdomain 
(127.0.0.1) 
Host 
is 
up 
(0.0016s 
latency). 
Not 
shown: 
995 
closed 
ports 
PORT 
STATE 
SERVICE 
22/tcp 
open 
ssh 
23/tcp 
open 
telnet 
25/tcp 
open 
smtp 
111/tcp 
open 
rpcbind 
631/tcp 
open 
ipp
⾲♧ᙧᘧ 
Python 
ධ㛛 
182 
䜰䝗䝺䝇1 
TCP: 
xxx, 
yyy, 
zzz, 
... 
UDP: 
xxx, 
yyy, 
zzz, 
... 
䜰䝗䝺䝇2 
TCP: 
xxx, 
yyy, 
zzz, 
... 
UDP: 
xxx, 
yyy, 
zzz, 
... 
... 
䜰䝗䝺䝇3 
TCP: 
xxx, 
yyy, 
zzz, 
... 
UDP: 
xxx, 
yyy, 
zzz, 
...
ḟᅇண࿌ 
• 䜸䝤䝆䜵䜽䝖ᣦྥ 
• 䜽䝷䝇 
• 䜰䝹䝂䝸䝈䝮 
• pexpect 
䛻䜘䜛Ciscoᶵჾ䛾䝥䝻䜾䝷䝮᧯స 
Python 
ධ㛛 
183
Python 
ධ㛛 
184
Python 
ධ㛛 
Learning 
to 
Program 
For 
Cisco 
Network 
Engineers 
Day 
3/4 
Japan 
TAC 
DC 
Team. 
Yuichi 
Ito
Agenda 
Day 
3 
• 䜽䝷䝇䛾ᴫᛕ 
• 䜽䝷䝇䛾సᡂ 
• ₇⩦ 
• 䝗䝷䜽䜶䛾ᐇ⿦䛷Ꮫ䜆䜸䝤䝆䜵䜽䝖ᣦྥ 
• ₇⩦1-­‐3 
• pexpect 
䛻䜘䜛 
Cisco 
ᶵჾ䛾᧯సἲ 
• ₇⩦ 
Python 
ධ㛛 
186
䝥䝻䜾䝷䝭䞁䜾ゝㄒ䛾ὶὴ 
Python 
ධ㛛 
187
䝥䝻䜾䝷䝭䞁䜾䛾ὶὴ 
• ᡭ⥆䛝ᆺ 
• 㛵ᩘᆺ 
• 䜸䝤䝆䜵䜽䝖ᣦྥᆺ 
Python 
ධ㛛 
188
䜸䝤䝆䜵䜽䝖ᣦྥ䛾ᴫᛕ 
Python 
ධ㛛 
189
䛺䛬䜸䝤䝆䜵䜽䝖ᣦྥ䛜ᚲせ䛛 
䛒䛘䛶䝁䞊䝗グ㏙సἲ䛻䝹䞊䝹䜢ㄢ䛩䛣䛸䛷䝥䝻 
䜾䝷䝮䛾䜹䜸䝇໬䜢㜵䛠 
-­‐ 
ᐇ⿦䛾ቃ⏺䛜᫂☜䛻䛺䜛 
Python 
ධ㛛 
190 
䝹䞊䝹䛻䛧䛯䛜䛳䛶 
䝥䝻䜾䝷䝮䜢ᩚ⌮
䛺䛬䜸䝤䝆䜵䜽䝖ᣦྥ䛜ᚲせ䛛 
䜸䝤䝆䜵䜽䝖ᣦྥ䛾ᶵ⬟䛷㛤Ⓨຠ⋡䜢ྥୖ䛥䛫䜛 
-­‐ 
⥅ᢎ䛻䜘䜛ᶵ⬟䛾ᘬ䛝⥅䛞 
-­‐ 
䝫䝸䝰䞊䝣䜱䝈䝮 
Python 
ධ㛛 
191 
Bu7on 
Hello 
World 
䝪䝍䞁䜢ᢲ䛧䛯䜙 
hello 
world 
䛸 
⾲♧䛩䜛䝥䝻䜾䝷䝮 
10ᩘ⾜䛷స䜜䜛(⥅ᢎ䛾䛚䛛䛢)
ே㛫䛾ᛮ⪃᪉ἲ 
• 䛂ㄡ䛜ఱ䜢䛩䜛䛛䛃䛜㔜せ 
• ㄡ䛻䛹䛖䛔䛖䛂䝯䝑䝉䞊䝆䛃䜢㏦䛳䛯䜙䚸䛹䛖䛔䛖䛂⤖ 
ᯝ䛃䛜ᚓ䜙䜜䜛䛛 
Python 
ධ㛛 
[ෆ㒊ฎ⌮] 
௙஦䜢䛩䜛 
192 
[䝯䝑䝉䞊䝆] 
䛣䛾௙஦䜢䛧䛶䛟䛰䛥䛔 
[⤖ᯝ] 
ୖྖ 
ᡂᯝ≀䛿䛣䜜䛷䛩䚹 
㒊ୗ
㛵ᩘ㻌vs 
䝯䝋䝑䝗 
䛂௙஦䜢䛥䛫䜛䛃(㒊ୗA䚸௙஦) 
Python 
ධ㛛 
193 
㛵ᩘ䝏䝑䜽䛺⪃䛘᪉ 
䜸䝤䝆䜵䜽䝖ᣦྥ䛾⪃䛘᪉ 
䛂㒊ୗA䛃䛻䛂௙஦䜢䛥䛫䜛䛃(௙஦)
㛵ᩘ㻌vs 
䝯䝋䝑䝗 
• 㛵ᩘ: 
䜸䝤䝆䜵䜽䝖䜢䛂ᘬᩘ䛸䛧䛶䛃౑䛖 
• 䝯䝋䝑䝗: 
䜸䝤䝆䜵䜽䝖䛂䛛䜙䛃ฎ⌮䜢࿧䜃ฟ䛩 
Python 
ධ㛛 
194 
 
string 
= 
hello 
world 
python 
# 
㛵ᩘ 
 
length 
= 
len(string) 
# 
䝯䝋䝑䝗 
 
words 
= 
string.split() 
ᩥᏐิ䛾ฎ⌮
㛵ᩘ㻌vs 
䝯䝋䝑䝗 
• 㛵ᩘ: 
䝕䞊䝍䛸ฎ⌮䛾⤖䜃䛴䛝䛜ᙅ䛔 
• 䝯䝋䝑䝗: 
䝕䞊䝍䛸ฎ⌮䛾⤖䜃䛴䛝䛜ᙉ䛔 
㛵ᩘ 
䝯䝋䝑䝗 
Python 
ධ㛛 
195 
len( 
) 
string.split() 
ฎ⌮䛻䝕䞊䝍䜢୚䛘䜛 
䝕䞊䝍䛻ฎ⌮䜢䛥䛫䜛 
len 
䛿ఱ䛻䛷䜒౑䛘䜛 
split 
䛿 
string 
䛻䛧䛛౑䛘䛺䛔
䜸䝤䝆䜵䜽䝖ᣦྥ 
• ㄡ(䜲䞁䝇䝍䞁䝇)䛻䝯䝑䝉䞊䝆䜢㏦䜛(䝯䝋䝑䝗䜢࿧䜃 
ฟ䛩)䛸䛹䛖䛔䛖⤖ᯝ(㏉䜚್)䛜ᚓ䜙䜜䜛䛛 
• ⣽䛛䛔ෆ㒊䛾௙⤌䜏䛿Ẽ䛻䛧䛺䛔 
グ㏙䝹䞊䝹 
Python 
ධ㛛 
196 
㏉䜚್ 
= 
ㄡ.䝯䝋䝑䝗(䝟䝷䝯䞊䝍)
ฎ⌮䚸᝟ሗ䛾㞃ⶸ 
• ෆ㒊䛾⣽䛛䛔ᣲື䛿⪃䛘䛪≧ែ䛾䜏䜢ព㆑䛩䜛 
䝕䞊䝍䜢HDD䛻᭩䛝㎸䜏䛯䛔 
(1) 
Block 
N 
䛻᭩䛝㎸䜏 
䝇䝖䝺䞊䝆䜸䝤䝆䜵䜽䝖 
᭩䛝㎸䜏ฎ⌮ 
(1)Block 
N 
䛾Disk 
䜢㑅ᢥ 
(2)Block 
N 
䛾䝉䜽䝍䜢ᣦᐃ 
(3)᭩䛝㎸䜏䜢⾜䛺䛖 
䝴䞊䝄䜰䝥䝸䜿䞊䝅䝵䞁䛾 
(ᑓ㛛ᐙ䛜䝥䝻䜾䝷䝮䜢సᡂ) 
䜸䝤䝆䜵䜽䝖 
(䝇䝖䝺䞊䝆䜢౑䛖䛰䛡) 
䈜 
⌧ᅾ䛾䝦䝑䝗఩⨨䛺䛹䛿Ẽ䛻䛧䛺䛔
䜎䛸䜑 
• 䜸䝤䝆䜵䜽䝖ᣦྥ䛾䝁䞁䝉䝥䝖 
– 䛂ㄡ䛜ఱ䜢䛩䜛䛛䛃 
– 䛂ㄡ䛜ㄡ䛸䛹䛖༠ຊ䛩䜛䛛䛃 
– 䛂」㞧䛺ฎ⌮䚸䝕䞊䝍䛿እ㒊䛻ぢ䛫䛺䛔䛃 
• 䝁䞊䝗䛾⛛ᗎ໬ 
– ᶒ㝈䜢ᣢ䛯䛺䛔䜒䛾䛿᧯స䛜䛷䛝䛺䛔 
Python 
ධ㛛 
198
䜽䝷䝇䛾ᴫᛕ 
Python 
ධ㛛 
199
䜽䝷䝇 
• 䜸䝤䝆䜵䜽䝖䛾✀㢮 
• 䜽䝷䝇䛿ᒓᛶ䛸䛧䛶䝕䞊䝍䛸ฎ⌮䜢ᣢ䛴 
Python 
ධ㛛 
200 
ᒓᛶ 
[䝕䞊䝍] 
-­‐ 
䜺䝋䝸䞁䛾㔞 
[ฎ⌮] 
-­‐ 
㉮䜛 
-­‐ 
Ṇ䜎䜛 
-­‐ 
᭤䛜䜛
䜲䞁䝇䝍䞁䝇 
• 䜽䝷䝇⮬య䛷ฎ⌮䛩䜛䛾䛷䛿䛺䛟䚸䜽䝷䝇䛛䜙స䜛䜲 
䞁䝇䝍䞁䝇䛷ฎ⌮䜢䛩䜛 
• 㒊ୗ䛸䛔䛖ᴫᛕ(䜽䝷䝇)䛷䛿䛺䛟㒊ୗ䛾䖣䖣(䜲䞁䝇䝍 
䞁䝇)䛜௙஦䜢䛩䜛 
Python 
ධ㛛 
[ෆ㒊ฎ⌮] 
௙஦䜢䛩䜛 
201 
[䝯䝑䝉䞊䝆] 
䛣䛾௙஦䜢䛧䛶䛟䛰䛥䛔 
[⤖ᯝ] 
䜽䝷䝇: 
ୖྖ 
ᡂᯝ≀䛿䛣䜜䛷䛩䚹 
䜲䞁䝇䝍䞁䝇: 
BOB 
䜽䝷䝇: 
㒊ୗ 
䜲䞁䝇䝍䞁䝇: 
TOM
䜽䝷䝇䛸䜲䞁䝇䝍䞁䝇䛾㛵ಀ 
• 䜽䝷䝇: 
䜲䞁䝇䝍䞁䝇䜢స䜛䛯䜑䛾㞮ᙧ 
• 䜲䞁䝇䝍䞁䝇: 
䜽䝷䝇䛛䜙స䜙䜜䜛䜸䝤䝆䜵䜽䝖 
• ᆺ(䜽䝷䝇) 
䛸䛭䛾್(䜲䞁䝇䝍䞁䝇)䛸䛔䛖⌮ゎ䛷OK 
Python 
ධ㛛 
202 
䜽䝷䝇 
䜲䞁䝇䝍䞁䝇໬ 
䜲䞁䝇䝍䞁䝇 
ᆺ(Class) 
String 
್(Instance) 
Hello 
Python, 
Hello 
Cisco
䝁䞁䝫䝆䝅䝵䞁 
• 䜽䝷䝇䛿ู䛾䜽䝷䝇(䜲䞁䝇䝍䞁䝇)䜢ᣢ䛴䛣䛸䛜䛒䜛 
Python 
ධ㛛 
203 
䜽䝷䝇㌴䛿 
-­‐ 
䜶䞁䝆䞁䜽䝷䝇䜢1䜲䞁䝇䝍䞁䝇 
-­‐ 
䝍䜲䝲䜽䝷䝇䜢4䜲䞁䝇䝍䞁䝇 
-­‐ 
䝝䞁䝗䝹䜽䝷䝇䜢1䜲䞁䝇䝍䞁䝇 
ᣢ䛳䛶䛔䜛
䜽䝷䝇䛾సᡂ 
Python 
ධ㛛 
204
䜽䝷䝇䛾ᐉゝ 
• ௨ୗ䛾せ௳䜢‶䛯䛩䜽䝷䝇䜢స䜛 
Python 
ධ㛛 
205 
䜽䝷䝇: 
MyClass 
ᒓᛶ: 
䜲䞁䝇䝍䞁䝇ኚᩘ 
string: 
ಖᣢ䛩䜛ᩥᏐิ 
ᒓᛶ: 
䝯䝋䝑䝗 
set_string: 
string䜢䝉䝑䝖 
get_string: 
string䜢ྲྀᚓ 
double: 
string䜢⧞䜚㏉䛩 
䝕䞊䝍䛸䛧䛶ᩥᏐิ䜢ෆ㒊䛻ᣢ䛴 
䛭䛾ᩥᏐิ䜢タᐃ䛩䜛䝯䝋䝑䝗 
䛭䛾ᩥᏐิ䜢ྲྀᚓ䛩䜛䝯䝋䝑䝗 
ᩥᏐิ䜢䛂ᩥᏐิ+ᩥᏐิ䛃䛻䛩䜛䝯䝋䝑䝗
䜽䝷䝇䛾ᐉゝ 
• 䜽䝷䝇䛾ᐉゝ䛿 
class 
䜽䝷䝇ྡ: 
• 䝯䝋䝑䝗䛾ᐉゝ: 
➨୍ᘬᩘ䛜㻌self 
䛸䛧䛶ᐃ⩏ 
• ᒓᛶ䜈䛾䜰䜽䝉䝇䛿㻌self.ᒓᛶ㻌䛸䛩䜛 
Python 
ධ㛛 
206 
class 
MyClass: 
string 
= 
 
def 
set_string(self, 
string): 
self.string 
= 
string 
def 
get_string(self): 
return 
self.string 
def 
double(self): 
self.string 
*= 
2 
class 
䛾ᐉゝ 
䝯䝋䝑䝗䛾ᐉゝ 
➨୍ᘬᩘ䜢 
self 
䛸䛩䜛䛾䛿 
python 
䛾䝹䞊䝹 
class 
䛜ಖᣢ䛩䜛䝕䞊䝍䛾ᐉゝ 
ᒓᛶ䜈䛾䜰䜽䝉䝇 
self.ᒓᛶ㻌䛸䛩䜛 
(䛭䛖䛧䛺䛔䛸㛵ᩘ䛰䛡䛾䝻䞊䜹䝹ኚᩘ䛸䛺䜛)
䜲䞁䝇䝍䞁䝇䛾฼⏝ 
• 䜽䝷䝇ྡ() 
䛷䜲䞁䝇䝍䞁䝇໬ 
• 䝯䝋䝑䝗䜢࿧䜃ฟ䛩㝿䛿➨୍ᘬᩘ(self)䛿ᣦᐃ䛧䛺䛔 
Python 
ධ㛛 
207 
class 
MyClass: 
string 
= 
 
def 
set_string(self, 
string): 
self.string 
= 
string 
def 
get_string(self): 
return 
self.string 
def 
double(self): 
self.string 
*= 
2 
# 
䜲䞁䝇䝍䞁䝇໬ 
 
mc 
= 
MyClass() 
# 
䝯䝋䝑䝗࿧䜃ฟ䛧 
 
mc.set_string(hello 
) 
 
print(mc.get_string()) 
hello 
 
mc.double() 
 
print(mc.get_string()) 
hello 
hello
₇⩦ 
• class 
Counter 
䜢సᡂ 
• ෆ㒊䛻䜹䜴䞁䝍䜢ᣢ䛴䚹ึᮇ್䛿1 
• get_value 
䝯䝋䝑䝗䛷⌧ᅾ䛾䜹䜴䞁䝍್䜢㏉䛩 
• count_up 
䝯䝋䝑䝗䛷⌧ᅾ䛾䜹䜴䞁䝍್䜢 
+1 
䛩䜛 
• clear_counter 
䝯䝋䝑䝗䛷䜹䜴䞁䝍䜢1䛻ᡠ䛩 
Python 
ධ㛛 
208
䝁䞁䝇䝖䝷䜽䝍䛾฼⏝ 
• 䝁䞁䝇䝖䝷䜽䝍: 
䜲䞁䝇䝍䞁䝇సᡂ᫬䛻࿧䜃ฟ䛥䜜䜛䝯䝋䝑䝗 
• 䝁䞁䝇䝖䝷䜽䝍䛾ᘬᩘ䛿䜲䞁䝇䝍䞁䝇సᡂ᫬䛻ᣦᐃ 
Python 
ධ㛛 
209 
class 
MyClass: 
string 
= 
 
def 
__init__(self, 
string): 
print(__init__ 
is 
called) 
self.string 
= 
string 
def 
set_string(self, 
string): 
self.string 
= 
string 
def 
get_string(self): 
return 
self.string 
def 
double(self): 
self.string 
*= 
2 
# 
䜲䞁䝇䝍䞁䝇໬ 
 
mc 
= 
MyClass(python 
) 
__init__ 
is 
called 
# 
䝯䝋䝑䝗࿧䜃ฟ䛧 
 
print(mc.get_string()) 
python
₇⩦ 
• class 
Counter 
䜢ᨵⰋ 
• ෆ㒊䛻䜹䜴䞁䝍䜢ᣢ䛴䚹ึᮇ್䛿䝁䞁䝇䝖䝷䜽䝍䛷ᣦᐃ 
• clear_counter 
䝯䝋䝑䝗䛷䜹䜴䞁䝍䜢ึᮇ್䛻ᡠ䛩 
Python 
ධ㛛 
210
⿵㊊)ືⓗ䛺䝯䞁䝞ኚᩘ 
• ືⓗ䛻䝯䞁䝞せ⣲䜢㏣ຍ䛷䛝䜛 
• Java䛸䛿␗䛺䜛䛾䛷せὀព!! 
Python 
ධ㛛 
211 
class 
MyClass: 
def 
func(self): 
print(func) 
 
mc 
= 
MyClass() 
 
mc.a 
= 
Hello 
World 
 
mc.a 
'Hello 
World' 
ᐃ⩏䛥䜜䛶䛔䛺䛔 
䝯䞁䝞ኚᩘ 
a 
䛻௦ධ 
䝯䞁䝞ኚᩘ 
a 
䛿 
ᐃ⩏䛥䜜䛶䛔䛺䛔
䜽䝷䝇䝃䞁䝥䝹 
Python 
ධ㛛 
212 
# 
bingo.py 
import 
random 
class 
BingoMachine: 
ballList 
= 
[] 
def 
__init__(self, 
min, 
max): 
self.ballList 
= 
range(min, 
max 
+ 
1) 
def 
getBall(self): 
if(len(self.ballList) 
== 
0): 
return 
-­‐1 
index 
= 
int(random.uniform(0, 
len(self.ballList))) 
return 
self.ballList.pop(index) 
# 
ᐇ⾜⤖ᯝ 
 
from 
bingo 
import 
* 
 
b 
= 
BingoMachine(1,5) 
 
b.getBall() 
1 
 
b.getBall() 
5 
 
b.getBall() 
2 
 
b.getBall() 
3 
 
b.getBall() 
4 
 
b.getBall() 
-­‐1 
䝡䞁䝂䝬䝅䞊䞁 
ᩘᏐ㻌n 
– 
m 
䛾㛫䛾ᩘᏐ䜢䝷䞁䝎䝮䛻㏉䛩 
୍ᗘ㏉䛧䛯್䛿㏉䛥䛺䛔
䜽䝷䝇䜎䛸䜑1 
• 䝯䝋䝑䝗䛾➨୍ᘬᩘ䛿⮬ศ⮬㌟䛾䜸䝤䝆䜵䜽䝖 
• 䝯䞁䝞ኚᩘ䜔䝯䝋䝑䝗䜢࿧䜆䛻䛿䛣䛾䜸䝤䝆䜵䜽䝖䛾ᣦᐃ䛜ᚲせ 
䝯䞁䝞ኚᩘ䜔䝯䝋䝑䝗䜢 
࿧䜃ฟ䛧䛶䛔䜛 
Python 
ධ㛛 
213 
class 
MyClass: 
var 
= 
0 
def 
method1(self): 
self.var 
= 
1 
print(method1) 
def 
method2(self): 
print(self.var) 
self.method1() 
print(method2) 
䝯䝋䝑䝗ᐃ⩏䛾➨୍ᘬᩘ䛜 
self 
䛻䛺䛳䛶䛔䜛 
self.ྡ๓㻌䛸䛧䛶 
䝯䝋䝑䝗࿧䜃ฟ䛧䛿ᐃ⩏䛾➨୍ᘬᩘ䛿㣕䜀䛩
䜽䝷䝇䜎䛸䜑2 
• 䜲䞁䝇䝍䞁䝇໬:䚷䜽䝷䝇ྡ(䝁䞁䝇䝖䝷䜽䝍䛻Ώ䛩ᘬᩘ) 
• 䝁䞁䝇䝖䝷䜽䝍: 
Python 
ධ㛛 
214 
__init__ 
class 
MyClass2: 
var 
= 
0 
def 
__init__(self, 
var): 
self.var 
= 
var 
def 
method(self, 
var): 
print(str(self.var) 
+ 
 
 
+ 
str(var)) 
print(method) 
m2 
= 
MyClass2(5) 
m2.method(8) 
-­‐-­‐ 
5 
8 
method 
䛸䛔䛖䝯䝋䝑䝗ྡ
䝗䝷䜽䜶䛷⌮ゎ䛩䜛䜸䝤䝆䜵䜽䝖ᣦྥ 
Python 
ධ㛛 
215
₇⩦䛾┠ⓗ 
Python 
ධ㛛 
216 
• ኱䛝䛺䝥䝻䜾䝷䝮䛾సᡂ᪉ἲ䜢Ꮫ䜆 
– 」ᩘ䛾䜽䝷䝇䛾㐃ᦠ 
– 䛹䛾䜘䛖䛻䛧䛶䝁䞊䝗䜢ᗈ䛢䛶䛔䛟䛛 
– 㢟ᮦ䛿䝗䝷䜽䜶1
䝗䝷䝂䞁䜽䜶䝇䝖 
඲䛶䜢స䜛䛾䛿↓⌮䛺䛾䛷 
Python 
Map 
ධ㛛 
๓ᚋᕥྑ䛻ື䛟 
䛯䛰䛧䚸䝬䝑䝥䛾እ䛻䛿ฟ䛺䛔 
⏫ே䛻ヰ䛧䛛䛡䜛 
217 
୺ேබ 
䛾䜏ᐇ⿦䛩䜛 
⏫ே 
୺ேබ䛻ᛂ⟅䛩䜛
AA∧䝗䝷䝂䞁䜽䜶䝇䝖(᏶ᡂᅗ) 
๓ᚋᕥྑ䛻ື䛟 
䛯䛰䛧䚸䝬䝑䝥䛾እ䛻䛿ฟ䛺䛔 
⏫ே䛻ヰ䛧䛛䛡䜛 
ྥ䛔䛶䛔䜛ྥ䛝䛷䜰䜲䝁䞁䛜ኚ䜟䜛 
Python 
⏫ே 
ධ㛛 
218 
a:Left, 
s:Right, 
w:Up, 
x:Down 
d:Talk, 
Ctrl-­‐C:Exit 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
| 
| 
| 
| 
| 
B 
| 
| 
| 
| 
M| 
| 
| 
| 
| 
| 
P 
| 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
####################### 
Hello 
I'm 
Bukiya 
####################### 
୺ேබ 
୺ேබ䛻ᛂ⟅䛩䜛 
䝯䝑䝉䞊䝆ḍ 
䝬䝑䝥
సᡂ䝥䝻䜾䝷䝮䛾せ⣲ 
䝬䝑䝥(඲య䛾⟶⌮) 
Python 
ධ㛛 
219 
⏫ே(」ᩘ) 
୺ேබ 
(䜲䝧䞁䝖ฎ⌮) 
ධຊ 
䜻䞊ධຊ⟶⌮䜽䝷䝇 
(ᐇ⿦῭䜏)
䜽䝷䝇⏫ே 
Python 
ධ㛛 
220 
䜽䝷䝇: 
⏫ே 
䝯䞁䝞ኚᩘ 
xᗙᶆ 
yᗙᶆ 
እぢ䛾✀㢮 
䝉䝸䝣 
䝯䝋䝑䝗: 
respond: 
୺ேබ䛻ᛂ⟅䛩䜛 
እぢ 
B: 
Ṋჾᒇ 
M: 
ၟே 
P: 
⚄∗
䜽䝷䝇⏫ே䛾䜲䞁䝇䝍䞁䝇 
Python 
ධ㛛 
221 
䜲䞁䝇䝍䞁䝇㻌⏫ேA 
䝯䞁䝞ኚᩘ 
x: 
8 
y: 
4 
እぢ: 
⏨A 
䝉䝸䝣: 
䛣䜣䛻䛱䛿 
䝯䝋䝑䝗: 
respond: 
୺ேබ䛻ᛂ⟅䛩䜛 
䜲䞁䝇䝍䞁䝇㻌⏫ேB 
䝯䞁䝞ኚᩘ 
x: 
4 
y: 
2 
እぢ: 
㨱ἲ౑䛔 
䝉䝸䝣: 
䛔䛔ኳẼ䛨䜓䛾䛖 
䝯䝋䝑䝗: 
respond: 
୺ேබ䛻ᛂ⟅䛩䜛
୺ேබ 
Python 
ධ㛛 
222 
୺ேබ䛾䜰䜲䝁䞁 
 
: 
ᕥྥ䛝 
 
: 
ྑྥ䛝 
^ 
: 
ୖྥ䛝 
V 
: 
ୗྥ䛝 
䜽䝷䝇: 
୺ேබ 
䝯䞁䝞ኚᩘ 
xᗙᶆ 
yᗙᶆ 
ྥ䛔䛶䛔䜛᪉ྥ 
䝉䝸䝣 
䝯䝋䝑䝗: 
run: 
䜻䞊ධຊ䜢ᚅ䛴(ᶆ‽ධຊ䛾↓㝈䝹䞊䝥) 
move: 
䜻䞊ධຊ䛷ୖୗᕥྑ䛻ື䛟 
talk: 
ྥ䛔䛶䛔䜛᪉ྥ䛾⏫ே䛸ヰ䛩㻌
䝬䝑䝥 
Python 
ධ㛛 
223 
䜽䝷䝇: 
Map 
䝯䞁䝞ኚᩘ 
-­‐ 
୺ேබ䛾䜲䞁䝇䝍䞁䝇 
-­‐ 
⏫ே䛯䛱䛾䜲䞁䝇䝍䞁䝇 
-­‐ 
䝬䝑䝥᝟ሗ 
䝯䝋䝑䝗: 
-­‐ 
render: 
䝬䝑䝥䜢᭩䛝ฟ䛩 
-­‐ 
୺ேබ䛸⏫ே䜢⟶⌮䛩䜛䝯䝋䝑䝗⩌ 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
| 
| 
| 
| 
| 
B 
| 
| 
| 
| 
P 
| 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
እᯟ 
୺ேබ 
⏫ே(」ᩘ)
䜽䝷䝇䛸䜽䝷䝇䛾┦஫㛵ಀ 
• ༑Ꮠ䜻䞊䜢᧯స䛩䜛䛸ຬ⪅䛜ື䛟 
• ᆅᅗ䛿䛭䛾᝟ሗ䜢཯ᫎ䛧䛺䛡䜜䜀䛺䜙䛺䛔 
Python 
ධ㛛 
224 
䜲䞁䝇䝍䞁䝇 
ຬ⪅ 
䜲䞁䝇䝍䞁䝇 
Map 
ධຊ(keyInput) 
⮬ศ䛾X,Y᝟ሗ䜢 
䜰䝑䝥䝕䞊䝖 
ᥥ⏬䜢౫㢗 
⏬㠃ฟຊ
䝬䝑䝥ᥥ⏬䛾඲య䛾ὶ䜜 
③ 
④ 
Python 
ධ㛛 
225 
Class: 
Field 
ኚᩘ: 
hero 
ኚᩘ: 
peopleMap 
ኚᩘ: 
ᆅᙧ᝟ሗ 
renderMap() 
ᥥ⏬(render) 
ᆅᙧ᝟ሗ 
+ 
ຬ⪅ 
+ 
⏫ே㐩 
Class: 
Hero 
move() 
talk() 
getXY() 
getImage() 
keyInputEvent() 
Class:䚷People 
respond() 
getImage() 
ධຊ(keyInput) 
x,yᗙᶆ䜔ぢ䛯┠ 
䛺䛹䛾᝟ሗ䜢ྲྀᚓ 
䝕䜱䝇䝥䝺䜲䛻ฟຊ 
① 
② 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
| 
| 
| 
| 
| 
B 
| 
| 
| 
| 
P 
| 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+
₇⩦1 
Python 
ධ㛛 
226
䝬䝑䝥 
• ఱ䜒䛺䛔䝬䝑䝥䜢ᥥ⏬ 
• ⦪䛸ᶓ䛾㛗䛥䜢ᣦᐃྍ⬟ 
• ⾜(row)ᣦྥ䛷ᥥ⏬䛩䜛 
227 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
| 
| 
| 
| 
| 
| 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
4㝮䛿㻌䛂+䛃 
yᗙᶆ0, 
᭱኱್䛿㻌䛂–䛃 
xᗙᶆ0, 
᭱኱್䛿㻌䛂|䛃 
䛭䜜௨እ䛿✵ⓑ 
class 
DQMap: 
max_x 
= 
0 
max_y 
= 
0 
def 
__init__(self, 
max_xy): 
(self.max_x, 
self.max_y) 
= 
max_xy 
def 
render_map(self): 
map_string_list 
= 
[] 
for 
y 
in 
range(self.max_y): 
for 
x 
in 
range(self.max_x): 
if(y 
== 
0 
or 
y 
== 
self.max_y 
-­‐ 
1): 
if(x 
== 
0 
or 
x 
== 
self.max_x 
-­‐ 
1): 
map_string_list.append(+) 
else: 
map_string_list.append(-­‐) 
else: 
if(x 
== 
0 
or 
x 
== 
self.max_x 
-­‐ 
1): 
map_string_list.append(|) 
else: 
map_string_list.append( 
) 
map_string_list.append(n) 
map_string 
= 
.join(map_string_list) 
print(map_string.rstrip())
୺ேබ 
• 䜻䞊ධຊ䛷୺ேබ䛾x,yᗙᶆ䛸䛂ྥ䛝䛃䜢䜰䝑䝥䝕䞊䝖䛩䜛 
• Ctrl-­‐C 
䛷䝥䝻䜾䝷䝮䜢⤊஢䛩䜛 
228 
import 
getch, 
sys 
class 
Hero: 
x 
= 
0 
y 
= 
0 
direccon 
= 
N 
def 
__init__(self, 
x, 
y): 
self.x 
= 
x 
self.y 
= 
y 
self.direccon 
= 
N 
def 
run(self): 
gc 
= 
getch.Getch() 
while(True): 
keycode 
= 
ord(gc()) 
self.move(keycode) 
print(x:{}, 
y:{}, 
dir:{}.format(self.x, 
self.y, 
self.direccon)) 
if(keycode 
== 
3): 
sys.exit() 
def 
move(self, 
keycode): 
if(keycode 
== 
97): 
#Le 
self.x 
-­‐= 
1 
self.direccon 
= 
W 
elif(keycode 
== 
115): 
#Right 
self.x 
+= 
1 
self.direccon 
= 
E 
elif(keycode 
== 
119): 
#Up 
self.y 
-­‐= 
1 
self.direccon 
= 
N 
elif(keycode 
== 
122): 
#Down 
self.y 
+= 
1 
self.direccon 
= 
S 
hero 
= 
Hero(10,10) 
hero.run()
䝬䝑䝥䛸୺ேබ(ㄢ㢟) 
• 䝬䝑䝥䛾୰䛻୺ேබ䜢ᥥ⏬ 
• ྥ䛔䛶䛔䜛᪉ྥ䛷୺ேබ䛾䜰䜲䝁䞁䜢ኚ䛘䜛 
• 䝬䝑䝥䛾⠊ᅖእ䛻୺ேබ䛜䜰䜽䝉䝇䛧䛺䛔 
• 䝥䝻䜾䝷䝮䛾㉳Ⅼ䛿㻌main.py 
229 
ᐇ⿦䛾䝠䞁䝖 
DQMap: 
-­‐ 
DQMap 
䛜 
Hero 
䛾䜲䞁䝇䝍䞁䝇䜢ᣢ䛴 
-­‐ 
୺ேබ䛸ྠ䛨㻌x, 
y 
ᗙᶆ䛾ሙྜ䛿୺ேබ䛾䜰䜲䝁䞁䜢⾲♧ 
(୺ேබ䛾䜲䞁䝇䝍䞁䝇䛛䜙䜰䜲䝁䞁䜢ྲྀᚓ) 
Hero: 
-­‐ 
᪂䛧䛔ᗙᶆ䛜 
Map 
⠊ᅖෆ䛷䛒䜛ሙྜ䛰䛡䚸x,y 
䜢䜰䝑䝥䝕䞊䝖 
(⠊ᅖእ䜈䛾䜰䜽䝉䝇䛿㻌x,y 
䜢䜰䝑䝥䝕䞊䝖䛧䛺䛔) 
-­‐ 
⾲♧䛩䜛୺ேබ䛾䜰䜲䝁䞁䛿୺ேබ䛾ྥ䛝䛷Ỵ䜎䜛 
(ྥ䛝䛾ᛂ䛨䛯䜰䜲䝁䞁䜢㏉䛩䝯䝋䝑䝗䜢ᐇ⿦)
₇⩦2 
Python 
ධ㛛 
230
⏫ே䛾ᐇ⿦ 
Python 
ධ㛛 
231 
class 
People: 
icon 
= 
 
x 
= 
0 
y 
= 
0 
comment 
= 
 
def 
__init__(self, 
icon, 
x, 
y, 
comment): 
self.icon 
= 
icon 
self.x 
= 
x 
self.y 
= 
y 
self.comment 
= 
comment 
def 
get_icon(self): 
return 
self.icon 
def 
get_xy(self): 
return 
(self.x, 
self.y) 
def 
get_response(self): 
return 
self.comment 
⏫ே䛿 
-­‐ 
䜰䜲䝁䞁(୍ᩥᏐ) 
-­‐ 
x,y 
-­‐ 
䝉䝸䝣 
䜢䝁䞁䝇䝖䝷䜽䝍䛷タᐃ 
䛂䝀䝑䝍䞊䝯䝋䝑䝗䛃䜢ᐇ⿦ 
䚷䈜 
䝟䝷䝯䞊䝍䜢ྲྀᚓ䛩䜛䛯䜑䛾䝯䝋䝑䝗
䝬䝑䝥䛸⏫ே䛾ᐇ⿦ 
Python 
ධ㛛 
232 
class 
DQMap: 
... 
people_map 
= 
{} 
def 
__init__(self, 
max_xy, 
hero_xy, 
people_paramList): 
.... 
for 
params 
in 
people_paramList: 
(icon, 
x, 
y, 
comment) 
= 
params 
self.people_map[(x,y)] 
= 
people.People(icon, 
x, 
y, 
comment) 
def 
render_map(self, 
comment): 
map_string_list 
= 
[] 
(hx, 
hy) 
= 
self.hero.get_xy() 
for 
y 
in 
range(self.max_y): 
for 
x 
in 
range(self.max_x): 
if((x,y) 
== 
(hx, 
hy)): 
map_string_list.append(self.hero.get_icon()) 
elif(self.people_map.has_key((x, 
y))): 
p 
= 
self.people_map[(x, 
y)] 
map_string_list.append(p.get_icon()) 
䝁䞁䝇䝖䝷䜽䝍䛷⏫ே䛾 
᝟ሗ䜢䝸䝇䝖ᙧᘧ䛷ᣦᐃ 
⏫ே䛾䜲䞁䝇䝍䞁䝇䜢సᡂ 
(x,y)䜢䜻䞊䛻䛧㎡᭩䛷⏫ே䜢⟶⌮ 
ᥥ⏬䛾㝿䛻䚸x, 
y 
䛻⏫ே䛜䛔䜛䛛☜ㄆ 
䜒䛧䛔䜜䜀⏫ே䜢ᥥ⏬䛩䜛
୺ேබ䛸⏫ே䛾ᐇ⿦(ㄢ㢟) 
• ୺ேබ䛜⏫ே䛸⿕䜙䛺䛔䜘䛖䛻䛩䜛 
Python 
ධ㛛 
233 
Hint 
ḟ䛻ື䛣䛖䛸䛩䜛ඛ䛻⏫ே䛜䛔䜛䛛☜ㄆ 
䛔䜛ሙྜ䛿୺ேබ䛾 
x, 
y 
䜢䜰䝑䝥䝕䞊䝖䛧䛺䛔
₇⩦3 
Python 
ධ㛛 
234
఍ヰᶵ⬟䛾ᐇ⿦ 
• ୺ேබ䛜ྥ䛔䛶䛔䜛᪉ྥ䛻⏫ே䛜䛔䜛䛛☜ㄆ 
• ⏫ே䛜䛔䜜䜀⏫ே䛾䝺䝇䝫䞁䝇䜢ྲྀᚓ䛧⾲♧ 
• 䛔䛺䛡䜜䜀ㄡ䜒䛔䛺䛔᪨䛾䝯䝑䝉䞊䝆䜢⾲♧ 
• DQMap䛾䝯䝋䝑䝗render_map䛻䝁䝯䞁䝖䜢Ώ䛧䛶ᥥ⏬(ᐇ⿦῭) 
Python 
ධ㛛 
235 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
| 
| 
| 
| 
| 
B 
| 
| 
| 
| 
M| 
| 
| 
| 
| 
| 
P 
| 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
####################### 
Hello 
I'm 
Merchant 
####################### 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
| 
| 
| 
| 
| 
B 
| 
| 
^ 
| 
| 
M| 
| 
| 
| 
| 
| 
P 
| 
+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 
####################### 
No 
one 
exists 
on 
that 
way 
#######################
䛭䛾௚ 
Python 
ධ㛛 
236
__name__ 
• __name__䚷 
• __name__ 
䛜㻌__main__ 
䛾䛸䛝䛿㉳Ⅼ 
Python 
import 
uclity 
def 
func(): 
print(func 
on 
main.py) 
print(global 
on 
main.py) 
func() 
uclity.func() 
if 
__name__ 
== 
__main__: 
print(__name__ 
is 
__main__ 
on 
main.py) 
ධ㛛 
237 
def 
func(): 
print(func() 
on 
uclity.py) 
print(global 
on 
uclity.py) 
func() 
if 
__name__ 
== 
__main__: 
print(__name__ 
is 
__main__ 
on 
uclity.py) 
main.py 
uclity.py
__name__ 
Python 
ධ㛛 
238 
def 
func(): 
print(func() 
on 
uclity.py) 
print(global 
on 
uclity.py) 
func() 
if 
__name__ 
== 
__main__: 
True 
print(__name__ 
is 
__main__ 
on 
uclity.py) 
# 
python 
uclity.py 
global 
on 
uclity.py 
func() 
on 
uclity.py 
__name__ 
is 
__main__ 
on 
uclity.py 
ᐇ⾜⤖ᯝ
__name__ 
Python 
ධ㛛 
239 
import 
uclity 
def 
func(): 
print(func 
on 
main.py) 
print(global 
on 
main.py) 
func() 
uclity.func() 
if 
__name__ 
== 
__main__: 
print(__name__ 
is 
__main__ 
on 
main.py) 
def 
func(): 
print(func() 
on 
uclity.py) 
print(global 
on 
uclity.py) 
func() 
if 
__name__ 
== 
__main__: 
print(__name__ 
is 
__main__ 
on 
uclity.py) 
main.py 
True 
uclity.py 
False 
#python 
main.py 
global 
on 
uclity.py 
func() 
on 
uclity.py 
global 
on 
main.py 
func 
on 
main.py 
func() 
on 
uclity.py 
__name__ 
is 
__main__ 
on 
main.py 
ᐇ⾜⤖ᯝ
䝁䝢䞊䛸ཧ↷ 
• 䝁䝢䞊: 
␗䛺䜛䜸䝤䝆䜵䜽䝖 
• ཧ↷: 
ྠ୍䜸䝤䝆䜵䜽䝖䜢」ᩘ⟠ᡤ䛷฼⏝ 
• ཧ↷䛻ᑐ䛧䛶䛿๪స⏝䛾ὀព䛜ᚲせ 
Python 
ධ㛛 
240 
 
a 
= 
[1,2,3,4] 
 
b 
= 
a 
 
b.append(5) 
 
a 
[1, 
2, 
3, 
4, 
5]
PEXPECT 
Python 
ධ㛛 
241
Pexpect 
• EXPECT 
䜢 
Python 
䛷౑䛖䛯䜑䛾䝟䝑䜿䞊䝆 
• Cisco䛾ᶵჾ䜔Linux䛸䛔䛳䛯CLI᧯స䜢ᚲせ䛸䛩䜛ᶵჾ䜢 
䝥䝻䜾䝷䝭䞁䜾䛷つᐃ㏻䜚䛻ື䛛䛩䛯䜑䛻฼⏝ 
Python 
ධ㛛 
242 
Python 
Script
Expect 
• Linux䛺䛹䛾䜲䞁䝍䝷䜽䝔䜱䝤䛺CLI䜢ไᚚ䛩䜛 
䛯䜑䛾䝥䝻䜾䝷䝮 
• pexpect 
䛾ඖ䛻䛺䛳䛯䝥䝻䜾䝷䝮 
• Teraterm 
Macro 
䛻ఝ䛶䛔䜛 
Python 
ධ㛛 
243 
The 
Expect 
Home 
Page 
h7p://expect.sourceforge.net/ 
Expect 
Script
Expect 
䛾ືస 
• spawn: 
䝉䝑䝅䝵䞁䜢㛤ጞ 
• expect: 
ᮇᚅ䛩䜛ᶆ‽ධຊ(from 
䝸䝰䞊䝖)䜢ᚅ䛴 
• send: 
䝸䝰䞊䝖䛻ฟຊ䜢㏦䜛 
Python 
ධ㛛 
244 
dhcp-­‐10-­‐141-­‐56-­‐250:~ 
yuichi$ 
telnet 
-­‐l 
yuiito 
rws1 
Trying 
10.71.231.245... 
Connected 
to 
rws1.cisco.com. 
Escape 
character 
is 
'^]'. 
Password: 
cisco123 
Last 
login: 
Thu 
Jan 
23 
14:53:37 
from 
dhcp-­‐10-­‐141-­‐56-­‐250.cisco.com 
unknown 
terminal 
xterm-­‐256color 
unknown 
terminal 
xterm-­‐256color 
[yuiito@rws1 
yuiito]$ 
echo 
hello 
 
test 
[yuiito@rws1 
yuiito]$ 
ls 
snmpget.sh 
test 
㟷䛜䝸䝰䞊䝖䛛䜙䛾ᶆ‽ධຊ 
㉥䛜䝸䝰䞊䝖䜈䛾ᶆ‽ฟຊ
expect 
䝃䞁䝥䝹 
• Linux 
䜈䛾䝻䜾䜲䞁䛸᧯స 
Python 
ධ㛛 
245 
Password: 
cisco123 
yuiito$ 
echo 
hello 
 
test 
login-­‐to-­‐rws1.exp 
#! 
/usr/bin/expect 
spawn 
telnet 
-­‐l 
yuiito 
rws1 
expect 
Password: 
send 
cisco123n 
expect 
yuiito]$ 
send 
touch 
hellon 
interact 
࿧䜃ฟ䛧 
yuichi$ 
expect 
login-­‐to-­‐rws1.exp
Pexpect 
• pip 
䜢౑䛳䛶䜲䞁䝇䝖䞊䝹 
• 䝁䞊䝗㻌pexpect.py 
ྠ୍䝣䜷䝹䝎䛻㓄⨨(฼⏝) 
• import 
pexpect 
Python 
ධ㛛 
246
Pexpect 
䛾సἲ 
• spawn 
session 
= 
pexpect.spawn(telnet➼䛾䝁䝬䞁䝗) 
• expect 
session.expect(ධຊ䛥䜜䜛䛾䜢ᚅ䛴ᩥᏐิ) 
• sendline 
session.sendline(㏦ಙ䛩䜛ᩥᏐิ䚹⮬ື䛷ᨵ⾜ 
䝁䞊䝗䛜䛴䛟) 
Python 
ධ㛛 
247
pexpect 
• 䝬䝑䝏䛧䛯ᩥᏐิᙜ䜢ᢳฟ 
Python 
ධ㛛 
248 
session.aer: 
䝬䝑䝏䛧䛯ᩥᏐิ 
session.before: 
䝬䝑䝏䛧䛯ᩥᏐิ䜎䛷䛾ᩥᏐิ 
session.buffer:
pexpect 
䛾䝃䞁䝥䝹 
• rws1 
䛻䝻䜾䜲䞁䛧䛶䝣䜯䜲䝹㻌pexpect 
䜢సᡂ 
Python 
ධ㛛 
249 
import 
pexpect 
child 
= 
pexpect.spawn(telnet 
-­‐l 
yuiito 
rws1) 
child.expect(Password:) 
child.sendline(cisco123) 
child.expect(yuiito]$) 
child.sendline(touch 
pexpect)
pexpext䛾䝃䞁䝥䝹 
• Nexus䛾タᐃ䜢䝣䜯䜲䝹ྡ䜢᪥᫬䛻䛧䛶FTP䝃䞊䝞䛻ಖᏑ 
Python 
ධ㛛 
250 
import 
pexpect, 
datecme 
def 
getLines(string): 
lines 
= 
lines 
= 
[x.strip() 
for 
x 
in 
string.split(n)] 
return 
filter(lambda 
x: 
x!= 
'', 
lines) 
def 
getHostName(exp): 
exp.sendline(show 
hostname) 
exp.expect(#) 
return 
getLines(exp.before)[1] 
def 
getToDay(): 
d 
= 
datecme.datecme.today() 
return 
{}_{}_{}-­‐{}_{}_{}.format(d.year, 
d.month, 
d.day, 
d.hour, 
d.minute, 
d.second) 
def 
makeFileName(exp): 
hostName 
= 
getHostName(exp) 
today 
= 
getToDay() 
return 
{}-­‐{}.conf.format(hostName, 
today) 
def 
login(exp, 
password): 
exp.expect(Password:) 
exp.sendline(password) 
exp.expect(#) 
exp.sendline(terminal 
length 
0) 
exp.expect(#) 
def 
saveFile(exp, 
saveFrom, 
saveTo, 
vrf, 
username, 
password): 
command 
= 
copy 
{} 
{}.format(saveFrom, 
saveTo) 
exp.sendline(command) 
exp.expect(Enter 
vrf) 
exp.sendline(vrf) 
exp.expect(Enter 
username:) 
exp.sendline(username) 
exp.expect(Password:) 
exp.sendline(password) 
exp.expect(#) 
child 
= 
pexpect.spawn(telnet 
-­‐l 
admin 
10.71.156.229) 
login(child, 
cisco) 
saveTo 
= 
p://10.71.224.115/yuiito/ 
+ 
makeFileName(child) 
saveFile(child, 
running-­‐config, 
saveTo, 
management, 
anonymous, 
) 
䝣䜯䜲䝹ྡ䜔䝁䝬䞁䝗䛾⏕ᡂ䛜 
python 
௵䛫 
child.close()
pexpect 
• show䝁䝬䞁䝗䜔タᐃኚ᭦䛿䜋䜌ྠ䛨䜘䛖䛺ᡭ 
㡰䛷ᐇ⿦䛥䜜䜛 
• ⥅ᢎ䜔䜽䝻䞊䝆䝱䜢౑䛖䛣䛸䛷ྛฎ⌮䜢ᐇ⿦ 
䛩䜜䜀⡆༢䛻ᶵ⬟䜢ቑ䜔䛫䜛 
Python 
ධ㛛 
251
₇⩦ 
• Nexus䛾⌧ᅾ䛾䝞䞊䝆䝵䞁䜢ྲྀᚓ䛩䜛䝥䝻䜾 
䝷䝮䜢సᡂ䛩䜛 
Python 
ධ㛛 
Nexus 
ip: 
1.110.8.1 
user-­‐name: 
admin 
pass: 
cisco 
252 
㋃䜏ྎ 
ip: 
10.71.224.172 
user-­‐name: 
guest 
pass: 
c1sco123
Python 
ධ㛛 
253
Python 
ධ㛛 
Learning 
to 
Program 
For 
Cisco 
Network 
Engineers 
Day 
4/4 
Japan 
TAC 
DC 
Team. 
Yuichi 
Ito
Agenda 
Day4 
• Python䛾䝥䝻䜾䝷䝮㓄ᕸᡭἲ 
• Tkinter 
(GUI) 
• Tkinter 
䛷Ꮫ䜆⥅ᢎ 
• ౛እฎ⌮ 
• NETWORK䝥䝻䜾䝷䝭䞁䜾 
• Pexpect 
Python 
ධ㛛 
255
䝥䝻䜾䝷䝮㓄ᕸᡭἲ 
Python 
ධ㛛 
256
䝋䞊䝇䝁䞊䝗䛾㓄ᕸ 
• 㛗ᡤ: 
㓄ᕸ䛜⡆༢ 
• ▷ᡤ: 
䝷䜲䝤䝷䝸䛻౫Ꮡ䚹䝁䞊䝗䛜୸ぢ䛘 
Python 
ධ㛛 
257 
python 
䝋䞊䝇 
䝁䞊䝗 
python 
䝋䞊䝇 
䝁䞊䝗 
python 
䝋䞊䝇 
䝁䞊䝗 
㛤Ⓨ⪅ 
฼⏝⪅ 
฼⏝⪅
䝞䜲䝘䝸໬䛧䛶ᐇ⾜⎔ቃ䛤䛸㓄ᕸ 
• 㛗ᡤ: 
฼⏝⪅䛜౑䛔䜔䛩䛔䚹 
• ▷ᡤ: 
సᡂ䛜㞴䛧䛔 
.exe 
.app 
Python 
ධ㛛 
258 
python 
䝋䞊䝇 
䝁䞊䝗 
㛤Ⓨ⪅ 
฼⏝⪅(Windows) 
฼⏝⪅(Mac)
㛵ᩘ(䝯䝋䝑䝗)䛾ᘬᩘ 
Python 
ධ㛛 
259
㛵ᩘ䛸䝟䝷䝯䞊䝍 
• 㛵ᩘ(䝯䝋䝑䝗)䛻䛿䝕䝣䜷䝹䝖್䜢౑䛘䜛 
• ࿧䜃ฟ䛧ഃ䛷ᘬᩘ䜢ዲ䛝䛺㡰ᗎ䛷ᣦᐃྍ⬟ 
Python 
ධ㛛 
260 
def 
func(a, 
b=python, 
c=cisco): 
print(hello 
{} 
{} 
{}.format(a,b,c)) 
func(world, 
PYTHON, 
CISCO) 
䚷# 
= 
hello 
world 
PYTHON 
CISCO 
func(world) 
# 
= 
hello 
world 
python 
cisco 
func(b=world, 
a=python) 
# 
= 
hello 
python 
world 
cisco 
ᘬᩘ 
b, 
c 
䛻䛿䝕䝣䜷䝹䝖್䜢ᣦᐃ 
ᘬᩘ㻌a,b,c 
䛻඲䛶௵ព䛾್䜢タᐃ 
ᘬᩘ 
a 
䛾䜏ᣦᐃ䛧䛶࿧䜃ฟ䛧 
௵ព䛾㡰ᗎ䛷ᘬᩘ䜢ᣦᐃ
GUI 
TKINTER 
Python 
ධ㛛 
261
GUI 
䛾Ꮫ⩦┠ⓗ 
• 䜰䝥䝸䜿䞊䝅䝵䞁䛻GUI୍⯡ⓗ 
• ぢ䛯┠䛻ศ䛛䜚䜔䛩䛔䛾䛷୰⣭⪅ྥ䛝 
• ⥅ᢎ䛾ᴫᛕ䛺䛹䜢⌮ゎ䛩䜛䛾䛻᭱㐺䛺㢟ᮦ 
Python 
ධ㛛 
262
Tkinter 
• Python 
䛾ᶆ‽ⓗ䛺GUI䝷䜲䝤䝷䝸 
• 䜲䞁䝇䝖䞊䝹䛺䛧䛻฼⏝ྍ⬟ 
• ௚䛾GUI䝷䜲䝤䝷䝸(Qt, 
wxpython)䛸ᴫᛕ䛜ఝ䛶䛔䜛 
Python 
ධ㛛 
263
Hello 
Tkinter 
Python 
ධ㛛 
264 
import 
Tkinter 
as 
tk 
font=(Helevecca, 
32, 
bold) 
label 
= 
tk.Label(text=Hello 
Cisco, 
font=font, 
bg=red) 
label.pack() 
label.mainloop()
Hello 
Tkinter 
• GUI 
సᡂ䛾ὶ䜜 
– Widget䛸࿧䜀䜜䜛䝟䞊䝒䜢ึᮇ໬ 
– 䝟䞊䝒䜢㓄⨨ 
– GUI䜰䝥䝸䜿䞊䝅䝵䞁䜢㉳ື 
Python 
ධ㛛 
265 
Widget 
䛿GUI䛾㒊ရ䚹 
䝪䝍䞁䜔䝔䜻䝇䝖䝪䝑䜽䝇䛺䛹䛾඲䛶䛾䝟䞊䝒䛿Widget
Hello 
Tkinter 
䛾ゎㄝ 
Python 
ධ㛛 
266 
import 
Tkinter 
as 
tk 
font=(Helevecca, 
32, 
bold) 
label 
= 
tk.Label(text=Hello 
Cisco, 
font=font, 
bg=red) 
label.pack() 
label.mainloop() 
Tkinter 
䛸䛔䛖䝰䝆䝳䞊䝹䜢䜲䞁䝫䞊䝖䚹࿧䜃ฟ䛧᫬䛿㻌tk 
䛸䛔䛖ู⛠䜢౑䛖 
Widget 
䛾㻌Label 
䜢సᡂ䛩䜛䚹ᘬᩘ䛸䛧䛶ᩥᏐิ䛸䝣䜷䞁䝖䚸⫼ᬒⰍ䜢ᣦᐃ 
pack() 
䝯䝋䝑䝗䛷㓄⨨ 
mainloop() 
䝯䝋䝑䝗䜢࿧䜃ฟ䛩䛣䛸䛷䜰䝥䝸䜿䞊䝅䝵䞁䜢ᐇ⾜
₇⩦1 
• 䝷䝧䝹䛻⾲♧䛩䜛ᩥᏐิ䜢 
Hello 
Python 
䛻ኚ᭦ 
• 䝣䜷䞁䝖䝃䜲䝈䜢 
44 
䛻䛩䜛 
• ⫼ᬒⰍ䜢 
blue 
䛻䛩䜛 
Ⓨᒎ 
• 䜰䞁䝎䞊䝷䜲䞁䜢௜䛡䜛 
• ᖜ䜢50䛻䛩䜛 
Python 
ධ㛛 
267 
An 
IntroducYon 
to 
Tkinter 
(Work 
in 
Progress) 
h7p://eot.org/tkinterbook/
Frame 
• Widget 
䜢㓄⨨䛩䜛䛯䜑䛾 
Widget 
• Frame 
in 
Frame 
䜒ྍ⬟(」㞧䛺ᵓᡂ䜢స䜜䜛) 
Python 
ධ㛛 
268 
Frame1 
Frame2 
Frame3 
widget 
1 
widget 
2 
widget 
3 
widget 
4 
widget 
5 
widget 
6 
Frame1 
| 
|-­‐-­‐ 
Frame 
2 
| 
| 
-­‐-­‐ 
Widget1 
| 
| 
-­‐-­‐ 
Widget2 
| 
| 
-­‐-­‐ 
Widget3 
| 
| 
-­‐-­‐ 
Widget4 
| 
| 
-­‐-­‐ 
Frame3 
| 
-­‐-­‐ 
Widget5 
| 
-­‐-­‐ 
Widget5
Hello 
Frame 
Python 
ධ㛛 
269 
import 
Tkinter 
as 
tk 
frame 
= 
tk.Frame() 
font=(Helevecca, 
32, 
bold) 
label1 
= 
tk.Label(frame, 
text=Hello 
Cisco, 
font=font, 
bg=red) 
label2 
= 
tk.Label(frame, 
text=Hello 
Python, 
font=font, 
bg=blue) 
label1.pack() 
label2.pack() 
frame.pack() 
frame.mainloop()
Hello 
Frame 
䛾ゎㄝ 
• Frame 
䛾䜲䞁䝇䝍䞁䝇䜢సᡂ 
• Frame 
䛾Ꮚせ⣲(୰䛻䛒䜛)䛿➨୍ᘬᩘ䜢 
Frame 
䛾 
䜲䞁䝇䝍䞁䝇䛻䛩䜛 
• 䝖䝑䝥䝺䝧䝹䛾 
widget 
䛷㻌mainloop() 
䜢࿧䜃ฟ䛩 
Python 
ධ㛛 
270 
import 
Tkinter 
as 
tk 
frame 
= 
tk.Frame() 
font=(Helevecca, 
32, 
bold) 
label1 
= 
tk.Label(frame, 
text=Hello 
Cisco, 
font=font, 
bg=red) 
label2 
= 
tk.Label(frame, 
text=Hello 
Python, 
font=font, 
bg=blue) 
label1.pack() 
label2.pack() 
frame.pack() 
frame.mainloop()
₇⩦ 
• 3䛴䛾䝷䝧䝹䛜⾲♧䛥䜜䜛䜘䛖䛻ᨵ㐀䛩䜛 
• 3␒┠䛾⫼ᬒⰍ䛿 
green 
Python 
ධ㛛 
271 
• Hello 
Cisco, 
Hello 
World 
䛾ᶓ䛾✵ⓑ䜢ᇙ䜑䜛 
• Hint: 
widget䛾 
pack 
䛻 
fill=tk.X 
䜢ᣦᐃ 
Ⓨᒎ⦅
Frame 
䛾⦪ᶓ 
• Frame䛜୪䜆䛾䛿䝕䝣䜷䝹䝖⦪᪉ྥ 
• Ꮚせ⣲䛾 
pack() 
䝯䝋䝑䝗䛾ᘬᩘ䛻᪉ྥ䜢ᣦᐃ 
• ᶓ᪉ྥ䛿 
side=LEFT 
䜢ᣦᐃ 
272 
import 
Tkinter 
as 
tk 
frame 
= 
tk.Frame() 
font=(Helevecca, 
32, 
bold) 
label1 
= 
tk.Label(frame, 
text=Hello 
Cisco, 
font=font, 
bg=red) 
label2 
= 
tk.Label(frame, 
text=Hello 
Python, 
font=font, 
bg=blue) 
label1.pack(side=tk.LEFT) 
label2.pack(side=tk.LEFT) 
frame.pack() 
frame.mainloop()
₇⩦ 
• Frame 
䛻 
Frame 
䜢ධ䜜䛶௨ୗ䛾䜴䜱䞁䝗䜴䜢 
సᡂ䛩䜛 
Python 
ධ㛛 
273
₇⩦䛾ゎ⟅౛ 
Python 
ධ㛛 
274 
import 
Tkinter 
as 
tk 
frame1 
= 
tk.Frame() 
frame2 
= 
tk.Frame() 
font=(Helevecca, 
32, 
bold) 
label1 
= 
tk.Label(frame2, 
text=Hello 
Cisco, 
font=font, 
bg=red) 
label2 
= 
tk.Label(frame2, 
text=Hello 
Python, 
font=font, 
bg=blue) 
label1.pack(side=tk.LEFT) 
label2.pack(side=tk.LEFT) 
label3 
= 
tk.Label(frame1, 
text=Hello 
Wodld, 
font=font, 
bg=green) 
label3.pack() 
frame2.pack() 
label3.pack() 
frame1.pack() 
frame1.mainloop()
⥅ᢎ䛾ᴫᛕ 
Python 
ධ㛛 
275
⥅ᢎ 
Python 
ධ㛛 
276 
• 䜽䝷䝇䛜ู䛾䜽䝷䝇䛾≉ᛶ䜢ᘬ䛝⥅䛠䛯䜑䛾ᶵ⬟ 
• ඖ䛾䜽䝷䝇䜢䝇䞊䝟䞊䜽䝷䝇(ぶ䜽䝷䝇)䛸࿧䜆 
• ὴ⏕䛥䜜䛯䜽䝷䝇䜢䝃䝤䜽䝷䝇(Ꮚ䜽䝷䝇)䛸࿧䜆 
ぶ䜽䝷䝇(㌴) 
Ꮚ䜽䝷䝇(䝆䞊䝥) 
Ꮚ䜽䝷䝇(䝇䞊䝟䞊䜹䞊)
TKinter 
䛾⥅ᢎ 
• Label 
䜔 
Bu7on 
䛿⥅ᢎ䛷సᡂ䛥䜜䛶䛔䜛 
• ⥅ᢎඖ䛷᰿ᮏ䛾ᥥ⏬ฎ⌮䛺䛹䜢ᐇ⿦ 
• Label 
䜔 
Bu7on 
䛿ᕪศ䛾䜏ᐇ⿦ 
Python 
ධ㛛 
277 
Widget 
Bu7on 
Hello 
World 
ᥥ⏬䛾௙⤌䜏䛾ᐇ⿦ 
䝪䝍䞁≉᭷䛾ᶵ⬟䛾ᐇ⿦ 
䝷䝧䝹≉᭷䛾ᶵ⬟䛾ᐇ⿦
⥅ᢎ䛾䝯䝸䝑䝖 
• Ꮚ䜽䝷䝇䛾ᐇ⿦䜢༢⣧໬䛷䛝䜛 
• 䜽䝷䝇⮬య䜒䛭䜜䜢౑䛖ഃ䜒䝁䞊䝗䛾ಖᏲᛶ䛜ቑ䛩 
䛾䝇䞊䝟䞊䜽䝷䝇䛜 
䛾Ꮚせ⣲䛸䛺䜛䛣䛸䛜䛷䛝䜛 
Python 
ධ㛛 
278 
Label 
䛸 
Bu7on 
Frame 
Label 
䜒 
Bu7on 
䜒 
Frame 
䛾 
Ꮚせ⣲䛸䛺䜛䛣䛸䛜䛷䛝䜛
Tkinter 
䛾⮬స䜽䝷䝇 
• ⥅ᢎ䛧䛶䜽䝷䝇䜢స䜛䛸䚸Frame䛻䛔䜜䜛䛣䛸 
䛜䛷䛝䜛䝟䞊䝒䛜స䜜䜛 
Python 
ධ㛛 
279 
Frame 
Frame 
⥅ᢎ䛧䛶䛔䛺䛔ሙྜ 
Frame 
䛻䛔䜜䜙䜜䛺䛔 
⥅ᢎ䛧䛶䛔䜛ሙྜ 
Frame 
䛻䛔䜜䜙䜜䜛
⥅ᢎ䛥䜜䛯䜽䝷䝇䛾≉ᚩ 
• ぶ䜽䝷䝇䛾䝯䞁䝞ኚᩘ䛻䜰䜽䝉䝇ྍ⬟ 
• ぶ䜽䝷䝇䛾䝯䝋䝑䝗䛻䜰䜽䝉䝇ྍ⬟ 
• Ꮚ䜽䝷䝇䛾䝁䞁䝇䝖䝷䜽䝍ෆ䛷ぶ䜽䝷䝇䛾䝁䞁 
䝇䝖䝷䜽䝍䜢࿧䜃ฟ䛩 
Python 
ධ㛛 
280 
Ꮚ䜽䝷䝇 
ぶ䜽䝷䝇 
⥅ᢎ䛾䜲䝯䞊䝆
⥅ᢎ䜽䝷䝇䛾సᡂ 
Python 
ධ㛛 
281
䜽䝷䝇⥅ᢎ䛾సἲ 
䜽䝷䝇䛾ᐉゝ 
class 
Ꮚ䜽䝷䝇(ぶ䜽䝷䝇): 
Ꮚ䜽䝷䝇䛾䝁䞁䝇䝖䝷䜽䝍䛷ぶ䜽䝷 
䝇䛾䝁䞁䝇䝖䝷䜽䝍䜢࿧䜃ฟ䛩 
ぶ䜽䝷䝇䛾䝁䞁䝇䝖䝷䜽䝍䛾ᘬᩘ 
䛿඲䛶ᣦᐃ䚹 
Ꮚ䜽䝷䝇䛿ぶ䜽䝷䝇䛾ኚᩘ䛸䝯 
䝋䝑䝗䛻䜰䜽䝉䝇ྍ⬟ 
(ୖ᭩䛝䜒ྍ⬟) 
Python 
ධ㛛 
282 
class 
Parent: 
text 
= 
 
def 
__init__(self, 
text): 
print(Parent.__init__()) 
self.text 
= 
text 
def 
get_text(self): 
return 
self.text 
class 
Child(Parent): 
def 
__init__(self, 
text): 
print(Child.__init__()) 
Parent.__init__(self, 
text) 
def 
double(self): 
self.text 
*= 
2
₇⩦ 
• 䜽䝷䝇 
Counter 
䜢⥅ᢎ䛧䛶 
Counter2 
䜢స䜛 
• Counter2 
䛻௨ୗ䛾䝯䝋䝑䝗䜢ᐇ⿦䛩䜛 
-­‐ 
double: 
⌧ᅾ䛾䜹䜴䞁䝍䞊䛾್䜢2ಸ䛩䜛 
-­‐ 
clear_counter: 
䜹䜴䞁䝍䞊䜢ึᮇ໬䛩䜛 
Python 
ධ㛛 
283 
class 
Counter: 
value 
= 
-­‐1 
def 
__init__(self, 
value): 
self.value 
= 
value 
def 
get_value(self): 
return 
self.value 
def 
count_up(self): 
self.value 
+= 
1
Tkinter 
䛾 
Bu7on 
• 䝪䝍䞁䜢ᢲ䛥䜜䛯㝿䛻≉ᐃ䛾㛵ᩘ(䝯䝋䝑䝗)䜢࿧䜃ฟ䛩 
• Bu7on(command=䛂࿧䜃ฟ䛩㛵ᩘ(䝯䝋䝑䝗)ྡ䛃) 
Python 
ධ㛛 
284 
import 
Tkinter 
as 
tk 
def 
clicked(): 
print(clicked) 
bu7on 
= 
tk.Bu7on(text=Click, 
command=clicked) 
bu7on.pack() 
bu7on.mainloop() 
 
clicked 
click 
䛩䜛 
⾲♧䛥䜜䜛
⥅ᢎ䜢౑䜟䛺䛔䜹䜴䞁䝍䞊䛾ᐇ⿦ 
Python 
ධ㛛 
285 
import 
Tkinter 
as 
tk 
class 
Counter: 
label 
= 
None 
value 
= 
-­‐1 
def 
clicked(self): 
# 
Click᫬䛻࿧䜀䜜䜛 
self.value 
+= 
1 
self.label.configure(text=self.getText()) 
def 
getText(self): 
# 
⾲♧䛩䜛ᩥᏐ䜢సᡂ 
return 
Count:{}.format(self.value) 
def 
__init__(self, 
value): 
self.value 
= 
value 
frame 
= 
tk.Frame() 
font 
= 
(Helevecca, 
32, 
bold) 
self.label 
= 
tk.Label(frame, 
text=self.getText(), 
font=font, 
bg=red) 
bu7on 
= 
tk.Bu7on(frame, 
text=Click, 
command=self.clicked) 
self.label.pack() 
bu7on.pack() 
frame.pack() 
frame.mainloop() 
c 
= 
Counter(0)
⥅ᢎ䜢౑䛖䜹䜴䞁䝍䛾ᐇ⿦ 
Python 
ධ㛛 
286 
import 
Tkinter 
as 
tk 
class 
Counter(tk.Frame): 
label 
= 
None 
value 
= 
-­‐1 
def 
clicked(self): 
self.value 
+= 
1 
self.label.configure(text=self.getText()) 
def 
getText(self): 
return 
Count:{}.format(self.value) 
def 
__init__(self, 
master=None, 
value=0): 
tk.Frame.__init__(self, 
master) 
self.value 
= 
value 
font 
= 
(Helevecca, 
32, 
bold) 
self.label 
= 
tk.Label(self, 
text=self.getText(), 
font=font, 
bg=red) 
bu7on 
= 
tk.Bu7on(self, 
text=Click, 
command=self.clicked) 
self.label.pack() 
bu7on.pack() 
c 
= 
Counter(value=0) 
c.pack() 
c.mainloop()
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング

More Related Content

What's hot

Pythonの理解を試みる 〜バイトコードインタプリタを作成する〜
Pythonの理解を試みる 〜バイトコードインタプリタを作成する〜Pythonの理解を試みる 〜バイトコードインタプリタを作成する〜
Pythonの理解を試みる 〜バイトコードインタプリタを作成する〜
Preferred Networks
 
TensorFlow Liteを使った組み込みディープラーニング開発
TensorFlow Liteを使った組み込みディープラーニング開発TensorFlow Liteを使った組み込みディープラーニング開発
TensorFlow Liteを使った組み込みディープラーニング開発
Makoto Koike
 

What's hot (20)

全力解説!Transformer
全力解説!Transformer全力解説!Transformer
全力解説!Transformer
 
TRICK 2022 Results
TRICK 2022 ResultsTRICK 2022 Results
TRICK 2022 Results
 
Pythonの理解を試みる 〜バイトコードインタプリタを作成する〜
Pythonの理解を試みる 〜バイトコードインタプリタを作成する〜Pythonの理解を試みる 〜バイトコードインタプリタを作成する〜
Pythonの理解を試みる 〜バイトコードインタプリタを作成する〜
 
分散表現を用いたリアルタイム学習型セッションベース推薦システム
分散表現を用いたリアルタイム学習型セッションベース推薦システム分散表現を用いたリアルタイム学習型セッションベース推薦システム
分散表現を用いたリアルタイム学習型セッションベース推薦システム
 
リクルート式 自然言語処理技術の適応事例紹介
リクルート式 自然言語処理技術の適応事例紹介リクルート式 自然言語処理技術の適応事例紹介
リクルート式 自然言語処理技術の適応事例紹介
 
こわくない Git
こわくない Gitこわくない Git
こわくない Git
 
レコメンドアルゴリズムの基本と周辺知識と実装方法
レコメンドアルゴリズムの基本と周辺知識と実装方法レコメンドアルゴリズムの基本と周辺知識と実装方法
レコメンドアルゴリズムの基本と周辺知識と実装方法
 
開発速度が速い #とは(LayerX社内資料)
開発速度が速い #とは(LayerX社内資料)開発速度が速い #とは(LayerX社内資料)
開発速度が速い #とは(LayerX社内資料)
 
Glibc malloc internal
Glibc malloc internalGlibc malloc internal
Glibc malloc internal
 
マイクロにしすぎた結果がこれだよ!
マイクロにしすぎた結果がこれだよ!マイクロにしすぎた結果がこれだよ!
マイクロにしすぎた結果がこれだよ!
 
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
 
機械学習モデルの判断根拠の説明
機械学習モデルの判断根拠の説明機械学習モデルの判断根拠の説明
機械学習モデルの判断根拠の説明
 
【メタサーベイ】基盤モデル / Foundation Models
【メタサーベイ】基盤モデル / Foundation Models【メタサーベイ】基盤モデル / Foundation Models
【メタサーベイ】基盤モデル / Foundation Models
 
明日使えないすごいビット演算
明日使えないすごいビット演算明日使えないすごいビット演算
明日使えないすごいビット演算
 
ChatGPT 人間のフィードバックから強化学習した対話AI
ChatGPT 人間のフィードバックから強化学習した対話AIChatGPT 人間のフィードバックから強化学習した対話AI
ChatGPT 人間のフィードバックから強化学習した対話AI
 
一般向けのDeep Learning
一般向けのDeep Learning一般向けのDeep Learning
一般向けのDeep Learning
 
Tensor コアを使った PyTorch の高速化
Tensor コアを使った PyTorch の高速化Tensor コアを使った PyTorch の高速化
Tensor コアを使った PyTorch の高速化
 
第1回ROS勉強会発表資料 ROS+Gazeboではじめるロボットシミュレーション
第1回ROS勉強会発表資料 ROS+Gazeboではじめるロボットシミュレーション第1回ROS勉強会発表資料 ROS+Gazeboではじめるロボットシミュレーション
第1回ROS勉強会発表資料 ROS+Gazeboではじめるロボットシミュレーション
 
一般化線形モデル (GLM) & 一般化加法モデル(GAM)
一般化線形モデル (GLM) & 一般化加法モデル(GAM) 一般化線形モデル (GLM) & 一般化加法モデル(GAM)
一般化線形モデル (GLM) & 一般化加法モデル(GAM)
 
TensorFlow Liteを使った組み込みディープラーニング開発
TensorFlow Liteを使った組み込みディープラーニング開発TensorFlow Liteを使った組み込みディープラーニング開発
TensorFlow Liteを使った組み込みディープラーニング開発
 

Viewers also liked

python-twitterを用いたTwitterデータ収集
python-twitterを用いたTwitterデータ収集python-twitterを用いたTwitterデータ収集
python-twitterを用いたTwitterデータ収集
Hikaru Takemura
 
RとPythonによるデータ解析入門
RとPythonによるデータ解析入門RとPythonによるデータ解析入門
RとPythonによるデータ解析入門
Atsushi Hayakawa
 
Pythonスタートアップ勉強会201109 python入門
Pythonスタートアップ勉強会201109 python入門Pythonスタートアップ勉強会201109 python入門
Pythonスタートアップ勉強会201109 python入門
Takayuki Shimizukawa
 
Rユーザのためのspark入門
Rユーザのためのspark入門Rユーザのためのspark入門
Rユーザのためのspark入門
Shintaro Fukushima
 

Viewers also liked (20)

Pythonによる機械学習入門〜基礎からDeep Learningまで〜
Pythonによる機械学習入門〜基礎からDeep Learningまで〜Pythonによる機械学習入門〜基礎からDeep Learningまで〜
Pythonによる機械学習入門〜基礎からDeep Learningまで〜
 
Pythonによる機械学習入門 ~SVMからDeep Learningまで~
Pythonによる機械学習入門 ~SVMからDeep Learningまで~Pythonによる機械学習入門 ~SVMからDeep Learningまで~
Pythonによる機械学習入門 ~SVMからDeep Learningまで~
 
Pythonistaデビュー #PyNyumon 2016/5/31
Pythonistaデビュー #PyNyumon 2016/5/31Pythonistaデビュー #PyNyumon 2016/5/31
Pythonistaデビュー #PyNyumon 2016/5/31
 
「Python言語」はじめの一歩 / First step of Python
「Python言語」はじめの一歩 / First step of Python「Python言語」はじめの一歩 / First step of Python
「Python言語」はじめの一歩 / First step of Python
 
python-twitterを用いたTwitterデータ収集
python-twitterを用いたTwitterデータ収集python-twitterを用いたTwitterデータ収集
python-twitterを用いたTwitterデータ収集
 
PythonによるWebスクレイピング入門
PythonによるWebスクレイピング入門PythonによるWebスクレイピング入門
PythonによるWebスクレイピング入門
 
Python 機械学習プログラミング データ分析ライブラリー解説編
Python 機械学習プログラミング データ分析ライブラリー解説編Python 機械学習プログラミング データ分析ライブラリー解説編
Python 機械学習プログラミング データ分析ライブラリー解説編
 
Pythonで簡単ネットワーク分析
Pythonで簡単ネットワーク分析Pythonで簡単ネットワーク分析
Pythonで簡単ネットワーク分析
 
10分でわかるPythonの開発環境
10分でわかるPythonの開発環境10分でわかるPythonの開発環境
10分でわかるPythonの開発環境
 
PythonとRによるデータ分析環境の構築と機械学習によるデータ認識
PythonとRによるデータ分析環境の構築と機械学習によるデータ認識PythonとRによるデータ分析環境の構築と機械学習によるデータ認識
PythonとRによるデータ分析環境の構築と機械学習によるデータ認識
 
野球Hack!~Pythonを用いたデータ分析と可視化 #pyconjp
野球Hack!~Pythonを用いたデータ分析と可視化 #pyconjp野球Hack!~Pythonを用いたデータ分析と可視化 #pyconjp
野球Hack!~Pythonを用いたデータ分析と可視化 #pyconjp
 
Python東海Vol.5 IPythonをマスターしよう
Python東海Vol.5 IPythonをマスターしようPython東海Vol.5 IPythonをマスターしよう
Python東海Vol.5 IPythonをマスターしよう
 
RとPythonによるデータ解析入門
RとPythonによるデータ解析入門RとPythonによるデータ解析入門
RとPythonによるデータ解析入門
 
Gensim
GensimGensim
Gensim
 
Scikit learnで学ぶ機械学習入門
Scikit learnで学ぶ機械学習入門Scikit learnで学ぶ機械学習入門
Scikit learnで学ぶ機械学習入門
 
scikit-learnを用いた機械学習チュートリアル
scikit-learnを用いた機械学習チュートリアルscikit-learnを用いた機械学習チュートリアル
scikit-learnを用いた機械学習チュートリアル
 
Python入門
Python入門Python入門
Python入門
 
Pythonスタートアップ勉強会201109 python入門
Pythonスタートアップ勉強会201109 python入門Pythonスタートアップ勉強会201109 python入門
Pythonスタートアップ勉強会201109 python入門
 
Rユーザのためのspark入門
Rユーザのためのspark入門Rユーザのためのspark入門
Rユーザのためのspark入門
 
HPCS2015 pythonを用いた量子化学プログラムの開発と応用
HPCS2015 pythonを用いた量子化学プログラムの開発と応用HPCS2015 pythonを用いた量子化学プログラムの開発と応用
HPCS2015 pythonを用いた量子化学プログラムの開発と応用
 

Similar to Python入門 : 4日間コース社内トレーニング

CEDEC2014 Live Coding in C++
CEDEC2014 Live Coding in C++CEDEC2014 Live Coding in C++
CEDEC2014 Live Coding in C++
Seiya Ishibashi
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdf
gmadhu8
 
Lesson6IntroductionToGraphs.pdf
Lesson6IntroductionToGraphs.pdfLesson6IntroductionToGraphs.pdf
Lesson6IntroductionToGraphs.pdf
cifoxo
 

Similar to Python入門 : 4日間コース社内トレーニング (20)

Shizuoka.py #4 pythonで設定ファイルを使う 質疑と資料について追記版
Shizuoka.py #4 pythonで設定ファイルを使う 質疑と資料について追記版Shizuoka.py #4 pythonで設定ファイルを使う 質疑と資料について追記版
Shizuoka.py #4 pythonで設定ファイルを使う 質疑と資料について追記版
 
CEDEC2014 Live Coding in C++
CEDEC2014 Live Coding in C++CEDEC2014 Live Coding in C++
CEDEC2014 Live Coding in C++
 
Arch TCP/IP Introduction
Arch TCP/IP IntroductionArch TCP/IP Introduction
Arch TCP/IP Introduction
 
SUSE Open Forum講演 - OpenStack Summit 2014 Paris 報告
SUSE Open Forum講演 - OpenStack Summit 2014 Paris 報告SUSE Open Forum講演 - OpenStack Summit 2014 Paris 報告
SUSE Open Forum講演 - OpenStack Summit 2014 Paris 報告
 
GDG DevFest Kyoto 2014 これからのGoの話をしよう
GDG DevFest Kyoto 2014 これからのGoの話をしようGDG DevFest Kyoto 2014 これからのGoの話をしよう
GDG DevFest Kyoto 2014 これからのGoの話をしよう
 
Constructs and techniques and their implementation in different languages
Constructs and techniques and their implementation in different languagesConstructs and techniques and their implementation in different languages
Constructs and techniques and their implementation in different languages
 
Grnxx 2014-11-29
Grnxx 2014-11-29Grnxx 2014-11-29
Grnxx 2014-11-29
 
IaaS業界激変の背景
IaaS業界激変の背景IaaS業界激変の背景
IaaS業界激変の背景
 
Python programming
Python programmingPython programming
Python programming
 
EMNLP2014_reading
EMNLP2014_readingEMNLP2014_reading
EMNLP2014_reading
 
Python Course
Python CoursePython Course
Python Course
 
IT勉強会 Anatomy of IT Study groups, seminars, conferences in Japan
IT勉強会 Anatomy of IT Study groups, seminars, conferences in JapanIT勉強会 Anatomy of IT Study groups, seminars, conferences in Japan
IT勉強会 Anatomy of IT Study groups, seminars, conferences in Japan
 
Micro Python で組み込み Python
Micro Python で組み込み PythonMicro Python で組み込み Python
Micro Python で組み込み Python
 
python_class.pptx
python_class.pptxpython_class.pptx
python_class.pptx
 
【実践】Python入門_第一回【岩手県立大学_競プロサークル】
【実践】Python入門_第一回【岩手県立大学_競プロサークル】【実践】Python入門_第一回【岩手県立大学_競プロサークル】
【実践】Python入門_第一回【岩手県立大学_競プロサークル】
 
Learn python
Learn pythonLearn python
Learn python
 
Python by ravi rajput hcon groups
Python by ravi rajput hcon groupsPython by ravi rajput hcon groups
Python by ravi rajput hcon groups
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdf
 
Lesson6IntroductionToGraphs.pdf
Lesson6IntroductionToGraphs.pdfLesson6IntroductionToGraphs.pdf
Lesson6IntroductionToGraphs.pdf
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 

Recently uploaded

Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Lisi Hocke
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
drm1699
 

Recently uploaded (20)

The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test Automation
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanWorkshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
 
What is a Recruitment Management Software?
What is a Recruitment Management Software?What is a Recruitment Management Software?
What is a Recruitment Management Software?
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
 

Python入門 : 4日間コース社内トレーニング

  • 1. Python ධ㛛 Cisco Systems, Japan TAC ఀ⸨ ⿱୍ (twi7er: @yuichi110)
  • 2. ᮏ䝇䝷䜲䝗䛿 Cisco Japan ♫ෆ䛷㛤ദ䛧䛯 TAC (䝖䝷䝤䝹 䝅䝳䞊䝔䜱䞁䜾䜢ᑓ㛛䛸䛩䜛䝛䝑䝖䝽䞊䜽䜶䞁䝆䝙䜰) ྥ 䛡䛾㻌python 䝖䝺䞊䝙䞁䜾䜢୍㒊ಟṇ䛧䛯䜒䛾䛷䛩䚹 ₇⩦㎸䜏䛷㻌4᫬㛫 x 4᪥㻌䛷㛤ദ䛧䛶䛔䜎䛩
  • 3. 䝖䝺䞊䝙䞁䜾䛾┠ⓗ • Python 䜢౑䛘䜛䜘䛖䛻䛺䜛 • 䝥䝻䜾䝷䝭䞁䜾䛾ᴫᛕ䜢⌮ゎ • 䝋䞊䝇䝁䞊䝗(diff䛺䛹)䜢ㄞ䜐 • SDN㛵㐃䛾SR䜢䛥䜀䛟 • python䛷ゎᯒ䝒䞊䝹䜢స䛳䛶䝏䞊䝮䛻㈉⊩ Python ධ㛛 3
  • 4. 䝖䝺䞊䝙䞁䜾䛾ᑐ㇟⪅ • Windows䛺䜚Mac䛺䜚䜢౑䛔䛣䛺䛫䜛䝴䞊䝄 • 䝥䝻䜾䝷䝭䞁䜾䛻⯆࿡䛜䛒䜛 • 䝖䝺䞊䝙䞁䜾䛾᫬㛫 + ⮬⩦᫬㛫䜢ᣢ䛶䜛 • 䝛䝑䝖䝽䞊䜽䜶䞁䝆䝙䜰 Python ධ㛛 4
  • 5. ⿵㊊ 䝖䝺䞊䝙䞁䜾䛾ෆᐜ䛿 Alan Gauld 䛥䜣䛾 Learning to proguram ver 2 䜢ཧ↷䛧䛶సᡂ䚹 䛾䛂ึ䜑䛶䛾Python䛃䜒ཧ↷䛧䛶 Python ධ㛛 5 䜎䛯䚸O’REILLY 䛔䜎䛩䚹 Learning to Program h7p://www.alan-­‐g.me.uk/
  • 7. 䝥䝻䜾䝷䝭䞁䜾䛸䛿 • 㟁Ꮚᶵჾ䜢䛂⮬ศ䛜ពᅗ䛩䜛㏻䜚䛃䛻ື䛛䛩ᡭẁ • ไᚚᩥ䛷ฎ⌮䛾ὶ䜜䜢Ỵ䜑䜛 • ྛฎ⌮䛿㐃⥆䛧䛯࿨௧䛷ᡂ䜚❧䛴 Python ධ㛛 7
  • 8. Ping 䛾ᣲື • ICMP echo 䜢㻌cisco.com 䛻3ᅇ㏦ಙ䛧䚸䛭䛾 reply 䛜ᒆ䛔䛯๭ྜ䛸RTT䜢⟬ฟ Python ධ㛛 8 yuichi$ ping -­‐c 3 cisco.com PING cisco.com (72.163.4.161): 56 data bytes 64 bytes from 72.163.4.161: icmp_seq=0 7l=238 cme=149.949 ms 64 bytes from 72.163.4.161: icmp_seq=1 7l=238 cme=149.783 ms 64 bytes from 72.163.4.161: icmp_seq=2 7l=238 cme=149.826 ms -­‐-­‐-­‐ cisco.com ping stacsccs -­‐-­‐-­‐ 3 packets transmi4ed, 3 packets received, 0.0% packet loss round-­‐trip min/avg/max/stddev = 149.783/149.853/149.949/0.070 ms
  • 9. Ping䛾ᐇ⿦ (1)ᐄඛ䜰䝗䝺䝇䜢ධຊ(cisco.com) (2)ᐄඛ䛻ྥ䛡䛶 ICMP echo request 䜢ᡴ䛴 (3)⤖ᯝ䜢⾲♧ -­‐ ICMP echo reply 䛜䛛䛘䛳䛶䛝䛯ሙྜ ฿╔᫬้ -­‐ ㏦ಙ᫬้㻌䛾 RTT 䜢⾲♧ -­‐ ICMP echo reply 䛜ᖐ䛳䛶䛣䛺䛛䛳䛯ሙྜ lost 䛧䛯䛣䛸䜢⾲♧ (4) (2)-­‐(3) 䜢䛒䛸2ᅇ⧞䜚㏉䛩 (5)3ᅇ䛾ICMP䛾⤖ᯝ䜢㞟ィ䛧䚸⾲♧ PC S D 1ᅇ┠⾲♧ 2ᅇ┠⾲♧ 3ᅇ┠⾲♧ 3ᅇ䛾⤖ᯝ䜢⾲♧ Python ධ㛛 9 echo request echo reply 䝃䞊䝞 ping 䛾ᐇ⿦
  • 10. 䝥䝻䜾䝷䝮䜢⤌䜐䛸䛔䛖䛣䛸 • 䝁䞊䝗䛻䜘䜛ᘓ⠏సᴗ • ⡆༢䛺䝰䝜䛺䜙ᑡ䛺䛔▱㆑䛷స䜜䜛 • ᕧ኱䛺䝰䝜䛿㧗ᗘ䛺ᑓ㛛▱㆑䛜ᚲせ Python ධ㛛 10 䛸䜚䛒䛘䛪䛾┠ᶆᆅⅬ
  • 12. 䝥䝻䜾䝷䝭䞁䜾ゝㄒ • 䝥䝻䜾䝷䝮䜢グ㏙䛩䜛䛯䜑䛾ゝㄒ • 䝁䞁䝢䝳䞊䝍(ᶵᲔㄒ)䛸ே㛫(⮬↛ゝㄒ)䛾௰௓ゝㄒ • 䛔䛟䛴䛛䛾✀㢮䛜䛒䜛 Python ධ㛛 12 'hello world'䜢⾲♧䛥䛫䜛 print('hello world') 00100101001101010 ே䛜䜔䜚䛯䛔䛣䛸 䝥䝻䜾䝷䝭䞁䜾ゝㄒ ᶵ఍䛜ゎ㔘ྍ⬟䛺࿨௧
  • 13. 䛺䛬㻌Python ? • ᩥἲ䛜䝅䞁䝥䝹䛷ึᚰ⪅ྥ䛡 • 䝟䝽䝣䝹䛺ゝㄒ • 䜸䝤䝆䜵䜽䝖ᆺᣦྥゝㄒ Python ධ㛛 13
  • 14. Python ௨እ䛾㑅ᢥ⫥ • C: 䝝䞊䝗䛻㏆䛔ᴫᛕ䜢ព㆑䛩䜛ᚲせ䛜䛒䜛 OS䛻㏆䛔䛣䛸䜢䛧䛺䛔䛸㛤Ⓨ䛜㠀ຠ⋡ • Java: Ⰻ䛔ゝㄒ䛰䛜䚸ぬ䛘䜛䛣䛸䛜ከ䛔 Python 䛾䜋䛖䛜⡆༢ • 䛭䛾௚Script⣔ゝㄒ: ciscoᶵჾ䛜㠀䝃䝫䞊䝖 • Web⣔ゝㄒ: cisco 䛷㔜せᗘ䛜ప䛔 • 䛭䛾௚: ୰⣭⪅-­‐> ୖ⣭⪅㻌䛻䛺䜚䛯䛔ேྥ䛡 Python ධ㛛 14
  • 15. C, Java, Python 䛾ẚ㍑ • ⌧ᅾ䛾䝕䜱䝺䜽䝖䝸௨ୗ䛾ᵓ㐀䜢᭩䛝ฟ䛩䝥䝻䜾䝷䝮 Python ධ㛛 [/Users/yuichi/Documents/_TRAINING] CCIE Java listFile.py python [/Users/yuichi/Documents/_TRAINING/CCIE] ccie-­‐lab.key [/Users/yuichi/Documents/_TRAINING/Java] Java Basic Training Day 2.pptx Java Basic Training Day 3.pptx Java Basic Training Day 4.pptx Java Basic Training Day1.pptx [/Users/yuichi/Documents/_TRAINING/python] adfunc.py dict-­‐test.py excepcon.py func.py 15 _TRAINING CCIE Java Python ccie-­‐lab.key Java Basic Training Day2 ..... adfunc.py ..... lismile.py 䝥䝻䜾䝷䝮䛾ฟຊ
  • 18. Python 䛷䛾౛ Python ධ㛛 18 IDLE
  • 19. ẚ㍑⤖ᯝ Python ධ㛛 19 • Python 䛿⡆༢䛻䝥䝻䜾䝷䝮䛜᭩䛡䜛
  • 21. Agenda • Day1: 䜲䞁䝇䝖䞊䝹䚸ᇶᮏᩥἲ䚸䝕䞊䝍ᆺ • Day2: ⥆䝕䞊䝍ᆺ䚸䝰䝆䝳䞊䝹䛸㛵ᩘ • Day3: 䜸䝤䝆䜵䜽䝖ᣦྥ䚸䜰䝹䝂䝸䝈䝮 • Day4: ṇつ⾲⌧䚸䜶䝷䞊ฎ⌮䚸䝕䞊䝍䝧䞊䝇 Python ධ㛛 21
  • 22. Agenda Day 1 • Python 䛾䜲䞁䝇䝖䞊䝹 • 䝥䝻䞁䝥䝖䛸IDLE䛾౑䛔᪉ • ᆺ䛸ኚᩘ • ᮲௳ศᒱ • 䝹䞊䝥 • ᶆ‽ධຊ • 㛵ᩘ䛸䝰䝆䝳䞊䝹䛾฼⏝ • ₇⩦ Python ධ㛛 22
  • 24. Python Installacon • Mac䛿䝍䞊䝭䝘䝹䛷᭱ึ䛛䜙౑䛘䜛 • 㛤Ⓨ⎔ቃ䜢స䜛䛯䜑䛻䝞䜲䝘䝸䛛䜙䜲䞁䝇䝖䞊䝹 • 䝞䞊䝆䝵䞁䛿 2.7 ⣔䛾᭱᪂∧ Python බᘧ䝨䞊䝆 Python ධ㛛 24
  • 25. Python 䛾䝞䞊䝆䝵䞁 • Version 3.3.x: ᭱᪂∧䛾䝞䞊䝆䝵䞁 • Version 2.7.x: 䜎䛰䜎䛰୺ὶ䛾䝞䞊䝆䝵䞁 • Cisco: 2.7.x 䜢฼⏝ Python ධ㛛 25
  • 28. PROMPT 䛸 IDLE 䛾฼⏝ Python ධ㛛 28
  • 29. Prompt 䛷 python 䜢࿧䜃ฟ䛩 • Windows: Start -­‐> python2.7 (command line) • Mac: terminal 䛷 python2.7 䛸ᡴ䛴 Python ධ㛛 29
  • 30. Prompt 䛷 python 䜢࿧䜃ฟ䛩 • Windows: Start -­‐> python2.7 (command line) • Mac: terminal 䛷 python2.7 䛸ᡴ䛴 Python ධ㛛 30
  • 31. Prompt 䛾฼⏝ • Prompt 䛷௨ୗ䛾䜘䛖䛺䝁䝬䞁䝗䜢Ⓨ⾜䛧䛶䜏䜛 Python ධ㛛 31 print('hello world') 6+1*3 (6+1)*3 5/2 5/2.0 5/0 print("{} + {} = {}".format(2,3,5)) print """hello I love Cisco I love Python too """ import sys sys.exit()
  • 32. 䝁䝬䞁䝗䛷 script 䝣䜯䜲䝹䜢ᐇ⾜ • 䝣䜯䜲䝹䛻ฎ⌮䜢グ㏙䛧䚸䛭䜜䜢䝁䝬䞁䝗䛷ᐇ⾜ • Python 䛾䝇䜽䝸䝥䝖䝣䜯䜲䝹䛿ᣑᙇᏊ䛜 .py • Windows 䛿ᚲせ䛒䜜䜀python 䜢䝟䝇ᣦᐃ䛩䜛 Python ධ㛛 32 print “hello Cisco” print “hello Python” print “done!” test.py Mac 䛾ᐇ⾜⏬㠃 $ python 䝣䜯䜲䝹ྡ
  • 33. 䝁䝬䞁䝗䛷 script 䝣䜯䜲䝹䜢ᐇ⾜ • 䝣䜯䜲䝹䛻ฎ⌮䜢グ㏙䛧䚸䛭䜜䜢䝁䝬䞁䝗ᐇ⾜ • Python 䛾䝇䜽䝸䝥䝖䝣䜯䜲䝹䛿ᣑᙇᏊ䛜 .py • Windows 䛿ᚲせ䛒䜜䜀python 䜢䝟䝇ᣦᐃ䛩䜛 䈜 ᫬㛫䛾䛒䜛䛸䛝䛻䝟䝇䛾タᐃ䜢ຍ䛘䛶䛚䛟䛸ᴦ Python ධ㛛 33 Windows 䛾ᐇ⾜⏬㠃
  • 34. IDLE • 䛣䛾䝖䝺䞊䝙䞁䜾䛾䝯䜲䞁䛾㛤Ⓨ⎔ቃ • Windows: startup -­‐> python -­‐> idle • Mac: applicacon -­‐> python -­‐> idle Python ධ㛛 34 IDLE 䝥䝻䞁䝥䝖⏬㠃
  • 35. IDLE 䛾฼⏝ • IDLE 䛾䜶䝕䜱䝍䜢౑䛳䛯㛤Ⓨ • 䝯䝙䝳䞊䜘䜚ඛ䜋䛹స䛳䛯㻌test.py 䜢㛤䛟 • 䜶䝕䜱䝍⏬㠃䜢㑅ᢥ䛧䚸F5 䜢ᢲ䛩䛸ᐇ⾜ Python ධ㛛 35
  • 36. 䝁䝯䞁䝖䜰䜴䝖 • 䝁䝯䞁䝖: 䝥䝻䜾䝷䝮䛾ὀ㔘䚹ฎ⌮⣔䛻↓ど䛥䜜䜛 • ୍⾜䛾䝁䝯䞁䝖䜰䜴䝖: # 䝁䝯䞁䝖ᩥ • 」ᩘᴗ䛾䝁䝯䞁䝖䜰䜴䝖: """ 」ᩘ⾜ᣦᐃྍ⬟ """ Python ධ㛛 36
  • 38. ᆺ • 䝕䞊䝍䛻䛿ᆺ(✀㢮)䛜䛒䜛 • ᩚᩘᆺ(Integer): 1,5,-­‐1 • ᩥᏐิᆺ(String): "Cisco", "Python" Python ධ㛛 38
  • 39. ᆺ䛸ไᚚ • ᆺ䛸䝥䝻䜾䝷䝮䛾ไᚚ䛻䛿ᐦ᥋䛺㛵ಀ䛜䛒䜛 – ᩥᏐิ䜢ᩘᏐ䛷๭䜛䛣䛸䛿䛷䛝䛺䛔 – ᩥᏐิ䛸ᩘᏐ䜢⤖ྜ䛷䛝䛺䛔 • 䜘䛟䛒䜛ฎ⌮䛿ᡭ⥆䛝᪉ἲ䜢ぬ䛘䜛 – ᩥᏐิ䛸ᩘᏐ䛾⤖ྜ䛿䚸ᩘᏐ䜢ᩥᏐิ䛻ኚ᥮䛧 䛶䛛䜙⤖ྜ䛩䜛 Python ධ㛛 39
  • 40. ኚᩘ • ኚᩘ: Data䜢ಖᏑ䛩䜛ᐜჾ Python ධ㛛 40 5 print(5) => 5 x = 5 print(x) => 5 5 ኚᩘ X 䝕䞊䝍(ᐃᩘ 5) ௦ධ print ( 5 ) print ( 5 )
  • 41. ኚᩘ䛸ᆺ • ᆺᐉゝ: 䛣䛾ኚᩘ䛿䛣䛾ᆺ䜢᱁⣡䛩䜛䛸䛔䛖ᐉゝ • Python 䛾ኚᩘ䛻䛿ᆺᐉゝ䛜䛺䛔 • ኚᩘ䛻䛹䛾䜘䛖䛺ᆺ䛷䜒䛔䜜䜙䜜䜛 Python ධ㛛 41 int x = 5; x = "Java" //Error x = 5; print(x) x = "Python" # OK Java print(x) Python
  • 42. Python 䛾ᆺ • ⤌䜏㎸䜏ᆺ – ᩘᏐ: int, float 䛺䛹 – ᩥᏐิ: ⱥㄒ䚸᪥ᮏㄒ – 䝸䝇䝖: 㓄ิ䛻ఝ䛶䛔䜛 – Bool: True or False – 㛵ᩘ: Python 䛷䛿㛵ᩘ䜒ᆺ䛾୍䛴 – 䛭䛾௚ • 䝴䞊䝄䛜ᐃ⩏䛧䛯ᆺ(Class) Python ධ㛛 42
  • 43. ᩘᏐ • ᩚᩘ(int)䛸ᐇᩘ(float)䚸」⣲ᩘ䛜䛒䜛 • ୖ㝈䛿Ẽ䛻䛧䛺䛟䛶䜘䛔 Python ධ㛛 >>> 1234567 * 3456789 4267637625363 (42඙) 43 ౑䛘䜛₇⟬Ꮚ ㄝ᫂ M + N ㊊䛧⟬ M – N ᘬ䛝⟬ M * N 䛛䛡⟬ M / N ๭䜚⟬ M % N ๫వ(䛒䜎䜚) M**N 䜉䛝஌ (M * M * M … 䜢Nᅇ)
  • 44. ᩘᏐ • 䜲䞁䜽䝸䝯䞁䝖䛿౑䛘䛺䛔 • ௦ධ₇⟬Ꮚ䛷௦⏝ Python ධ㛛 44 ₇⟬Ꮚ ㄝ᫂ M += N M = M + N M -­‐= N M = M – N M *= N M = M * N M /= N M = M / N M %= N M = M % N Java int i = 0 i++ Python i = 0 i += 1 ß i = i+1
  • 45. ᩥᏐิᆺ • 3✀㢮䛾グ㏙᪉ᘧ Python ධ㛛 45 䝅䞁䜾䝹䜽䜸䝔䞊䝅䝵䞁 string = ‘ Hello Python’ 䝎䝤䝹䜽䜸䝔䞊䝅䝵䞁 string = “Hello Python” 䝖䝸䝥䝹䞉䝎䝤䝹䜽䜸䝔䞊䝅䝵䞁 string = “””Hello World Hello Python # this isn’t comment Hello CIsco””” ᨵ⾜䜔≉Ṧグྕ䜒 䛭䛾䜎䜎ᩥᏐิ䛻䚹
  • 46. ᩥᏐิᆺ • ᩥᏐิ䜒₇⟬グྕ䜢౑䛘䜛 Python ධ㛛 46 print “I love “ + “python” => “I love Python” print “hello “ * 3 => “hello hello hello “ print “I say “ + (“hello “ * 3) => “I say hello hello hello” x = “I love “ y = “Python” print(x+y) => "I love Python"
  • 47. Boolᆺ • 䛔䜟䜖䜛 True, False 䜢ᣢ䛴ᆺ • If ᩥ䛺䛹䛾ไᚚศ䛷䜘䛟฼⏝䛥䜜䜛 Python ධ㛛 47 ₇⟬Ꮚ ព࿡ A and B A 䜒 B 䜒 True 䛺䜙 True A or B A 䛛 B 䛜 True 䛺䜙 True A == B A 䛸 B 䛜ྠ䛨䛺䜙 True A != B A 䛸 B 䛜㐪䛘䜀 True A <> B ྠୖ not A A 䛜 False 䛺䜙 True
  • 48. Boolᆺ • 䛔䜟䜖䜛 True, False 䜢ᣢ䛴ᆺ • If ᩥ䛺䛹䛾ไᚚศ䛷䜘䛟฼⏝䛥䜜䜛 Python ධ㛛 48 ₇⟬Ꮚ ព࿡ A > B A 䛜 B 䜘䜚኱䛝䛡䜜䜀 True A >= B A 䛜 B ௨ୖ䛺䜙 True A < B A 䛜 B 䜘䜚ᑠ䛥䛡䜜䜀 True A <= B A 䛜 B ௨ୗ䛺䜙 True
  • 49. Listᆺ • ᣑᙇ䛜䛷䛝䜛㓄ิ [x,y,z] 䛸䛔䛖ᙧᘧ䛷సᡂ • ₇⟬Ꮚ [x] 䛷䚸㓄ิ䛾 x ␒┠䛻䜰䜽䝉䝇䛩䜛 • ఱ␒┠䛛(index)䛾ᣦᐃ䛿0䛛䜙ᩘ䛘䜛 Python ධ㛛 49 list1 = [3,4,5] print(list1) => [3, 4, 5] list2 = [“I”, “Love”, “Python”] print(list2[2]) => “Python” list3 = [[1,2], ['A',”B”]] print(list3) => [[1, 2], [“A”, “B”]] print(list3[1][1]) => “B” print(list3[5][0]) => # index out of range 䛾 Error ⾲♧
  • 50. Listᆺ • 㓄ิ㛗䜢ྲྀᚓ: len(X) • 䜸䝤䝆䜵䜽䝖䛾 index 䜢ྲྀᚓ: index(X) • 㓄ิ䛻䜸䝤䝆䜵䜽䝖䜢㏣ຍ: append(x) • 㓄ิ䛛䜙䜸䝤䝆䜵䜽䝖䜢๐㝖: del Python ධ㛛 50 list1 = [1,2,3,4,5] print(list1.index(3)) => 2 print(len(list1)) => 5 list1.append(6) print(list) => [1,2,3,4,5,6] del list2[1] print(list2) => [1,3,4,5,6]
  • 51. 䛭䛾௚䛾ᆺ(ᚋ᪥) • Tuple • Dicconary • File • Dates, Times • 䝴䞊䝄ᐃ⩏䛾ᆺ(䜽䝷䝇) • 㛵ᩘ Python ධ㛛 51
  • 53. ₇⩦ (₇⟬) • ௨ୗ䛾ᅗᙧ䛾㠃✚䜢䝥䝻䜾䝷䝮䛷ィ⟬䛧䛶䛟䛰䛥 䛔 • వ⿱䛜䛒䜜䜀䚸import ௨እ䛿୍⾜(䝽䞁䝷䜲䝘 䞊)䛸䛧䛶䛟䛰䛥䛔 Python ධ㛛 53 4cm 3cm 10cm 5cm 2cm 8cm ༙ᚄ 2cm 䈜 ෇࿘⋡䛿 math module 䛾 π 䜢 ౑䛳䛶䛟䛰䛥䛔 >>> import math >>> math.pi 3.141592653589793
  • 54. ₇⩦ (䝗䜻䝳䝯䞁䝖) • ᩘᏐ 1984 䛸ᩥᏐิ “cisco” 䜢ᩥᏐิ⤖ྜ䛧䚸⾲ ♧䛧䛶䛟䛰䛥䛔 (䛭䛾䜎䜎⤖ྜ䛩䜛䛸䜶䝷䞊䛻䛺䜛 䛾䛷䚸ᩘᏐ䜢ᩥᏐิ䛻ኚ᥮䛩䜛ᚲせ䛒䜚) • 䜾䜾䜛䛾䛷䛿䛺䛟䚸௨ୗ䛾䝨䞊䝆䛛䜙ᚲせ䛺䛂⤌ 䜏㎸䜏㛵ᩘ䛃䜢᥈䛧䛶䛟䛰䛥䛔 Python ධ㛛 54 Python ᶆ‽䝷䜲䝤䝷䝸 h7p://docs.python.jp/2/library/index.html (ⱥ) h7p://docs.python.org/2.7/library/index.html#python
  • 56. ไᚚᵓ㐀 • ⡆༢䛺䝞䝑䝏ฎ⌮: ୖ䛛䜙ୗ䛻୍⾜䛪䛴ᐇ⾜䛧䛶䛔䛟 • 䝥䝻䜾䝷䝭䞁䜾 – ୖ䛛䜙ୗ䛻ᐇ⾜䛧䛶䛔䛟 – Ỵ䜑䜙䜜䛯ᩥἲ䛸༢ㄒ(ண⣙ㄒ)䛷ฎ⌮᪉ἲ䜢グ㏙ – ifᩥ䚸loopᩥ䛺䛹䛷䝥䝻䜾䝷䝮䛾ὶ䜜䜢ไᚚ䛩䜛 – 䜽䝷䝇䜔㛵ᩘ䛺䛹䜢౑䛳䛶ฎ⌮䜢䜎䛸䜑䜛 Python ධ㛛 56
  • 58. ண⣙ㄒ • ✵ⓑ䛷༊ษ䜙䜜䛯༢ㄒ䛾㐃⥆䛷䝥䝻䜾䝷䝮䜢グ㏙䛩䜛 • ண⣙ㄒ: 䝥䝻䜾䝷䝮䛾ไᚚ䛾䛯䜑䛾䛂≉ู䛃䛺༢ㄒ • ண⣙ㄒ௨እ: ᐃᩘ䚸ኚᩘ䚸㛵ᩘ䚸䜽䝷䝇ྡ䛺䛹⮬⏤䛻䛴 䛡䜙䜜䜛ྡ๓ Python ධ㛛 58 䝥䝻䜾䝷䝮䛾ไᚚ if, for, while ᐉゝ class, def ண⣙ㄒ䛾౛
  • 59. 䝁䞊䝗䝤䝻䝑䜽䛸䜲䞁䝕䞁䝖 • C,Java䛺䛹: {} 䛸䛔䛖グྕ䛷ฎ⌮䜢䜎䛸䜑䜛 • Python: 䜲䞁䝕䞁䝖(Ꮠୗ䛢)䛷ฎ⌮䜢䜎䛸䜑䜛 Python ධ㛛 59 for(int i=0; i10; i++){ if(i%2 == 0){ System.out.println(i + is even); }else{ System.out.println(i + is odd); } } System.out.println(done); for i in range(10): if(i%2 == 0): print({} is even.format(i)) else: print({} is odd.format(i)) print(done) Java Python
  • 61. ᮲௳ศᒱ IF • ≉ᐃ䛾᮲௳䜢‶䛯䛩ሙྜ䛾䜏ฎ⌮䜢⾜䛖 Python ධ㛛 61 Step1 Test Condicon Path1 Path2
  • 62. ᮲௳ศᒱ IF • ≉ᐃ䛾᮲௳䜢‶䛯䛩ሙྜ䛾䜏ฎ⌮䜢⾜䛖 Python ධ㛛 62 if(᮲௳A): ᮲௳A䛜True䛺䜙ᐇ⾜ …. elif(᮲௳B): ᮲௳A䛜False䛛䛴᮲௳B䛜True䛺䜙ᐇ⾜ …. else: ᮲௳A䜒B䜒False䛺䜙ᐇ⾜ …. if 䛾᮲௳ศᒱ䛾⤖ᯝ䛻㛵䜟䜙䛪ᐇ⾜ ….
  • 63. ᮲௳ศᒱ IF • Python 䛾䝁䞊䝗 Python ධ㛛 63 i = 5 if (i 10): print “This is never printed” else: print “This might be printed”
  • 64. (⿵㊊)᮲௳ศᒱ SWITCH-­‐CASE • ᐇ⿦䛥䜜䛶䛔䛺䛔䚹Ifᩥ䛷స䜛 Python ධ㛛 64 switch(᮲௳){ case A: ᮲௳䛜A䛾ሙྜ䛾ฎ⌮ break; case B: ᮲௳䛜B䛾ሙྜ䛾ฎ⌮ break default: 䛭䜜௨እ䛾ሙྜ䛾ฎ⌮ } if(᮲௳ == A): ᮲௳䛜A䛾ሙྜ䛾ฎ⌮ elif(᮲௳ == B): ᮲௳䛜B䛾ሙྜ䛾ฎ⌮ else: 䛭䜜௨እ䛾ሙྜ䛾ฎ⌮ Java Python
  • 66. 䝹䞊䝥ฎ⌮ for • ≉ᐃ䛾ฎ⌮䜢Ỵ䜑䜙䜜䛯ᅇᩘ䛣䛺䛩 • Python䛷䛿䝸䝇䝖䛾㉮ᰝ䛻౑⏝ Python ධ㛛 66 Repeated Steps Test Condicon
  • 67. 䝸䝇䝖䛸for • C䜔Java䛺䛹䛾for䛸䛿ู≀ • C䜔Java䛸ྠ䛨䜘䛖䛺౑䛔᪉䜒䛷䛝䜛 Python ධ㛛 67 䝸䝇䝖 A B C D (1) A䜢౑䛳䛶ฎ⌮䜢ᐇ⾜ (2) B䜢౑䛳䛶ฎ⌮䜢ᐇ⾜ (3) C䜢౑䛳䛶ฎ⌮䜢ᐇ⾜ (4) D䜢౑䛳䛶ฎ⌮䜢ᐇ⾜
  • 68. forᩥ䛾᭩ᘧ Python ධ㛛 68 for ኚᩘ in 䝸䝇䝖: ᐇ⾜䛩䜛ฎ⌮㻌 䝸䝇䝖䛾0␒┠䜢ኚᩘ䛻௦ධ䛧䛶ฎ⌮䜢ᐇ⾜ 䝸䝇䝖䛾1␒┠䜢ኚᩘ䛻௦ධ䛧䛶ฎ⌮䜢ᐇ⾜ ..... string = for char in [H,e,l,l,o]: string += char print(string) Hello
  • 69. for䛻䜘䜛୍ᐃᅇᩘ䛾ฎ⌮ • ୍ᐃᅇᩘ䛾ฎ⌮䛻䛿 range 㛵ᩘ䜢ే⏝ • range(x): 0 䛛䜙 x-­‐1 䜎䛷䛾㐃␒䛾䝸䝇䝖䜢㏉䛩 • range(x,y): x 䛛䜙 y-­‐1 䜎䛷䛾㐃␒䛾䝸䝇䝖䜢㏉䛩 Python ධ㛛 69 for(int i=0; i10; i++){ System.out.println(i) } for i in range(10): print(i) Java Python
  • 70. (⿵㊊)䜲䝔䝺䞊䝍 • python 䛾㻌for 䛿 iterator 䛸࿧䜀䜜䜛௙⤌䜏 • 䝍䝥䝹䜔䚸ᩥᏐิ䚸㎡᭩䛺䛹䜒 key 䛸䛧䛶౑䛘䜛 Python ධ㛛 70 for char in hello: print(char) = h e l l o
  • 71. 䝹䞊䝥ฎ⌮ while • ≉ᐃ᮲௳䜢‶䛯䛩䜎䛷䝹䞊䝥䜢⧞䜚㏉䛩 • 䛂ࠥ 䛾㛫䛿䛪䛳䛸䝹䞊䝥䛃䛸䛔䛖䜘䛖䛺䜲䝯䞊䝆 • For 䜋䛹䛿౑䜟䜜䛺䛔 Python ධ㛛 71
  • 72. while 䛻㐺䛧䛯䝹䞊䝥ฎ⌮ • ఱ࿘䛩䜜䜀䛔䛔䛛ศ䛛䜙䛺䛔ฎ⌮䛻౑䛖 • ↓㝈䝹䞊䝥䛻䛺䜙䛺䛔䜘䛖䛻ὀព䛜ᚲせ ୚䛘䜙䜜䛯ᩘ䜢2㐍ᩘ䛷⾲⌧䛩䜛䛾䛻ᚲせ䛺᱆ᩘ䜢ồ䜑䜛 Python ධ㛛 72 x = 6789329 i = 1 while(2**i x): i += 1 print(2 ^ {} {}.format(i,x)) 2 ^ 23 6789329
  • 74. ₇⩦ (᮲௳ศᒱ) • 1 – 100 䜎䛷䛾അᩘ䛾್䜢ᣢ䛴 List 䜢సᡂ䛩䜛 • 1 – 10000 䜎䛷䛾⣲ᩘ䜢⾲♧䛩䜛䚹 䚷䚷䚷㻌⣲ᩘ: 1䛸⮬ศ௨እ䛾ᩚᩘ䛷๭䜜䛺䛔ᩚᩘ • Fizz buzz 䜢 100 䜎䛷⾜䛖 1-­‐100䜎䛷䜢⾲♧䚹 3䛷๭䜚ษ䜜䜛᫬䛿 fizz, 5䛷๭䜚ษ䜜䜛᫬䛿 buzz 䛸⾲♧ 3䛷䜒5䛷䜒๭䜚ษ䜜䜛᫬䛿 fizz buzz 䛸⾲♧䛩䜛 Python ධ㛛 74
  • 76. 㛵ᩘ䛾࿧䜃ฟ䛧 • ≉ᐃ䛾ฎ⌮䜢⾜䛖䛯䜑䛾࿧䜃ฟ䛧ཱྀ • 㛵ᩘྡ䛻ᘬᩘ䜢୚䛘䛶࿧䜃ฟ䛩 • 㛵ᩘ䛿㏉䜚್䜢㏉䛩 Python ධ㛛 76 㛵ᩘྡ(ᘬᩘ) ㏉䜚್ length = len([1,2,3,4,5]) print(length) 5
  • 77. 㛵ᩘ䛷」㞧䛺ฎ⌮䜢㞃䛩 • 㛵ᩘ䜢౑䛖䛣䛸䛷䝁䞊䝗䛜䜟䛛䜚䜔䛩䛟䛺䜛 Python ධ㛛 77 i = -­‐5 if(i0): i = -­‐i print i = 5 䜁䛳䛸ぢ䚸ఱ䜢䜔䛳䛶 䛔䜛䛛ศ䛛䜙䛺䛔 i = -­‐5 i = abs(i) print i = 5 㛵ᩘྡ䛷䛩䛠䛂⤯ᑐᩘ䜢 ᚓ䜛䝥䝻䜾䝷䝮䛃䛸䜟䛛䜛
  • 78. 㛵ᩘ䛷䝁䞊䝗䛾㔜」䜢ῶ䜙䛩 • 䝁䞊䝗䛾㔜」䜢䛺䛟䛧䚸ಖᏲᛶ䜢ྥୖ䛥䛫䜛 Python ධ㛛 78 i = -­‐5 j = 8 If(i0): i = -­‐i If(j0): j = -­‐j print(i) = 5 print(j) = 8 i = -­‐5 j = 8 㛵ᩘ abs 䛾ᐃ⩏ print(abs(i)) = 5 print(abs(j)) = 8
  • 79. 䝰䝆䝳䞊䝹 • 䝰䝆䝳䞊䝹䛿䝥䝻䜾䝷䝮䛾ᩚ⌮ᩚ㡻ᡭἲ Python ධ㛛 79 vs 䝰䝆䝳䞊䝹䛜䛺䛔 䝰䝆䝳䞊䝹䜢฼⏝
  • 80. 䝰䝆䝳䞊䝹䛻䜘䜛㝵ᒙ໬ • ᶵ⬟䛤䛸䛻䜘䜛䝥䝻䜾䝷䝮䛾ᩚ⌮ Python ධ㛛 80 math file system os ⮬ศ䛜సᡂ ⤌䜏㎸䜏 (䝕䝣䜷䝹䝖䛷 ౑䛘䜛)
  • 81. 䝰䝆䝳䞊䝹䜈䛾䜰䜽䝉䝇 • 䝰䝆䝳䞊䝹䜢฼⏝䛩䜛䛻䛿 import 䛩䜛ᚲせ䛜䛒䜛 Python ධ㛛 81 random() Traceback (most recent call last): File “stdin”, line 1, in module NameError: name 'random' is not defined import random random.random() 0.6019003331149728 import 䛧䛶䛔䛺䛔䛸 ฼⏝䛷䛝䛪䛻䜶䝷䞊 䛂䝰䝆䝳䞊䝹ྡ.㛵ᩘ䛃 䛷䝰䝆䝳䞊䝹䛾㛵ᩘ䜢฼⏝
  • 84. ᶆ‽ධຊ • 䝴䞊䝄䛛䜙䛾ධຊ䜢ཷ䛡ྲྀ䜛 䛸ධຊ䛥䜜䜛 ධຊᚅ䛱䛜ゎ㝖䛥䜜䜛 Python ධ㛛 84 input = raw_input() 䝴䞊䝄䛛䜙䛾ධຊ䜢ᚅ䛴 “hello” print(input) = “hello”
  • 85. ᶆ‽ධຊ • ↓㝈 echo 䝥䝻䜾䝷䝮 Python ධ㛛 85 while(True): print “please input:” input = raw_input() print input 䈜䚷Ctr-­‐C 䛷ᢤ䛡䜛㻌
  • 87. 䝁䝬䞁䝗䝷䜲䞁ᘬᩘ • 䝇䜽䝸䝥䝖䜢䛂䝟䝷䝯䞊䝍䛃䛴䛝䛷㉳ື䛩䜛 • 䝇䜽䝸䝥䝖ෆ䛻┤᥋᭩䛝㎸䜐䜘䜚⼥㏻ᛶ䛜䛒䜛 Python ධ㛛 87 ping䛾䝥䝻䜾䝷䝮 (1) ᐄඛ䜢ᘬᩘ䛷ཷ䛡ྲྀ䜛 (1) ᐄඛ䜈㻌icmp echo request 䜢㏦䜛 (2) ᐄඛ䛛䜙㻌icmp echo reply 䜢ཷ䛡ྲྀ䜛 (3) ཷಙ⤖ᯝ䜢⾲♧ ping.py cisco.com python.org #python ping.py cisco.com #python ping.py python.org
  • 88. sys.argv • 䝁䝬䞁䝗䝷䜲䞁ᘬᩘ䛻䜰䜽䝉䝇䛩䜛䛻䛿 sys 䝰䝆䝳䞊 䝹䜢 import 䛩䜛 • ᘬᩘ䛜argv䛻䝸䝇䝖䛸䛧䛶᱁⣡䛥䜜䛶䛔䜛 Python ධ㛛 88 import sys print(-­‐-­‐-­‐-­‐ args -­‐-­‐-­‐-­‐) print(len(sys.argv)) print(sys.argv[0]) print(sys.argv[1]) print(sys.argv[2]) print(-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐) a = int(sys.argv[1]) b = int(sys.argv[2]) print({} + {} = {}.format(a,b,a+b)) $ python test.py 1 5 -­‐-­‐-­‐-­‐ args -­‐-­‐-­‐-­‐ 3 test.py 1 5 -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐ 1 + 5 = 6 test.py ฟຊ
  • 90. ₇⩦(ᶆ‽ධຊ) • 䝴䞊䝄䛜ධຊ䛧䛯ᩥᏐิ䛾ᩥᏐᩘ䜢⾲♧䛩 䜛䝥䝻䜾䝷䝮䜢సᡂ • ධຊ䛿ఱᗘ䛷䜒ཷ䛡௜䛡䜛䛜䚸ᩥᏐิ exit 䜢 ཷ䛡ྲྀ䜛䛸⤊஢䛩䜛䛣䛸 Python ධ㛛 90
  • 91. ḟᅇண࿌ • 㛵ᩘ䜢స䜛 • 㧗ᗘ䛺ᆺ䜢౑䛖 • 䝣䜯䜲䝹ฎ⌮ Python ධ㛛 91 • 䛷䛝䜛䜘䛖䛻䛺䜛䛣䛸 – 䝔䜻䝇䝖ฎ⌮䛻䜘䜛䝻䜾䛾ゎᯒ – OS䜢᧯స䛩䜛䛯䜑䛾䝅䜵䝹䝇䜽䝸䝥䝖䛾సᡂ
  • 93. Python ධ㛛 Learning to Program For Cisco Network Engineers Day 2/4 Japan TAC DC Team. Yuichi Ito
  • 94. Agenda Day 2 • Python • ⥆䝕䞊䝍ᆺ • 㛵ᩘ䛾సᡂ • 䝰䝆䝳䞊䝹䛾సᡂ • ₇⩦1(Map) • ᩥᏐิฎ⌮ • 䝣䜯䜲䝹ฎ⌮ • OS䝁䝬䞁䝗䛾฼⏝ • ₇⩦2(䝻䜾ゎᯒ䚸Linux䛾䝇䜽䝸䝥䝖) Python ධ㛛 94
  • 96. Python • Ⓩሙ᫬ᮇ: 1990ᖺ • タィ⪅: 䜾䜲䝗䞉䞂䜯䞁䝻䝑䝃䝮 • ᆺ௜䛡: ᙉ䛔ືⓗᆺ௜䛡 Python ධ㛛 96 • 1.x: 1994ᖺ • 2.x: 2000ᖺ䚹䝯䝆䝱䞊ゝㄒ໬ • 3.x: 2008ᖺ䚹ᚋ᪉஫᥮ᛶ䛺䛧
  • 98. Python 䛾ᆺ • 䝴䞊䝄ᐃ⩏䛾ᆺ(Class) • ⤌䜏㎸䜏ᆺ(python䛜ᥦ౪) Python ධ㛛 98 ᩘ್ᆺ -­‐ ᩚᩘ -­‐ 㛗〇ᩘ -­‐ ᾋືᑠᩘⅬ -­‐ 」⣲ᩘ 䝅䞊䜿䞁䝇ᆺ -­‐ ᩥᏐิ -­‐ 䝸䝇䝖 -­‐ 䝍䝥䝹 -­‐ xrangeᆺ 䝬䝑䝥ᆺ -­‐ ㎡᭩ 䝣䜯䜲䝹ᆺ -­‐ 䝣䜯䜲䝹䜸䝤䝆䜵䜽䝖
  • 99. ⏝ㄒ • 䜸䝤䝆䜵䜽䝖 • 䜲䞁䝕䝑䜽䝇 • 䜶䝺䝯䞁䝖 • 䝸䝔䝷䝹 • ௦ධ • ᘬᩘ • ㏉䜚್ Python ධ㛛 99
  • 100. ㎡᭩ᆺ • Key 䛸 Value 䛾䝨䜰䜢ಖᣢ䛩䜛䝕䞊䝍ᆺ • Key䛛䜙Value䜢ྲྀᚓ䛩䜛 Dicconary Python ධ㛛 ㏣ຍ 100 Apple : Red Lemon : Yellow Grape : Purple Apple䛾Ⰽ䛿? Red !! X : Y ྲྀᚓ
  • 101. ㎡᭩䛾᧯స • ㎡᭩䛾ᙧᘧ: {key1:value1, key2:value2} • ್䛾ྲྀᚓ: ㎡᭩䜸䝤䝆䜵䜽䝖[key] • ್䛾㏣ຍ(᭦᪂): ㎡᭩䜸䝤䝆䜵䜽䝖[key] = value • ್䛾๐㝖: del ㎡᭩䜸䝤䝆䜵䜽䝖[key] • key୍ぴ䛾ྲྀᚓ: ㎡᭩䜸䝤䝆䜵䜽䝖.keys() • key䜢ᣢ䛳䛶䛔䜛䛛: ㎡᭩䜸䝤䝆䜵䜽䝖.has_key(key) Python ධ㛛 101
  • 102. ㎡᭩ᆺ䛾᧯స # ㎡᭩䜸䝤䝆䜵䜽䝖䜢సᡂ d = {Apple:Red, Lemon:Yellow} # key 䛛䜙 Value 䜢ྲྀᚓ d[Apple] 'Red' # ㎡᭩䜸䝤䝆䜵䜽䝖䛻せ⣲䜢㏣ຍ d[Grape] = Purple d {'Grape': 'Purple', 'Lemon': 'Yellow', 'Apple': 'Red'} # ㎡᭩䜸䝤䝆䜵䜽䝖䛾せ⣲䜢๐㝖 del d[Apple] d {'Grape': 'Purple', 'Lemon': 'Yellow'} Python ධ㛛 102
  • 103. ㎡᭩䛸䝸䝇䝖䛾㐪䛔 • 䝸䝇䝖ᆺ䛿⥺ᙧ䛻䝕䞊䝍ಖᣢ • ㎡᭩ᆺ䛿䝝䝑䝅䝳䜢฼⏝䛧䛶䝕䞊䝍ಖᣢ • せ⣲ᩘ䛜ቑ䛘䛯ሙྜ䛾᳨⣴䛜㧗㏿ Python ධ㛛 103 䝝䝑䝅䝳䛾௙⤌䜏
  • 104. ㎡᭩ᆺ䛾䝃䞁䝥䝹 • Arp Table 䜢 List 䛸㻌㎡᭩䛷ᐇ⿦(䜽䝷䝇䛺䛧) Python ධ㛛 104 ipList = [] macList = [] def registerMAC1(ip, mac): if ip in ipList: index = ipList.index(ip) macList[index] = mac else: ipList.append(ip) macList.append(mac) def getMAC1(ip): if ip in ipList: index = ipList.index(ip) return macList[index] else: return None arpTable = {} def registerMAC2(ip, mac): arpTable[ip] = mac def getMAC2(ip): return arpTable.get(ip) List䛻䜘䜛ᐇ⿦: ᥈⣴䝁䝇䝖 Order(N) ㎡᭩䛻䜘䜛ᐇ⿦: ᥈⣴䝁䝇䝖 Order(1) List䛻䜘䜛ᐇ⿦ ㎡᭩䛻䜘䜛ᐇ⿦
  • 105. 䝉䝑䝖 • 㔜」䚸㡰ᗎ䛾䛺䛔」ᩘ䛾䝕䞊䝍䜢ಖᣢ䛩䜛ᆺ • Value 䛾䛺䛔 Map 䛻㏆䛔 Python ධ㛛 105 a = set([1,2,3,4,5,3,2]) print(a) set([1, 2, 3, 4, 5]) a = set(hello) b = set(world) print(a) set(['h', 'e', 'l', 'o']) print(b) set(['d', 'r', 'o', 'w', 'l']) print(a b) set(['l', 'o'])
  • 106. 䝉䝑䝖䛾᧯స • ึᮇ໬: set(䝅䞊䜿䞁䝇ᆺ䛾䜸䝤䝆䜵䜽䝖) • ㏣ຍ: set䜸䝤䝆䜵䜽䝖.add(value) • ๐㝖: set䜸䝤䝆䜵䜽䝖.discard(value) Python ධ㛛 106
  • 107. 䝍䝥䝹ᆺ • ୙ྍኚ䛺䝕䞊䝍ᵓ㐀 • せ⣲䛾ᩘ䛜Ỵ䜎䛳䛯䛂୍⤌䛾䝕䞊䝍䛃 Python ධ㛛 107 # show logging Link down OSPF Neighbor XXX down Admin up Link up OSPF Neighbor XXX up .... ⾜ᩘ(せ⣲ᩘ)䛿ఙ䜃䜛 = Listᆺ ♫ဨ᝟ሗ Yuichi Ito 2011-­‐04-­‐01 Tokyo TAC ⾜ᩘ(せ⣲ᩘ)䛿ఙ䜃䛺䛔 㡰୙ྠ = Tuple ᆺ
  • 108. 䝍䝥䝹ᆺ䛾᧯స • Tuple 䛾సᡂ: tupleObject = (elem1, elem2,...) • せ⣲䛾ྲྀᚓ: item = tupleObject[index] • 」ᩘ䛾せ⣲䛾ྲྀᚓ: • せ⣲䛾᭦᪂: 䛷䛝䛺䛔 Python ධ㛛 108
  • 109. 䝍䝥䝹ᆺ䛾᧯స # Tuple 䛾సᡂ yuiito = (Yuichi, Ito, 2011) yuiito ('Yuichi', 'Ito', 2011) # せ⣲䛾ཧ↷ yuiito[1] 'Ito' yuiito = (Yuichi, Ito, 2011) (name, famName, year) = yuiito year 2011 # せ⣲䛾᭦᪂ yuiito[2] = 2009 Traceback (most recent call last): File stdin, line 1, in module TypeError: 'tuple' object does not support item assignment Python ධ㛛 109
  • 110. 䝍䝥䝹ᆺ䝃䞁䝥䝹 • 䝕䞊䝍ᵓ㐀䜢సᡂྍ⬟(䜽䝷䝇䛾⡆᫆∧) • ୙ྍኚ䛺䜸䝤䝆䜵䜽䝖 Python ධ㛛 110 def getMinMax(numList): minNum = numList[0] maxNum = numList[0] for n in numList: if n minNum: minNum = n if n maxNum: maxNum = n return (minNum, maxNum)
  • 111. None • ఱ䜒䛺䛔䛣䛸䜢᫂♧䛩䜛䛯䜑䛾≉Ṧ䛺ᆺ • C, Java 䛾 NULL 䛸䛿㐪䛖䛜䚸䜋䜌ྠ䛨 Python ධ㛛 111
  • 113. 㛵ᩘ • ≉ᐃ䛾ฎ⌮䜢⾜䛖ཷ௜❆ཱྀ • ⣽䛛䛔ฎ⌮䜢㞃ⶸ Python ධ㛛 113 䝥䝻䜾䝷䝮(䝟䞁䛾సᡂ㛵ᩘ) ᡂᯝ≀(䝟䞁) 䛒䜣䜁䜣 䜹䝺䞊䝟䞁
  • 114. 䛒䜣䜁䜣䛾〇㐀ᕤ⛬ Python ධ㛛 114 䛣䛽䜛 ᮦᩱ䜢⏝ព ⢊䛺䛹䜢䜎䛬䜛 䛣䛽䜛 Ⓨ㓝 ⏕ᆅ䜢ᩚᙧ ᨺ⨨ 䛒䜣䜁䜣䛾ᙧస䜚 ⏕ᆅ䜢㐺㔞䛸䜛 㤆䜢䛔䜜䜛 ୸䜑䜛 ↝䛟 䜸䞊䝤䞁䜢 䜑䜛 䝟䞁䜢䛔䜜䜛 ୍ᐃ᫬㛫ᚅ䛴 ྲྀ䜚ฟ䛩
  • 115. 䜹䝺䞊䝟䞁䛾〇㐀ᕤ⛬ Python ධ㛛 115 䛣䛽䜛 ᮦᩱ䜢⏝ព ⢊䛺䛹䜢䜎䛬䜛 䛣䛽䜛 Ⓨ㓝 ⏕ᆅ䜢ᩚᙧ ᨺ⨨ 䜹䝺䞊䝟䞁䛾ᙧస䜚 ⏕ᆅ䜢㐺㔞䛸䜛 䜹䝺䞊䜢䛔䜜䜛 ୸䜑䜛 ᥭ䛢䜛 Ἔ䜢 䜑䜛 䝟䞁䜢䛔䜜䜛 ୍ᐃ᫬㛫ᚅ䛴 ྲྀ䜚ฟ䛩
  • 116. 㛵ᩘ䜢౑䜟䛺䛔䝧䝍᭩䛝 • ฎ⌮䛾༊ษ䜚䛜ศ䛛䜚䛵䜙䛟䚸㔜」䛜ከ䛔 Python ධ㛛 116 ᮦᩱ䜢⏝ព ⢊䜢ΰ䛬䜛 䛣䛽䜛 ⏕ᆅ䜢ᩚᙧ ᨺ⨨ ⏕ᆅ䜢㐺㔞䛸䜛 㤆䜢ධ䜜䜛 ᩚᙧ 䜸䞊䝤䞁䜢 䜑䜛 䝟䞁䜢䛔䜜䜛 ୍ᐃ᫬㛫ᚅ䛴 ྲྀ䜚ฟ䛩 AnPan.py ᮦᩱ䜢⏝ព ⢊䜢ΰ䛬䜛 䛣䛽䜛 ⏕ᆅ䜢ᩚᙧ ᨺ⨨ ⏕ᆅ䜢㐺㔞䛸䜛 䜹䝺䞊䜢ධ䜜䜛 ᩚᙧ Ἔ䜢 䜑䜛 䝟䞁䜢䛔䜜䜛 ୍ᐃ᫬㛫ᚅ䛴 ྲྀ䜚ฟ䛩 CurryPan.py
  • 117. 㛵ᩘ䛻䜘䜛ฎ⌮༢఩䛾ᩚ⌮ Python ධ㛛 117 䛣䛽䜛 Ⓨ㓝 䛒䜣䜁䜣ᙧస䜚 ↝䛟 䜹䝺䞊䝟䞁ᙧస䜚 ᥭ䛢䜛 㔜」䛾᤼㝖
  • 118. 㛵ᩘ䛻䜘䜛䝁䞊䝗䛾ᩚ⌮ Python ධ㛛 118 㛵ᩘ: 䛣䛽䜛 ᮦᩱ䜢⏝ព ⢊䜢ΰ䛬䜛 䛣䛽䜛 㛵ᩘ: Ⓨ㓝 ⏕ᆅ䜢ᩚᙧ ᨺ⨨ 㛵ᩘ: 䛒䜣䜁䜣ᙧస䜚 ⏕ᆅ䜢㐺㔞䛸䜛 㤆䜢ධ䜜䜛 ᩚᙧ 㛵ᩘ: ↝䛟 䜸䞊䝤䞁䜢 䜑䜛 䝟䞁䜢䛔䜜䜛 ୍ᐃ᫬㛫ᚅ䛴 ྲྀ䜚ฟ䛩 ௨ୗ┬␎ Pan.py (䛴䛵䛝) 㛵ᩘ: make䛒䜣䜁䜣 䛣䛽䜛 Ⓨ㓝 䛒䜣䜁䜣ᙧస䜚 ↝䛟 㛵ᩘ:make䜹䝺䞊䝟䞁 䛣䛽䜛 Ⓨ㓝 䜹䝺䞊䝟䞁ᙧస䜚 ᥭ䛢䜛 ….. Pan.py
  • 119. 㛵ᩘྡ • 㛵ᩘ䜢༊ู䛩䜛䛯䜑䛾ྡ๓ • ⤌䜏㎸䜏㛵ᩘ䛸⿕䜙䛺䛔䜘䛖䛻ὀព䛜ᚲせ Python ධ㛛 119
  • 120. ᘬᩘ • 㛵ᩘ䜢࿧䜃ฟ䛩㝿䛻㛵ᩘ䛻୚䛘䜛್ • ௬ᘬᩘ(㛵ᩘ䛾ᐃ⩏ഃ䛷ཷ䛡ྲྀ䜛ኚᩘ) • ᐇᘬᩘ(㛵ᩘ䛾࿧䜃ฟ䛧ഃ䛷୚䛘䜛್) Python ධ㛛 120 def 㛵ᩘྡ(௬ᘬᩘ䠅 㛵ᩘෆ䛾ฎ⌮ 㛵ᩘྡ(ᐇᘬᩘ) ᐃ⩏ ࿧䜃ฟ䛧
  • 121. ㏉䜚್ • 㛵ᩘ䛷ฎ⌮䛥䜜䛯䝕䞊䝍䜢࿧䜃ฟ䛧ඖ䛻㏉䛩 • ㏉䜚್䛜䛺䛔ሙྜ䜒䛒䜛(๪స⏝䛾䜏䛾㛵ᩘ) Python ධ㛛 121 length = len([1,2,3,4,5]) length len([1,2,3,4,5]) 5 ㏉䜚್
  • 123. 㛵ᩘసᡂ䛾᭩ᘧ1 • ᘬᩘ䚸㏉䜚್䛺䛧䛾᭩ᘧ • ㏉䜚್䜢᫂♧䛧䛺䛔ሙྜ䛿 None 䛜㏉䜛 Python ධ㛛 123 def 㛵ᩘྡ(): ฎ⌮ def printHello(): print(hello) printHello() = hello
  • 124. 㛵ᩘసᡂ䛾᭩ᘧ2 • ᘬᩘ䚸㏉䜚್䛒䜚 • ᘬᩘ䛿㛵ᩘྡ䛾ᚋ䛾()䛻ᚲせ䛺ᩘ䛰䛡ᣦᐃ • ㏉䜚್䛿 return 䛷᫂♧ⓗ䛻᭩䛟 Python ධ㛛 124 def 㛵ᩘྡ(ᘬᩘ): ฎ⌮ return ㏉䜚್ def adder(a, b): return a + b print(adder(3,4)) = 7
  • 125. 㛵ᩘ䛾䝃䞁䝥䝹 def power(x): return x*x def absolute(x): if(x0): return -­‐x else: return x print(power(6)) -­‐ 36 print(absolute(-­‐5)) -­‐ 5 Python ධ㛛 125
  • 126. ᘬᩘ䛾䝕䝣䜷䝹䝖್ • ᣦᐃ䛧䛺䛔ሙྜ䛻タᐃ䛥䜜䜛ᘬᩘ Python ධ㛛 126 def func(a, b=1) print(a) print(b) func(5,6) -­‐ 5 6 func(5) -­‐ 5 5
  • 127. global • 㛵ᩘෆ䛷䜾䝻䞊䝞䝹ኚᩘ䛻䜰䜽䝉䝇䛩䜛䛯䜑䛾ᐉゝ Python ධ㛛 127 gv = 0 def func1(): gv = 1 def func2(): global gv gv = 2 print(gv) -­‐ 0 func1() print(gv) -­‐ 0 func2() print(gv) -­‐ 2
  • 129. ₇⩦ • day2-­‐1.py.zip 䜢䝎䜴䞁䝻䞊䝗䛧ゎ෾ • text = ࠥ 䛸䛧䛶஦๓ᐃ⩏䛥䜜䛶䛔䜛ᩥᏐิ䛾 ༢ㄒฟ⌧ᩘ䜢䜹䜴䞁䝖䛩䜛䝥䝻䜾䝷䝮䜢సᡂ • 䛂༢ㄒ:ฟ⌧ᩘ䛃䛸䛔䛖䝣䜷䞊䝬䝑䝖䛷ฟຊ䛩䜛 • ᩥᏐิ䛾ศ๭: ᩥᏐิ.split() -­‐ [༢ㄒ䛾㓄ิ] Ⓨᒎ⦅ Python ධ㛛 dog:1 cat:2 ... 129 • ฟ⌧ᩘ䛜ከ䛔༢ㄒ㡰䛻䝋䞊䝖䛧䛶⾲♧䛩䜛 • 䝠䞁䝖䛿ḟ䝨䞊䝆
  • 130. ₇⩦⿵㊊ • 䝸䝇䝖䜢౑䛳䛯⡆᫆䝋䞊䝖䜰䝹䝂䝸䝈䝮 • sort㛵ᩘ䜢౑䛳䛶䜒ᐇ⿦ྍ⬟䛰䛜䚸䜎䛰ᩍ䛘䛶䛺䛔 䝔䜽䝙䝑䜽䛜ᚲせ Python ධ㛛 130 list1 = [3,6,2,9,0,1,5] list2 = [] while(0 != len(list1)): maxValue = max(list1) index = list1.index(maxValue) list2.append(maxValue) del list1[index] print(list2) -­‐ [9, 6, 5, 3, 2, 1, 0]
  • 132. 䝰䝆䝳䞊䝹䛾ᚲせᛶ • 䝰䝆䝳䞊䝹 • 䝴䞊䝄సᡂ䝰䝆䝳䞊䝹: 1䛴䛾python䝣䜯䜲䝹 Python ධ㛛 132
  • 133. 䝰䝆䝳䞊䝹䜈䛾䜰䜽䝉䝇 • ᶵ⬟䛤䛸䛻䜘䜛䝥䝻䜾䝷䝮䛾ᩚ⌮ Python ධ㛛 133 math file system os ⮬ศ䛜సᡂ ⤌䜏㎸䜏 (䝕䝣䜷䝹䝖䛷 ౑䛘䜛)
  • 135. 䝰䝆䝳䞊䝹䛾ศ๭ • ᕧ኱䛺䝣䜯䜲䝹䛿䛂䛹䛣䛻ఱ䛜䛒䜛䛛䛃䜟䛛䜚䛻䛟䛔 • 㐺ษ䛺⢏ᗘ䛷䝣䜯䜲䝹䜢ศ๭䛩䜛 Python ධ㛛 135 010101010101010101010101 010101010010101010101010 101010101010101010010101 010101010101010101010101 010010101010101010101010 101010101010010101010101 010101010101010101010010 101010101010101010101010 101010010101010101010101 010101010101010010101010 010101010 101010101 010101010 101010010 101010101 010101010 101010101 010101010 101010010 101010101 010101010 101010101 010101010 101010010 101010101 010101010 101010101 010101010 101010010 101010101 ᕧ኱䛺䝣䜯䜲䝹 」ᩘ䛾ᑠ䛥䛺䝣䜯䜲䝹
  • 136. 䝰䝆䝳䞊䝹䛾ཧ↷ Python ධ㛛 136 010101010 101010101 010101010 101010010 101010101 010101010 101010101 010101010 101010010 101010101 010101010 101010101 010101010 101010010 101010101 010101010 101010101 010101010 101010010 101010101 010101010 101010101 010101010 101010010 101010101 010101010 10101010䠍 䝰䝆䝳䞊䝹䜢౑䛖䝥䝻䜾䝷䝮 ඲య䛾ὶ䜜䜢グ㏙ 䝰䝆䝳䞊䝹䜢౑䛖䝥䝻䜾䝷䝮 ඲య䛾ὶ䜜䜢グ㏙
  • 137. import • import 䛩䜛䛣䛸䛷䝰䝆䝳䞊䝹䛜౑䛘䜛 Python import mymath print(mymath.add(2,3)) # -­‐ 5 print(mymath.minus(2,3)) # -­‐ -­‐1 print(mymath.mulcply(2,3)) # -­‐ 6 print(divide(2,3)) #ERROR HAPPEN Traceback (most recent call last): File ~/main.py, line 6, in module print(divide(2,3)) NameError: name 'divide' is not defined ධ㛛 137 def add(a,b): return a + b def minus(a, b): return a -­‐ b def mulcply(a, b): return a * b def divide(a, b): return a / b
  • 138. from • from 䝣䜯䜲䝹ྡ(䝟䝑䜿䞊䝆ྡ) import * 䛷䝣䜯䜲䝹ྡ 䛾ᣦᐃ䛺䛧䛻࿧䜃ฟ䛧䛜ྍ⬟䛻䛺䜛 Python ධ㛛 138 def add(a,b): return a + b def minus(a, b): return a -­‐ b def mulcply(a, b): return a * b def divide(a, b): return a / b from mymath import * print(add(2,3)) # -­‐ 5 print(minus(2,3)) # -­‐ -­‐1 print(mulcply(2,3)) # -­‐ 6 print(mymath.divide(2,3)) # ERROR HAPPEN Traceback (most recent call last): File ~/main.py, line 6, in module print(divide(2,3)) NameError: name 'mymath' is not defined
  • 139. (⿵㊊) ᚠ⎔ཧ↷ • 䝣䜯䜲䝹A䛸B䛜஫䛔䛻ཧ↷䛧䛒䛳䛶䛔䜛 • ᮃ䜎䛧䛟䛺䛟䚸䜶䝷䞊䛻䛺䜛ሙྜ䛜䛒䜛 Python ධ㛛 139 import file2 print(file1) import file1 print(file2) file1.py $ python file1.py file1 file2 file1 file2.py 䛺䜣䛷2ᅇ?
  • 141. ᩥᏐิ䛾ฎ⌮1 • ᩥᏐิ䛾సᡂ: str1=ᩥᏐิ, str2=' ᩥᏐิ' • ᩥᏐิ䛾⤖ྜ: ᩥᏐิ + ᩥᏐิ • 䜶䝇䜿䞊䝥グྕ: 䛻⥆䛡䛶≉ᐃ䛾ᩥᏐ • 㛗䛥䛾ྲྀᚓ: len(ᩥᏐิ) Python ධ㛛 141
  • 142. 䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮1 # ᩥᏐิ䛾సᡂ str1 = hello str2 = ' world' # ⤖ྜ print(str1 + str2) hello world # 䜶䝇䜿䞊䝥 print(str1 + t + str2 + ncisco) hello world cisco # 㛗䛥䛾ྲྀᚓ len(str1) 5 Python ධ㛛 142
  • 143. ᩥᏐิ䛾ฎ⌮2 • ⧞䜚㏉䛧: ᩥᏐิ * ⧞䜚㏉䛧ᅇᩘ • ᩥᏐิ໬: str(ᩥᏐิ௨እ䛾䜸䝤䝆䜵䜽䝖) • ᩥᏐิ䛾୍ᩥᏐ䜢ྲྀᚓ㻌ᩥᏐิ[index] • ᩥᏐิ䛾⠊ᅖྲྀᚓ: ᩥᏐิ[start:end] Python ධ㛛 143
  • 144. 䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮2 # ⧞䜚㏉䛧 print(cisco * 3) ciscociscocisco # ᩥᏐิ໬ print(value: + str(5)) # 㠀ᩥᏐิ䜢ᩥᏐิ໬䛧䛶䛛䜙⤖ྜ value: 5 # ᩥᏐิ䛾୍ᩥᏐ䜢ྲྀᚓ hello world[4] 'o' # ᩥᏐิ䛾୍㒊䜢ྲྀᚓ hello world[4:8] 'o wo' Python ධ㛛 144
  • 145. ᩥᏐิ䛾ฎ⌮3 • ⨨䛝᥮䛘: ᩥᏐิ.replace(old, new) • ศ๭: ᩥᏐิ.split(༊ษ䜚ᩥᏐ) • ྵ䜐䛛: ᥈䛩ᩥᏐิ in ᥈䛥䜜䜛ᩥᏐิ • ఩⨨: ᩥᏐิ.find(᥈䛩ᩥᏐ) Python ධ㛛 145
  • 146. 䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮3 # ⨨䛝᥮䛘 hello world.replace(world, python) 'hello python' # ศ๭ hello world.split( ) ['hello', 'world'] # ྵ䜐䛛 hell in hello world True # ఩⨨ hello world.find(o) 4 hello world.find(X) -­‐1 # ᣦᐃ䛧䛯ᩥᏐิ䛜ྵ䜎䜜䛶䛔䛺䛔ሙྜ䛿 -­‐1 Python ධ㛛 146
  • 148. ṇつ⾲⌧䛸䛿 • ᩥᏐิ䝟䝍䞊䞁䛾ᐃ⩏ • ᰂ㌾䛻ᩥᏐิ䛾䝟䝍䞊䞁䝬䝑䝏䞁䜾䜢⾜䛖䛣 䛸䛜䛷䛝䜛 – 䝟䝍䞊䞁䝬䝑䝏䞁䜾䛻ヱᙜ䛩䜛䛛 – ヱᙜ䛩䜛ᩥᏐิ䛾ྲྀᚓ Python ධ㛛 148
  • 149. ṇつ⾲⌧䛾᭷ຠᛶ • ᩥᏐิ䛜ᩚᩘ䛛䛹䛖䛛ุᐃ䛩䜛䛯䜑䛾䝥䝻 䜾䝷䝮䜢సᡂ䛩䜛 • ṇつ⾲⌧䛾᭷↓䛷䝁䞊䝗㔞䛜኱ᖜ䛻㐪䛖 Python ධ㛛 149 def isIntegerString(string): numberList = ['1','2','3','4','5','6','7','8','9','0'] if len(string) == 0: return false for char in string: if char not in numberList: return False return True import re def isIntegerString2(string): return re.match('^[d]*$', string) != None ṇつ⾲⌧䛺䛧 ṇつ⾲⌧䛒䜚
  • 150. python 䛾ṇつ⾲⌧సἲ • re: ṇつ⾲⌧䛾䝟䝑䜿䞊䝆 import re • match : ྜ⮴䛩䜛ᩥᏐิ⩌䜢ᢤ䛝ฟ䛩 re.match(䝟䝍䞊䞁ᩥᏐิ, ᩥᏐิ) • search: ྜ⮴䛩䜛ᩥᏐิ䛾఩⨨䜢㏉䛩 re.search(䝟䝍䞊䞁ᩥᏐิ䚸ᩥᏐิ) Python ධ㛛 150
  • 151. ṇつ⾲⌧ Python ධ㛛 151 m = re.match(^[d]*$, 12345) print(m) _sre.SRE_Match object at 0x1005bdbf8 m.group() '12345' m.start(), m.end() (0, 5)
  • 153. 䝣䜯䜲䝹ฎ⌮䛾ὶ䜜 • 䝣䜯䜲䝹ฎ⌮䛾ὶ䜜 1. file䜢䜸䞊䝥䞁䛧䛶 file䜸䝤䝆䜵䜽䝖䜢ྲྀᚓ 2. file䜸䝤䝆䜵䜽䝖䛻ᑐ䛧䛶ฎ⌮䜢⾜䛖 3. file䜢䜽䝻䞊䝈䛩䜛 Python ධ㛛 153
  • 154. 䝣䜯䜲䝹䜸䝤䝆䜵䜽䝖 • 䝣䜯䜲䝹䜸䝤䝆䜵䜽䝖䛾సᡂ open(䝣䜯䜲䝹䝟䝇, 䜸䞊䝥䞁䛩䜛䝰䞊䝗) – 䝣䜯䜲䝹䝟䝇: ┦ᑐ or ⤯ᑐ – 䝰䞊䝗(䝕䝣䜷䝹䝖䛿r) – r: ㄞ䜏㎸䜏䝰䞊䝗 – w: ᪂つ᭩䛝㎸䜏䝰䞊䝗 – a: ㏣ຍ᭩䛝㎸䜏䝰䞊䝗 • 䝣䜯䜲䝹䜸䝤䝆䜵䜽䝖䛾䜽䝻䞊䝈 f.close() Python ධ㛛 154
  • 155. ㄞ䜏㎸䜏ฎ⌮ • Read – readline(): ୍⾜ㄞ䜏㎸䜏 – readlines(): ඲䛶䛾⾜䜢 list 䛸䛧䛶ㄞ䜏㎸䜏 Python ධ㛛 155 f = open(file.txt, r) f.readline() 'hello world1n' f.readline() 'hello world2n' f.readlines() ['hello world3n', 'hello world4n'] hello world1 hello world2 hello world3 hello world4 file.txt
  • 156. ᭩䛝㎸䜏ฎ⌮ • Write – write(ᩥᏐิ) : ᩥᏐิ䜢᭩䛝㎸䜏 – writelines(ᩥᏐิ䛾䝸䝇䝖) : 䝸䝇䝖䜢᭩䛝㎸䜏 – flush() : 䝞䝑䝣䜯䜢䝕䜱䝇䜽䛻䝣䝷䝑䝅䝳 Python ධ㛛 156 f2 = open(file2.txt, w) f2.write(hello) f2.write(world) f2.write(n) wlist = [abc, def, ghi] f2.writelines(wlist) f2.flush() f2.close() helloworld abcdefghi file2.txt
  • 157. ୖ᭩䛝䛸㏣グ • ୖ᭩䛝: 䝣䜯䜲䝹䛾ෆᐜ䜢ᾘ䛧䛶䛛䜙᭩䛝㎸䜐 • ㏣グ: 䝣䜯䜲䝹䛾ᮎᑿ䛻㏣ຍ䛷᭩䛝㎸䜐 Python ධ㛛 157
  • 159. Pickel • 䝥䝻䜾䝷䝮䛾ᐇ⾜≧ែ䛿䝥䝻䜾䝷䝮䜢೵Ṇ䛩䜛䛸ᾘኻ • Pickel 䛿䜸䝤䝆䜵䜽䝖䜢䝣䜯䜲䝹䛻Ọ⥆ⓗ䛻ಖᏑ䛩䜛ᡭἲ • Pickel – dump: 䜸䝤䝆䜵䜽䝖䜢䝣䜯䜲䝹䛻᭩䛝ฟ䛧 – load: 䝣䜯䜲䝹䛛䜙䜸䝤䝆䜵䜽䝖䜢ㄞ䜏ฟ䛧 Python ධ㛛 159
  • 160. Pickel Python ධ㛛 160 yuichi$ python data = [hello, world, 1, 2, 3] import pickle f = open(dump.txt, w) pickle.dump(data, f) f.close() exit() (lp0 S'hello' p1 aS'world' p2 aI1 aI2 aI3 a. $ python f = open(dump.txt) import pickle data = pickle.load(f) print(data) ['hello', 'world', 1, 2, 3] dump.txt
  • 162. 䝟䝇 • OSୖ䛾䝣䜯䜲䝹䜔䝕䜱䝺䜽䝖䝸䛾ᡤᅾᆅ䜢♧䛩 Python ධ㛛 162 /Users/yuichi/Documents/python.pptx / Users/ yuichi/ Documents/ python.pptx etc/ var/ Shared/ Desktop/ Pictures/ java.pptx VM/
  • 163. ⤯ᑐ䝟䝇䛸┦ᑐ䝟䝇 • ⤯ᑐ䝟䝇: 䝹䞊䝖䛛䜙㎺䜛䝟䝇 • ┦ᑐ䝟䝇: ⌧ᅾ䛾⮬ศ䛾఩⨨䛛䜙㎺䜛䝟䝇 – ./ : ⮬ศ䛜䛔䜛䝕䜱䝺䜽䝖䝸 – ../ : ⮬ศ䛾ୖ䛾㝵ᒙ䛾䝕䜱䝺䜽䝖䝸 Python ධ㛛 163 / Users/ yuichi/ Documents/ python.pptx etc/ var/ Shared/ Desktop/ test.txt java.pptx VM/ /Users/yuichi/test.txt ../test.txt
  • 164. 䝟䝇䛾ྲྀᚓ • ⌧ᅾ䛾䜹䝺䞁䝖䝕䜱䝺䜽䝖䝸䜢ྲྀᚓ: os.getcwd() • 䝕䜱䝺䜽䝖䝸䛾⛣ື: os.chdir(⛣ືඛ䛾䝟䝇) • ⤯ᑐ䝟䝇䛾ྲྀᚓ: os.path.abspath(path) • OS㠀౫Ꮡ䛾䝟䝇䛾ྲྀᚓ᪉ἲ㻌os.path.join(TUPLE) Python ධ㛛 164 yuichi$ ls | grep Documents Documents yuichi$ python import os os.getcwd() '/Users/yuichi' os.path.abspath(Documents) '/Users/yuichi/Documents' os.path.join(/,dir1,dir2,file1) '/dir1/dir2/file1'
  • 165. 䝟䝇䛻㛵䛩䜛㛵ᩘ • 䝣䜯䜲䝹㻌or 䝕䜱䝺䜽䝖䝸䛾Ꮡᅾ☜ㄆ: os.path.exists(䝟䝇) • 䝕䜱䝺䜽䝖䝸䛛: os.path.isdir(䝟䝇) • 䝣䜯䜲䝹䛛: os.path.isfile(䝟䝇) Python ධ㛛 165
  • 166. 䝟䝇䛾᧯స㛵ᩘ Python ධ㛛 166 os.path.exists(/Users/yuichi) True os.path.isfile(/Users/yuichi) False os.listdir(./) ['dir1', 'file1'] os.mkdir(dir2) os.rmdir(dir2)
  • 167. 䝕䜱䝺䜽䝖䝸䛾᧯స㛵ᩘ • 䝕䜱䝺䜽䝖䝸䛾సᡂ: os.mkdir(䝕䜱䝺䜽䝖䝸䛾䝟䝇) • 䝕䜱䝺䜽䝖䝸䛾๐㝖: os.rmdir(䝕䜱䝺䜽䝖䝸䛾䝟䝇) • 䝕䜱䝺䜽䝖䝸䛾୰㌟୍ぴ: os.listdir(䝟䝇) • 䝣䜯䜲䝹䚸䝕䜱䝺䜽䝖䝸䛾๐㝖: os.remove(䝟䝇) • 䝕䜱䝺䜽䝖䝸䛾෌ᖐⓗ๐㝖: os.removedirs(䝟䝇) Python ධ㛛 167
  • 169. 䝅䜵䝹䝇䜽䝸䝥䝖 • 䝥䝻䜾䝷䝭䞁䜾: C 䜔 Java 䛾䜘䛖䛺䝅䜵䝹䜢ព㆑䛧䛺 䛔䝇䝍䜲䝹 • 䝅䜵䝹䝇䜽䝸䝥䝖: 䝅䜵䝹⮬య䜢᧯స䛩䜛䝥䝻䜾䝷䝭䞁 䜾䝇䝍䜲䝹 Python ධ㛛 169 import os currentDir = os.getcwd() files = os.listdir(currentDir) for fname in files: print(fname) import commands files = commands.getoutput(ls).split() for fname in files: print(fname) ㏻ᖖ䛾䝥䝻䜾䝷䝭䞁䜾䝇䝍䜲䝹 䝅䜵䝹䝇䜽䝸䝥䝖䛾䝇䝍䜲䝹
  • 170. 䝅䜵䝹䝇䜽䝸䝥䝖グ㏙䛾䝁䝒 • ྍ⬟䛺㝈䜚᪤Ꮡ䛾䝁䝬䞁䝗䜢౑䛖 • 䝁䝬䞁䝗䛸䝁䝬䞁䝗䛾᥋⥆䛻 python 䜢౑䛖 Python ධ㛛 170 (1) ⤖ᯝ = 䝁䝬䞁䝗䛾Ⓨ⾜ (2) ⤖ᯝ䜢ฎ⌮ (3) ฎ⌮⤖ᯝ䛻䜒䛸䛵䛔䛶ḟ䛾䝁䝬䞁䝗䜢సᡂ (4) (1)䜈
  • 171. 䝁䝬䞁䝗䛾࿧䜃ฟ䛧 Python ධ㛛 171 • os.system(䝁䝬䞁䝗) : ฟຊ䛜୙せ䛺ሙྜ • commands.getoutput(䝁䝬䞁䝗) : ฟຊ䛜ᚲせ䛺ሙྜ commands.getoutput(ls).split(n) ['CONFIG_FILE', 'Cisco Mac Support.webloc', 'Desktop', 'Documents', 'Downloads', 'Dropbox', 'Library', 'MATERIAL', 'Movies', 'Music', 'Pictures', 'Public', 'SR', 'STP-­‐BA-­‐Dispute_Japan-­‐ RSVT.pptx', 'VM', 'appProperces', 'bingo.py', 'bingo.pyc', 'dump.txt', 'file.txt', 'file2.txt', 'get-­‐fpath-­‐info.py', 'mki-­‐fp-­‐ capture.zip', 'tcpdump', 'tcpdump.zip', 'test', 'unctled folder']
  • 172. command.getoutput() Python ධ㛛 172 import commands result = commands.getoutput(ping -­‐c 5 cisco.com) lines = result.split(n) for line in lines: print(line) PING cisco.com (72.163.4.161): 56 data bytes 64 bytes from 72.163.4.161: icmp_seq=0 7l=239 cme=195.865 ms 64 bytes from 72.163.4.161: icmp_seq=1 7l=239 cme=200.497 ms 64 bytes from 72.163.4.161: icmp_seq=2 7l=239 cme=197.125 ms 64 bytes from 72.163.4.161: icmp_seq=3 7l=239 cme=197.620 ms 64 bytes from 72.163.4.161: icmp_seq=4 7l=239 cme=197.553 ms -­‐-­‐-­‐ cisco.com ping stacsccs -­‐-­‐-­‐ 5 packets transmi7ed, 5 packets received, 0.0% packet loss round-­‐trip min/avg/max/stddev = 195.865/197.732/200.497/1.519 ms
  • 173. exping 䛾ᐇ⿦ • 」ᩘ䛾ᐄඛ䛾ping฿㐩⋡䜢 ᐃ Python ධ㛛 173 import commands def ping(dest): result = commands.getoutput(ping -­‐c 5 {}.format(dest)) lines = result.split(n) length = len(lines) packetLoss = lines[length -­‐2].split()[6] r7 = lines[length -­‐1].split()[3].split(/)[1] return (dest, packetLoss, r7) dests = [cisco.com, google.com, yahoo.com] for dest in dests: print(ping(dest)) ('cisco.com', '0.0%', '196.756') ('google.com', '0.0%', '13.317') ('yahoo.com', '0.0%', '262.060')
  • 174. Linux䝩䝇䝖ୖ䛷䛾ping • ≉ᐃIP䜰䝗䝺䝇䛾䝩䝇䝖㛫䛾䜏㏻ಙ䛷䛝䛺䛔 • ஦㇟Ⓨ⏕☜❧䛿㻌1/16 ⛬ᗘ VLAN X VLAN Y Nexus5500 Primary Nexus5500 Secondary Python ධ㛛 174 Nexus2000 FEX L2 Switch Source Dest VPC(VSS) 䝩䝇䝖50ྎ 䝩䝇䝖50ྎ 50 x 50 -­‐ 2500 flow
  • 177. ₇⩦ • ᮍ᏶ᡂ䛾䝻䜾ᢳฟ䝥䝻䜾䝷䝮䜢᏶ᡂ䛥䛫䜛 • 䝞䜾䛿3⟠ᡤ䛻䛒䜛 yuichi$ python log.py -­‐f log.log -­‐s 2014 Jan 28 12:30:50 -­‐e 2014 Jan 29 16:06:15 -­‐k port-­‐channel -­‐f : Python ධ㛛 177 䝣䜯䜲䝹ྡ -­‐s : 䝇䝍䞊䝖᫬้ -­‐e : ⤊஢᫬้ -­‐k : 䜻䞊䝽䞊䝗 dhcp-­‐10-­‐141-­‐56-­‐250:Documents yuichi$ python log.py -­‐f log.log -­‐s 2014 Jan 28 12:30:50 -­‐e 2014 Jan 29 16:06:15 -­‐k port-­‐channel 2014 Jan 29 12:14:20 N6K-­‐2 %ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: isis_fabricpath-­‐default [5150] P2P adj L1 n7kb-­‐MKI-­‐FP over port-­‐channel50 -­‐ DOWN (New) on MT-­‐0 2014 Jan 29 12:14:20 N6K-­‐2 %ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: isis_fabricpath-­‐default [5150] P2P adj L1 n7kb-­‐MKI-­‐FP over port-­‐channel50 -­‐ INIT on MT-­‐0 2014 Jan 29 12:14:20 N6K-­‐2 %ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: isis_fabricpath-­‐default [5150] P2P adj L1 n7kb-­‐MKI-­‐FP over port-­‐channel50 -­‐ UP on MT-­‐0 2014 Jan 29 12:24:58 N6K-­‐2 %ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: isis_fabricpath-­‐default [5150] P2P adj L1 n7kb-­‐MKI-­‐FP over port-­‐channel50 -­‐ DOWN (Delete All) on MT-­‐0 2014 Jan 29 12:27:01 N6K-­‐2 %ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: isis_fabricpath-­‐default [5150] P2P adj L1 n7kb-­‐MKI-­‐FP over port-­‐channel50 -­‐ DOWN (New) on MT-­‐0 2014 Jan 29 12:27:01 N6K-­‐2 %ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: isis_fabricpath-­‐default [5150] P2P adj L1 n7kb-­‐MKI-­‐FP over port-­‐channel50 -­‐ INIT on MT-­‐0 2014 Jan 29 12:27:01 N6K-­‐2 %ISIS_FABRICPATH-­‐5-­‐ADJCHANGE: isis_fabricpath-­‐default [5150] P2P adj L1 n7kb-­‐MKI-­‐FP over port-­‐channel50 -­‐ UP on MT-­‐0
  • 178. 䝁䞊䝗(䝞䜾䛿2⟠ᡤ) Python ධ㛛 178 import sys, os def getTimeString(line): return line[0:20] def parseTime(string): monthDict = {Jan:1, Feb:2, Mar:3, Apr:4, May:5, Jun:6, Jul:7, Aug:8, Sep:9, Oct:10, Nov:11, Dec:12} d1 = string.split( ) d2 = d1[3].split(:) return (int(d1[0]), monthDict[d1[1]], int(d1[2]), int(d2[0]), int(d2[1]), int(d2[2])) def printFile(fname, start, end, keyword): if(not os.path.isfile(fname)): print(File {} isn't exist. exit.format(fname)) sys.exit() f = open(fname) for line in f.readlines(): line = line.rstrip() t = parseTime(getTimeString(line)) within = (start = t) and (t = end) contains = keyword in line if(within and contains): print(line) 䝁䝬䞁䝗ᘬᩘ䛾ึᮇ್ fname = start = 1900 Jan 1 00:00:00 end = 2100 Jan 1 00:00:00 keyword = for index in range(len(sys.argv)): arg = sys.argv[index] if(arg == -­‐f): fname = sys.argv[index + 1] elif(arg == -­‐s): start = sys.argv[index + 1] elif(arg == -­‐k): keyword = sys.argv[index + 1] if(fname == ): s1 = syntax: python log.py -­‐f FILENAME -­‐s s2 = START_TIME -­‐e END_TIME -­‐k KEYWORD print(s1 + s2) print(START_TIME, END_TIME, KEYWORD are opcon.) else: printFile(fname, start, end, keyword) 䝻䜾䛾୍⾜䛛䜙᫬㛫䛾ᩥᏐิ䜢ᢳฟ ᢳฟ䛧䛯ᩥᏐิ䜢ẚ㍑ྍ⬟䛺䝍䝥䝹䛻ኚ᥮ ᮲௳䛻䝬䝑䝏䛧䛯⾜䜢printฟຊ 䝁䝬䞁䝗ᘬᩘ䜢ㄞ䜏ྲྀ䜛 䝣䜯䜲䝹ྡ䛜୚䛘䜙䜜䛶䛔䛯䜙䝥䝻䜾䝷䝮䜢ᐇ⾜
  • 179. ₇⩦ Python ධ㛛 179 Ⓨᒎ⦅ 䜻䞊䝽䞊䝗䜢ṇつ⾲⌧䛷ᣦᐃ䛷䛝䜛䜘䛖䛻䛩䜛 ṇつ⾲⌧䛻䝬䝑䝏䛧䛯ሙྜ䛾䜏⾲♧䜢⾜䛖 䜸䝥䝅䝵䞁 -­‐r 䛻⥆䛡䛶ṇつ⾲⌧䜢ᣦᐃ
  • 180. ₇⩦ • 䝷䝪䛾Linux(10.71.224.172)䛻䝻䜾䜲䞁(guest, c1sco123)䛧䚸௨ୗ䛾䝇䜽䝸䝥䝖䜢సᡂ䛧ᐇ⾜䛩䜛 • 䝣䜯䜲䝹ྡ䛿 cecid.py 䛸䛩䜛 Python ධ㛛 180 ௨ୗ䛾ᶵჾ䛾㛤䛔䛶䛔䜛䝫䞊䝖୍ぴ䜢ྲྀᚓ䛩䜛 -­‐ rws1 (1.0.0.1) -­‐ gateway (1.0.0.100) -­‐ linux( 1.110.120.173) -­‐ esxi ( 10.71.224.170) -­‐ Nexus7000 (1.110.8.1)
  • 181. ฼⏝䛩䜛䝁䝬䞁䝗 Python ධ㛛 181 yuichi$ ssh -­‐l guest 10.71.224.172 pass: c1sco123 [guest@fedora172 ~]$ nmap 127.0.0.1 Starcng Nmap 6.01 ( h7p://nmap.org ) at 2014-­‐02-­‐08 13:05 EST Nmap scan report for fedora172.localdomain (127.0.0.1) Host is up (0.0016s latency). Not shown: 995 closed ports PORT STATE SERVICE 22/tcp open ssh 23/tcp open telnet 25/tcp open smtp 111/tcp open rpcbind 631/tcp open ipp
  • 182. ⾲♧ᙧᘧ Python ධ㛛 182 䜰䝗䝺䝇1 TCP: xxx, yyy, zzz, ... UDP: xxx, yyy, zzz, ... 䜰䝗䝺䝇2 TCP: xxx, yyy, zzz, ... UDP: xxx, yyy, zzz, ... ... 䜰䝗䝺䝇3 TCP: xxx, yyy, zzz, ... UDP: xxx, yyy, zzz, ...
  • 183. ḟᅇண࿌ • 䜸䝤䝆䜵䜽䝖ᣦྥ • 䜽䝷䝇 • 䜰䝹䝂䝸䝈䝮 • pexpect 䛻䜘䜛Ciscoᶵჾ䛾䝥䝻䜾䝷䝮᧯స Python ධ㛛 183
  • 185. Python ධ㛛 Learning to Program For Cisco Network Engineers Day 3/4 Japan TAC DC Team. Yuichi Ito
  • 186. Agenda Day 3 • 䜽䝷䝇䛾ᴫᛕ • 䜽䝷䝇䛾సᡂ • ₇⩦ • 䝗䝷䜽䜶䛾ᐇ⿦䛷Ꮫ䜆䜸䝤䝆䜵䜽䝖ᣦྥ • ₇⩦1-­‐3 • pexpect 䛻䜘䜛 Cisco ᶵჾ䛾᧯సἲ • ₇⩦ Python ධ㛛 186
  • 188. 䝥䝻䜾䝷䝭䞁䜾䛾ὶὴ • ᡭ⥆䛝ᆺ • 㛵ᩘᆺ • 䜸䝤䝆䜵䜽䝖ᣦྥᆺ Python ධ㛛 188
  • 190. 䛺䛬䜸䝤䝆䜵䜽䝖ᣦྥ䛜ᚲせ䛛 䛒䛘䛶䝁䞊䝗グ㏙సἲ䛻䝹䞊䝹䜢ㄢ䛩䛣䛸䛷䝥䝻 䜾䝷䝮䛾䜹䜸䝇໬䜢㜵䛠 -­‐ ᐇ⿦䛾ቃ⏺䛜᫂☜䛻䛺䜛 Python ධ㛛 190 䝹䞊䝹䛻䛧䛯䛜䛳䛶 䝥䝻䜾䝷䝮䜢ᩚ⌮
  • 191. 䛺䛬䜸䝤䝆䜵䜽䝖ᣦྥ䛜ᚲせ䛛 䜸䝤䝆䜵䜽䝖ᣦྥ䛾ᶵ⬟䛷㛤Ⓨຠ⋡䜢ྥୖ䛥䛫䜛 -­‐ ⥅ᢎ䛻䜘䜛ᶵ⬟䛾ᘬ䛝⥅䛞 -­‐ 䝫䝸䝰䞊䝣䜱䝈䝮 Python ධ㛛 191 Bu7on Hello World 䝪䝍䞁䜢ᢲ䛧䛯䜙 hello world 䛸 ⾲♧䛩䜛䝥䝻䜾䝷䝮 10ᩘ⾜䛷స䜜䜛(⥅ᢎ䛾䛚䛛䛢)
  • 192. ே㛫䛾ᛮ⪃᪉ἲ • 䛂ㄡ䛜ఱ䜢䛩䜛䛛䛃䛜㔜せ • ㄡ䛻䛹䛖䛔䛖䛂䝯䝑䝉䞊䝆䛃䜢㏦䛳䛯䜙䚸䛹䛖䛔䛖䛂⤖ ᯝ䛃䛜ᚓ䜙䜜䜛䛛 Python ධ㛛 [ෆ㒊ฎ⌮] ௙஦䜢䛩䜛 192 [䝯䝑䝉䞊䝆] 䛣䛾௙஦䜢䛧䛶䛟䛰䛥䛔 [⤖ᯝ] ୖྖ ᡂᯝ≀䛿䛣䜜䛷䛩䚹 㒊ୗ
  • 193. 㛵ᩘ㻌vs 䝯䝋䝑䝗 䛂௙஦䜢䛥䛫䜛䛃(㒊ୗA䚸௙஦) Python ධ㛛 193 㛵ᩘ䝏䝑䜽䛺⪃䛘᪉ 䜸䝤䝆䜵䜽䝖ᣦྥ䛾⪃䛘᪉ 䛂㒊ୗA䛃䛻䛂௙஦䜢䛥䛫䜛䛃(௙஦)
  • 194. 㛵ᩘ㻌vs 䝯䝋䝑䝗 • 㛵ᩘ: 䜸䝤䝆䜵䜽䝖䜢䛂ᘬᩘ䛸䛧䛶䛃౑䛖 • 䝯䝋䝑䝗: 䜸䝤䝆䜵䜽䝖䛂䛛䜙䛃ฎ⌮䜢࿧䜃ฟ䛩 Python ධ㛛 194 string = hello world python # 㛵ᩘ length = len(string) # 䝯䝋䝑䝗 words = string.split() ᩥᏐิ䛾ฎ⌮
  • 195. 㛵ᩘ㻌vs 䝯䝋䝑䝗 • 㛵ᩘ: 䝕䞊䝍䛸ฎ⌮䛾⤖䜃䛴䛝䛜ᙅ䛔 • 䝯䝋䝑䝗: 䝕䞊䝍䛸ฎ⌮䛾⤖䜃䛴䛝䛜ᙉ䛔 㛵ᩘ 䝯䝋䝑䝗 Python ධ㛛 195 len( ) string.split() ฎ⌮䛻䝕䞊䝍䜢୚䛘䜛 䝕䞊䝍䛻ฎ⌮䜢䛥䛫䜛 len 䛿ఱ䛻䛷䜒౑䛘䜛 split 䛿 string 䛻䛧䛛౑䛘䛺䛔
  • 196. 䜸䝤䝆䜵䜽䝖ᣦྥ • ㄡ(䜲䞁䝇䝍䞁䝇)䛻䝯䝑䝉䞊䝆䜢㏦䜛(䝯䝋䝑䝗䜢࿧䜃 ฟ䛩)䛸䛹䛖䛔䛖⤖ᯝ(㏉䜚್)䛜ᚓ䜙䜜䜛䛛 • ⣽䛛䛔ෆ㒊䛾௙⤌䜏䛿Ẽ䛻䛧䛺䛔 グ㏙䝹䞊䝹 Python ධ㛛 196 ㏉䜚್ = ㄡ.䝯䝋䝑䝗(䝟䝷䝯䞊䝍)
  • 197. ฎ⌮䚸᝟ሗ䛾㞃ⶸ • ෆ㒊䛾⣽䛛䛔ᣲື䛿⪃䛘䛪≧ែ䛾䜏䜢ព㆑䛩䜛 䝕䞊䝍䜢HDD䛻᭩䛝㎸䜏䛯䛔 (1) Block N 䛻᭩䛝㎸䜏 䝇䝖䝺䞊䝆䜸䝤䝆䜵䜽䝖 ᭩䛝㎸䜏ฎ⌮ (1)Block N 䛾Disk 䜢㑅ᢥ (2)Block N 䛾䝉䜽䝍䜢ᣦᐃ (3)᭩䛝㎸䜏䜢⾜䛺䛖 䝴䞊䝄䜰䝥䝸䜿䞊䝅䝵䞁䛾 (ᑓ㛛ᐙ䛜䝥䝻䜾䝷䝮䜢సᡂ) 䜸䝤䝆䜵䜽䝖 (䝇䝖䝺䞊䝆䜢౑䛖䛰䛡) 䈜 ⌧ᅾ䛾䝦䝑䝗఩⨨䛺䛹䛿Ẽ䛻䛧䛺䛔
  • 198. 䜎䛸䜑 • 䜸䝤䝆䜵䜽䝖ᣦྥ䛾䝁䞁䝉䝥䝖 – 䛂ㄡ䛜ఱ䜢䛩䜛䛛䛃 – 䛂ㄡ䛜ㄡ䛸䛹䛖༠ຊ䛩䜛䛛䛃 – 䛂」㞧䛺ฎ⌮䚸䝕䞊䝍䛿እ㒊䛻ぢ䛫䛺䛔䛃 • 䝁䞊䝗䛾⛛ᗎ໬ – ᶒ㝈䜢ᣢ䛯䛺䛔䜒䛾䛿᧯స䛜䛷䛝䛺䛔 Python ධ㛛 198
  • 200. 䜽䝷䝇 • 䜸䝤䝆䜵䜽䝖䛾✀㢮 • 䜽䝷䝇䛿ᒓᛶ䛸䛧䛶䝕䞊䝍䛸ฎ⌮䜢ᣢ䛴 Python ධ㛛 200 ᒓᛶ [䝕䞊䝍] -­‐ 䜺䝋䝸䞁䛾㔞 [ฎ⌮] -­‐ ㉮䜛 -­‐ Ṇ䜎䜛 -­‐ ᭤䛜䜛
  • 201. 䜲䞁䝇䝍䞁䝇 • 䜽䝷䝇⮬య䛷ฎ⌮䛩䜛䛾䛷䛿䛺䛟䚸䜽䝷䝇䛛䜙స䜛䜲 䞁䝇䝍䞁䝇䛷ฎ⌮䜢䛩䜛 • 㒊ୗ䛸䛔䛖ᴫᛕ(䜽䝷䝇)䛷䛿䛺䛟㒊ୗ䛾䖣䖣(䜲䞁䝇䝍 䞁䝇)䛜௙஦䜢䛩䜛 Python ධ㛛 [ෆ㒊ฎ⌮] ௙஦䜢䛩䜛 201 [䝯䝑䝉䞊䝆] 䛣䛾௙஦䜢䛧䛶䛟䛰䛥䛔 [⤖ᯝ] 䜽䝷䝇: ୖྖ ᡂᯝ≀䛿䛣䜜䛷䛩䚹 䜲䞁䝇䝍䞁䝇: BOB 䜽䝷䝇: 㒊ୗ 䜲䞁䝇䝍䞁䝇: TOM
  • 202. 䜽䝷䝇䛸䜲䞁䝇䝍䞁䝇䛾㛵ಀ • 䜽䝷䝇: 䜲䞁䝇䝍䞁䝇䜢స䜛䛯䜑䛾㞮ᙧ • 䜲䞁䝇䝍䞁䝇: 䜽䝷䝇䛛䜙స䜙䜜䜛䜸䝤䝆䜵䜽䝖 • ᆺ(䜽䝷䝇) 䛸䛭䛾್(䜲䞁䝇䝍䞁䝇)䛸䛔䛖⌮ゎ䛷OK Python ධ㛛 202 䜽䝷䝇 䜲䞁䝇䝍䞁䝇໬ 䜲䞁䝇䝍䞁䝇 ᆺ(Class) String ್(Instance) Hello Python, Hello Cisco
  • 203. 䝁䞁䝫䝆䝅䝵䞁 • 䜽䝷䝇䛿ู䛾䜽䝷䝇(䜲䞁䝇䝍䞁䝇)䜢ᣢ䛴䛣䛸䛜䛒䜛 Python ධ㛛 203 䜽䝷䝇㌴䛿 -­‐ 䜶䞁䝆䞁䜽䝷䝇䜢1䜲䞁䝇䝍䞁䝇 -­‐ 䝍䜲䝲䜽䝷䝇䜢4䜲䞁䝇䝍䞁䝇 -­‐ 䝝䞁䝗䝹䜽䝷䝇䜢1䜲䞁䝇䝍䞁䝇 ᣢ䛳䛶䛔䜛
  • 205. 䜽䝷䝇䛾ᐉゝ • ௨ୗ䛾せ௳䜢‶䛯䛩䜽䝷䝇䜢స䜛 Python ධ㛛 205 䜽䝷䝇: MyClass ᒓᛶ: 䜲䞁䝇䝍䞁䝇ኚᩘ string: ಖᣢ䛩䜛ᩥᏐิ ᒓᛶ: 䝯䝋䝑䝗 set_string: string䜢䝉䝑䝖 get_string: string䜢ྲྀᚓ double: string䜢⧞䜚㏉䛩 䝕䞊䝍䛸䛧䛶ᩥᏐิ䜢ෆ㒊䛻ᣢ䛴 䛭䛾ᩥᏐิ䜢タᐃ䛩䜛䝯䝋䝑䝗 䛭䛾ᩥᏐิ䜢ྲྀᚓ䛩䜛䝯䝋䝑䝗 ᩥᏐิ䜢䛂ᩥᏐิ+ᩥᏐิ䛃䛻䛩䜛䝯䝋䝑䝗
  • 206. 䜽䝷䝇䛾ᐉゝ • 䜽䝷䝇䛾ᐉゝ䛿 class 䜽䝷䝇ྡ: • 䝯䝋䝑䝗䛾ᐉゝ: ➨୍ᘬᩘ䛜㻌self 䛸䛧䛶ᐃ⩏ • ᒓᛶ䜈䛾䜰䜽䝉䝇䛿㻌self.ᒓᛶ㻌䛸䛩䜛 Python ධ㛛 206 class MyClass: string = def set_string(self, string): self.string = string def get_string(self): return self.string def double(self): self.string *= 2 class 䛾ᐉゝ 䝯䝋䝑䝗䛾ᐉゝ ➨୍ᘬᩘ䜢 self 䛸䛩䜛䛾䛿 python 䛾䝹䞊䝹 class 䛜ಖᣢ䛩䜛䝕䞊䝍䛾ᐉゝ ᒓᛶ䜈䛾䜰䜽䝉䝇 self.ᒓᛶ㻌䛸䛩䜛 (䛭䛖䛧䛺䛔䛸㛵ᩘ䛰䛡䛾䝻䞊䜹䝹ኚᩘ䛸䛺䜛)
  • 207. 䜲䞁䝇䝍䞁䝇䛾฼⏝ • 䜽䝷䝇ྡ() 䛷䜲䞁䝇䝍䞁䝇໬ • 䝯䝋䝑䝗䜢࿧䜃ฟ䛩㝿䛿➨୍ᘬᩘ(self)䛿ᣦᐃ䛧䛺䛔 Python ධ㛛 207 class MyClass: string = def set_string(self, string): self.string = string def get_string(self): return self.string def double(self): self.string *= 2 # 䜲䞁䝇䝍䞁䝇໬ mc = MyClass() # 䝯䝋䝑䝗࿧䜃ฟ䛧 mc.set_string(hello ) print(mc.get_string()) hello mc.double() print(mc.get_string()) hello hello
  • 208. ₇⩦ • class Counter 䜢సᡂ • ෆ㒊䛻䜹䜴䞁䝍䜢ᣢ䛴䚹ึᮇ್䛿1 • get_value 䝯䝋䝑䝗䛷⌧ᅾ䛾䜹䜴䞁䝍್䜢㏉䛩 • count_up 䝯䝋䝑䝗䛷⌧ᅾ䛾䜹䜴䞁䝍್䜢 +1 䛩䜛 • clear_counter 䝯䝋䝑䝗䛷䜹䜴䞁䝍䜢1䛻ᡠ䛩 Python ධ㛛 208
  • 209. 䝁䞁䝇䝖䝷䜽䝍䛾฼⏝ • 䝁䞁䝇䝖䝷䜽䝍: 䜲䞁䝇䝍䞁䝇సᡂ᫬䛻࿧䜃ฟ䛥䜜䜛䝯䝋䝑䝗 • 䝁䞁䝇䝖䝷䜽䝍䛾ᘬᩘ䛿䜲䞁䝇䝍䞁䝇సᡂ᫬䛻ᣦᐃ Python ධ㛛 209 class MyClass: string = def __init__(self, string): print(__init__ is called) self.string = string def set_string(self, string): self.string = string def get_string(self): return self.string def double(self): self.string *= 2 # 䜲䞁䝇䝍䞁䝇໬ mc = MyClass(python ) __init__ is called # 䝯䝋䝑䝗࿧䜃ฟ䛧 print(mc.get_string()) python
  • 210. ₇⩦ • class Counter 䜢ᨵⰋ • ෆ㒊䛻䜹䜴䞁䝍䜢ᣢ䛴䚹ึᮇ್䛿䝁䞁䝇䝖䝷䜽䝍䛷ᣦᐃ • clear_counter 䝯䝋䝑䝗䛷䜹䜴䞁䝍䜢ึᮇ್䛻ᡠ䛩 Python ධ㛛 210
  • 211. ⿵㊊)ືⓗ䛺䝯䞁䝞ኚᩘ • ືⓗ䛻䝯䞁䝞せ⣲䜢㏣ຍ䛷䛝䜛 • Java䛸䛿␗䛺䜛䛾䛷せὀព!! Python ධ㛛 211 class MyClass: def func(self): print(func) mc = MyClass() mc.a = Hello World mc.a 'Hello World' ᐃ⩏䛥䜜䛶䛔䛺䛔 䝯䞁䝞ኚᩘ a 䛻௦ධ 䝯䞁䝞ኚᩘ a 䛿 ᐃ⩏䛥䜜䛶䛔䛺䛔
  • 212. 䜽䝷䝇䝃䞁䝥䝹 Python ධ㛛 212 # bingo.py import random class BingoMachine: ballList = [] def __init__(self, min, max): self.ballList = range(min, max + 1) def getBall(self): if(len(self.ballList) == 0): return -­‐1 index = int(random.uniform(0, len(self.ballList))) return self.ballList.pop(index) # ᐇ⾜⤖ᯝ from bingo import * b = BingoMachine(1,5) b.getBall() 1 b.getBall() 5 b.getBall() 2 b.getBall() 3 b.getBall() 4 b.getBall() -­‐1 䝡䞁䝂䝬䝅䞊䞁 ᩘᏐ㻌n – m 䛾㛫䛾ᩘᏐ䜢䝷䞁䝎䝮䛻㏉䛩 ୍ᗘ㏉䛧䛯್䛿㏉䛥䛺䛔
  • 213. 䜽䝷䝇䜎䛸䜑1 • 䝯䝋䝑䝗䛾➨୍ᘬᩘ䛿⮬ศ⮬㌟䛾䜸䝤䝆䜵䜽䝖 • 䝯䞁䝞ኚᩘ䜔䝯䝋䝑䝗䜢࿧䜆䛻䛿䛣䛾䜸䝤䝆䜵䜽䝖䛾ᣦᐃ䛜ᚲせ 䝯䞁䝞ኚᩘ䜔䝯䝋䝑䝗䜢 ࿧䜃ฟ䛧䛶䛔䜛 Python ධ㛛 213 class MyClass: var = 0 def method1(self): self.var = 1 print(method1) def method2(self): print(self.var) self.method1() print(method2) 䝯䝋䝑䝗ᐃ⩏䛾➨୍ᘬᩘ䛜 self 䛻䛺䛳䛶䛔䜛 self.ྡ๓㻌䛸䛧䛶 䝯䝋䝑䝗࿧䜃ฟ䛧䛿ᐃ⩏䛾➨୍ᘬᩘ䛿㣕䜀䛩
  • 214. 䜽䝷䝇䜎䛸䜑2 • 䜲䞁䝇䝍䞁䝇໬:䚷䜽䝷䝇ྡ(䝁䞁䝇䝖䝷䜽䝍䛻Ώ䛩ᘬᩘ) • 䝁䞁䝇䝖䝷䜽䝍: Python ධ㛛 214 __init__ class MyClass2: var = 0 def __init__(self, var): self.var = var def method(self, var): print(str(self.var) + + str(var)) print(method) m2 = MyClass2(5) m2.method(8) -­‐-­‐ 5 8 method 䛸䛔䛖䝯䝋䝑䝗ྡ
  • 216. ₇⩦䛾┠ⓗ Python ධ㛛 216 • ኱䛝䛺䝥䝻䜾䝷䝮䛾సᡂ᪉ἲ䜢Ꮫ䜆 – 」ᩘ䛾䜽䝷䝇䛾㐃ᦠ – 䛹䛾䜘䛖䛻䛧䛶䝁䞊䝗䜢ᗈ䛢䛶䛔䛟䛛 – 㢟ᮦ䛿䝗䝷䜽䜶1
  • 217. 䝗䝷䝂䞁䜽䜶䝇䝖 ඲䛶䜢స䜛䛾䛿↓⌮䛺䛾䛷 Python Map ධ㛛 ๓ᚋᕥྑ䛻ື䛟 䛯䛰䛧䚸䝬䝑䝥䛾እ䛻䛿ฟ䛺䛔 ⏫ே䛻ヰ䛧䛛䛡䜛 217 ୺ேබ 䛾䜏ᐇ⿦䛩䜛 ⏫ே ୺ேබ䛻ᛂ⟅䛩䜛
  • 218. AA∧䝗䝷䝂䞁䜽䜶䝇䝖(᏶ᡂᅗ) ๓ᚋᕥྑ䛻ື䛟 䛯䛰䛧䚸䝬䝑䝥䛾እ䛻䛿ฟ䛺䛔 ⏫ே䛻ヰ䛧䛛䛡䜛 ྥ䛔䛶䛔䜛ྥ䛝䛷䜰䜲䝁䞁䛜ኚ䜟䜛 Python ⏫ே ධ㛛 218 a:Left, s:Right, w:Up, x:Down d:Talk, Ctrl-­‐C:Exit +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ | | | | | B | | | | M| | | | | | P | +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ ####################### Hello I'm Bukiya ####################### ୺ேබ ୺ேබ䛻ᛂ⟅䛩䜛 䝯䝑䝉䞊䝆ḍ 䝬䝑䝥
  • 219. సᡂ䝥䝻䜾䝷䝮䛾せ⣲ 䝬䝑䝥(඲య䛾⟶⌮) Python ධ㛛 219 ⏫ே(」ᩘ) ୺ேබ (䜲䝧䞁䝖ฎ⌮) ධຊ 䜻䞊ධຊ⟶⌮䜽䝷䝇 (ᐇ⿦῭䜏)
  • 220. 䜽䝷䝇⏫ே Python ධ㛛 220 䜽䝷䝇: ⏫ே 䝯䞁䝞ኚᩘ xᗙᶆ yᗙᶆ እぢ䛾✀㢮 䝉䝸䝣 䝯䝋䝑䝗: respond: ୺ேබ䛻ᛂ⟅䛩䜛 እぢ B: Ṋჾᒇ M: ၟே P: ⚄∗
  • 221. 䜽䝷䝇⏫ே䛾䜲䞁䝇䝍䞁䝇 Python ධ㛛 221 䜲䞁䝇䝍䞁䝇㻌⏫ேA 䝯䞁䝞ኚᩘ x: 8 y: 4 እぢ: ⏨A 䝉䝸䝣: 䛣䜣䛻䛱䛿 䝯䝋䝑䝗: respond: ୺ேබ䛻ᛂ⟅䛩䜛 䜲䞁䝇䝍䞁䝇㻌⏫ேB 䝯䞁䝞ኚᩘ x: 4 y: 2 እぢ: 㨱ἲ౑䛔 䝉䝸䝣: 䛔䛔ኳẼ䛨䜓䛾䛖 䝯䝋䝑䝗: respond: ୺ேබ䛻ᛂ⟅䛩䜛
  • 222. ୺ேබ Python ධ㛛 222 ୺ேබ䛾䜰䜲䝁䞁 : ᕥྥ䛝 : ྑྥ䛝 ^ : ୖྥ䛝 V : ୗྥ䛝 䜽䝷䝇: ୺ேබ 䝯䞁䝞ኚᩘ xᗙᶆ yᗙᶆ ྥ䛔䛶䛔䜛᪉ྥ 䝉䝸䝣 䝯䝋䝑䝗: run: 䜻䞊ධຊ䜢ᚅ䛴(ᶆ‽ධຊ䛾↓㝈䝹䞊䝥) move: 䜻䞊ධຊ䛷ୖୗᕥྑ䛻ື䛟 talk: ྥ䛔䛶䛔䜛᪉ྥ䛾⏫ே䛸ヰ䛩㻌
  • 223. 䝬䝑䝥 Python ධ㛛 223 䜽䝷䝇: Map 䝯䞁䝞ኚᩘ -­‐ ୺ேබ䛾䜲䞁䝇䝍䞁䝇 -­‐ ⏫ே䛯䛱䛾䜲䞁䝇䝍䞁䝇 -­‐ 䝬䝑䝥᝟ሗ 䝯䝋䝑䝗: -­‐ render: 䝬䝑䝥䜢᭩䛝ฟ䛩 -­‐ ୺ேබ䛸⏫ே䜢⟶⌮䛩䜛䝯䝋䝑䝗⩌ +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ | | | | | B | | | | P | +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ እᯟ ୺ேබ ⏫ே(」ᩘ)
  • 224. 䜽䝷䝇䛸䜽䝷䝇䛾┦஫㛵ಀ • ༑Ꮠ䜻䞊䜢᧯స䛩䜛䛸ຬ⪅䛜ື䛟 • ᆅᅗ䛿䛭䛾᝟ሗ䜢཯ᫎ䛧䛺䛡䜜䜀䛺䜙䛺䛔 Python ධ㛛 224 䜲䞁䝇䝍䞁䝇 ຬ⪅ 䜲䞁䝇䝍䞁䝇 Map ධຊ(keyInput) ⮬ศ䛾X,Y᝟ሗ䜢 䜰䝑䝥䝕䞊䝖 ᥥ⏬䜢౫㢗 ⏬㠃ฟຊ
  • 225. 䝬䝑䝥ᥥ⏬䛾඲య䛾ὶ䜜 ③ ④ Python ධ㛛 225 Class: Field ኚᩘ: hero ኚᩘ: peopleMap ኚᩘ: ᆅᙧ᝟ሗ renderMap() ᥥ⏬(render) ᆅᙧ᝟ሗ + ຬ⪅ + ⏫ே㐩 Class: Hero move() talk() getXY() getImage() keyInputEvent() Class:䚷People respond() getImage() ධຊ(keyInput) x,yᗙᶆ䜔ぢ䛯┠ 䛺䛹䛾᝟ሗ䜢ྲྀᚓ 䝕䜱䝇䝥䝺䜲䛻ฟຊ ① ② +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ | | | | | B | | | | P | +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+
  • 227. 䝬䝑䝥 • ఱ䜒䛺䛔䝬䝑䝥䜢ᥥ⏬ • ⦪䛸ᶓ䛾㛗䛥䜢ᣦᐃྍ⬟ • ⾜(row)ᣦྥ䛷ᥥ⏬䛩䜛 227 +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ | | | | | | +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ 4㝮䛿㻌䛂+䛃 yᗙᶆ0, ᭱኱್䛿㻌䛂–䛃 xᗙᶆ0, ᭱኱್䛿㻌䛂|䛃 䛭䜜௨እ䛿✵ⓑ class DQMap: max_x = 0 max_y = 0 def __init__(self, max_xy): (self.max_x, self.max_y) = max_xy def render_map(self): map_string_list = [] for y in range(self.max_y): for x in range(self.max_x): if(y == 0 or y == self.max_y -­‐ 1): if(x == 0 or x == self.max_x -­‐ 1): map_string_list.append(+) else: map_string_list.append(-­‐) else: if(x == 0 or x == self.max_x -­‐ 1): map_string_list.append(|) else: map_string_list.append( ) map_string_list.append(n) map_string = .join(map_string_list) print(map_string.rstrip())
  • 228. ୺ேබ • 䜻䞊ධຊ䛷୺ேබ䛾x,yᗙᶆ䛸䛂ྥ䛝䛃䜢䜰䝑䝥䝕䞊䝖䛩䜛 • Ctrl-­‐C 䛷䝥䝻䜾䝷䝮䜢⤊஢䛩䜛 228 import getch, sys class Hero: x = 0 y = 0 direccon = N def __init__(self, x, y): self.x = x self.y = y self.direccon = N def run(self): gc = getch.Getch() while(True): keycode = ord(gc()) self.move(keycode) print(x:{}, y:{}, dir:{}.format(self.x, self.y, self.direccon)) if(keycode == 3): sys.exit() def move(self, keycode): if(keycode == 97): #Le self.x -­‐= 1 self.direccon = W elif(keycode == 115): #Right self.x += 1 self.direccon = E elif(keycode == 119): #Up self.y -­‐= 1 self.direccon = N elif(keycode == 122): #Down self.y += 1 self.direccon = S hero = Hero(10,10) hero.run()
  • 229. 䝬䝑䝥䛸୺ேබ(ㄢ㢟) • 䝬䝑䝥䛾୰䛻୺ேබ䜢ᥥ⏬ • ྥ䛔䛶䛔䜛᪉ྥ䛷୺ேබ䛾䜰䜲䝁䞁䜢ኚ䛘䜛 • 䝬䝑䝥䛾⠊ᅖእ䛻୺ேබ䛜䜰䜽䝉䝇䛧䛺䛔 • 䝥䝻䜾䝷䝮䛾㉳Ⅼ䛿㻌main.py 229 ᐇ⿦䛾䝠䞁䝖 DQMap: -­‐ DQMap 䛜 Hero 䛾䜲䞁䝇䝍䞁䝇䜢ᣢ䛴 -­‐ ୺ேබ䛸ྠ䛨㻌x, y ᗙᶆ䛾ሙྜ䛿୺ேබ䛾䜰䜲䝁䞁䜢⾲♧ (୺ேබ䛾䜲䞁䝇䝍䞁䝇䛛䜙䜰䜲䝁䞁䜢ྲྀᚓ) Hero: -­‐ ᪂䛧䛔ᗙᶆ䛜 Map ⠊ᅖෆ䛷䛒䜛ሙྜ䛰䛡䚸x,y 䜢䜰䝑䝥䝕䞊䝖 (⠊ᅖእ䜈䛾䜰䜽䝉䝇䛿㻌x,y 䜢䜰䝑䝥䝕䞊䝖䛧䛺䛔) -­‐ ⾲♧䛩䜛୺ேබ䛾䜰䜲䝁䞁䛿୺ேබ䛾ྥ䛝䛷Ỵ䜎䜛 (ྥ䛝䛾ᛂ䛨䛯䜰䜲䝁䞁䜢㏉䛩䝯䝋䝑䝗䜢ᐇ⿦)
  • 231. ⏫ே䛾ᐇ⿦ Python ධ㛛 231 class People: icon = x = 0 y = 0 comment = def __init__(self, icon, x, y, comment): self.icon = icon self.x = x self.y = y self.comment = comment def get_icon(self): return self.icon def get_xy(self): return (self.x, self.y) def get_response(self): return self.comment ⏫ே䛿 -­‐ 䜰䜲䝁䞁(୍ᩥᏐ) -­‐ x,y -­‐ 䝉䝸䝣 䜢䝁䞁䝇䝖䝷䜽䝍䛷タᐃ 䛂䝀䝑䝍䞊䝯䝋䝑䝗䛃䜢ᐇ⿦ 䚷䈜 䝟䝷䝯䞊䝍䜢ྲྀᚓ䛩䜛䛯䜑䛾䝯䝋䝑䝗
  • 232. 䝬䝑䝥䛸⏫ே䛾ᐇ⿦ Python ධ㛛 232 class DQMap: ... people_map = {} def __init__(self, max_xy, hero_xy, people_paramList): .... for params in people_paramList: (icon, x, y, comment) = params self.people_map[(x,y)] = people.People(icon, x, y, comment) def render_map(self, comment): map_string_list = [] (hx, hy) = self.hero.get_xy() for y in range(self.max_y): for x in range(self.max_x): if((x,y) == (hx, hy)): map_string_list.append(self.hero.get_icon()) elif(self.people_map.has_key((x, y))): p = self.people_map[(x, y)] map_string_list.append(p.get_icon()) 䝁䞁䝇䝖䝷䜽䝍䛷⏫ே䛾 ᝟ሗ䜢䝸䝇䝖ᙧᘧ䛷ᣦᐃ ⏫ே䛾䜲䞁䝇䝍䞁䝇䜢సᡂ (x,y)䜢䜻䞊䛻䛧㎡᭩䛷⏫ே䜢⟶⌮ ᥥ⏬䛾㝿䛻䚸x, y 䛻⏫ே䛜䛔䜛䛛☜ㄆ 䜒䛧䛔䜜䜀⏫ே䜢ᥥ⏬䛩䜛
  • 233. ୺ேබ䛸⏫ே䛾ᐇ⿦(ㄢ㢟) • ୺ேබ䛜⏫ே䛸⿕䜙䛺䛔䜘䛖䛻䛩䜛 Python ධ㛛 233 Hint ḟ䛻ື䛣䛖䛸䛩䜛ඛ䛻⏫ே䛜䛔䜛䛛☜ㄆ 䛔䜛ሙྜ䛿୺ேබ䛾 x, y 䜢䜰䝑䝥䝕䞊䝖䛧䛺䛔
  • 235. ఍ヰᶵ⬟䛾ᐇ⿦ • ୺ேබ䛜ྥ䛔䛶䛔䜛᪉ྥ䛻⏫ே䛜䛔䜛䛛☜ㄆ • ⏫ே䛜䛔䜜䜀⏫ே䛾䝺䝇䝫䞁䝇䜢ྲྀᚓ䛧⾲♧ • 䛔䛺䛡䜜䜀ㄡ䜒䛔䛺䛔᪨䛾䝯䝑䝉䞊䝆䜢⾲♧ • DQMap䛾䝯䝋䝑䝗render_map䛻䝁䝯䞁䝖䜢Ώ䛧䛶ᥥ⏬(ᐇ⿦῭) Python ධ㛛 235 +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ | | | | | B | | | | M| | | | | | P | +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ ####################### Hello I'm Merchant ####################### +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ | | | | | B | | ^ | | M| | | | | | P | +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ ####################### No one exists on that way #######################
  • 237. __name__ • __name__䚷 • __name__ 䛜㻌__main__ 䛾䛸䛝䛿㉳Ⅼ Python import uclity def func(): print(func on main.py) print(global on main.py) func() uclity.func() if __name__ == __main__: print(__name__ is __main__ on main.py) ධ㛛 237 def func(): print(func() on uclity.py) print(global on uclity.py) func() if __name__ == __main__: print(__name__ is __main__ on uclity.py) main.py uclity.py
  • 238. __name__ Python ධ㛛 238 def func(): print(func() on uclity.py) print(global on uclity.py) func() if __name__ == __main__: True print(__name__ is __main__ on uclity.py) # python uclity.py global on uclity.py func() on uclity.py __name__ is __main__ on uclity.py ᐇ⾜⤖ᯝ
  • 239. __name__ Python ධ㛛 239 import uclity def func(): print(func on main.py) print(global on main.py) func() uclity.func() if __name__ == __main__: print(__name__ is __main__ on main.py) def func(): print(func() on uclity.py) print(global on uclity.py) func() if __name__ == __main__: print(__name__ is __main__ on uclity.py) main.py True uclity.py False #python main.py global on uclity.py func() on uclity.py global on main.py func on main.py func() on uclity.py __name__ is __main__ on main.py ᐇ⾜⤖ᯝ
  • 240. 䝁䝢䞊䛸ཧ↷ • 䝁䝢䞊: ␗䛺䜛䜸䝤䝆䜵䜽䝖 • ཧ↷: ྠ୍䜸䝤䝆䜵䜽䝖䜢」ᩘ⟠ᡤ䛷฼⏝ • ཧ↷䛻ᑐ䛧䛶䛿๪స⏝䛾ὀព䛜ᚲせ Python ධ㛛 240 a = [1,2,3,4] b = a b.append(5) a [1, 2, 3, 4, 5]
  • 242. Pexpect • EXPECT 䜢 Python 䛷౑䛖䛯䜑䛾䝟䝑䜿䞊䝆 • Cisco䛾ᶵჾ䜔Linux䛸䛔䛳䛯CLI᧯స䜢ᚲせ䛸䛩䜛ᶵჾ䜢 䝥䝻䜾䝷䝭䞁䜾䛷つᐃ㏻䜚䛻ື䛛䛩䛯䜑䛻฼⏝ Python ධ㛛 242 Python Script
  • 243. Expect • Linux䛺䛹䛾䜲䞁䝍䝷䜽䝔䜱䝤䛺CLI䜢ไᚚ䛩䜛 䛯䜑䛾䝥䝻䜾䝷䝮 • pexpect 䛾ඖ䛻䛺䛳䛯䝥䝻䜾䝷䝮 • Teraterm Macro 䛻ఝ䛶䛔䜛 Python ධ㛛 243 The Expect Home Page h7p://expect.sourceforge.net/ Expect Script
  • 244. Expect 䛾ືస • spawn: 䝉䝑䝅䝵䞁䜢㛤ጞ • expect: ᮇᚅ䛩䜛ᶆ‽ධຊ(from 䝸䝰䞊䝖)䜢ᚅ䛴 • send: 䝸䝰䞊䝖䛻ฟຊ䜢㏦䜛 Python ධ㛛 244 dhcp-­‐10-­‐141-­‐56-­‐250:~ yuichi$ telnet -­‐l yuiito rws1 Trying 10.71.231.245... Connected to rws1.cisco.com. Escape character is '^]'. Password: cisco123 Last login: Thu Jan 23 14:53:37 from dhcp-­‐10-­‐141-­‐56-­‐250.cisco.com unknown terminal xterm-­‐256color unknown terminal xterm-­‐256color [yuiito@rws1 yuiito]$ echo hello test [yuiito@rws1 yuiito]$ ls snmpget.sh test 㟷䛜䝸䝰䞊䝖䛛䜙䛾ᶆ‽ධຊ ㉥䛜䝸䝰䞊䝖䜈䛾ᶆ‽ฟຊ
  • 245. expect 䝃䞁䝥䝹 • Linux 䜈䛾䝻䜾䜲䞁䛸᧯స Python ධ㛛 245 Password: cisco123 yuiito$ echo hello test login-­‐to-­‐rws1.exp #! /usr/bin/expect spawn telnet -­‐l yuiito rws1 expect Password: send cisco123n expect yuiito]$ send touch hellon interact ࿧䜃ฟ䛧 yuichi$ expect login-­‐to-­‐rws1.exp
  • 246. Pexpect • pip 䜢౑䛳䛶䜲䞁䝇䝖䞊䝹 • 䝁䞊䝗㻌pexpect.py ྠ୍䝣䜷䝹䝎䛻㓄⨨(฼⏝) • import pexpect Python ධ㛛 246
  • 247. Pexpect 䛾సἲ • spawn session = pexpect.spawn(telnet➼䛾䝁䝬䞁䝗) • expect session.expect(ධຊ䛥䜜䜛䛾䜢ᚅ䛴ᩥᏐิ) • sendline session.sendline(㏦ಙ䛩䜛ᩥᏐิ䚹⮬ື䛷ᨵ⾜ 䝁䞊䝗䛜䛴䛟) Python ධ㛛 247
  • 248. pexpect • 䝬䝑䝏䛧䛯ᩥᏐิᙜ䜢ᢳฟ Python ධ㛛 248 session.aer: 䝬䝑䝏䛧䛯ᩥᏐิ session.before: 䝬䝑䝏䛧䛯ᩥᏐิ䜎䛷䛾ᩥᏐิ session.buffer:
  • 249. pexpect 䛾䝃䞁䝥䝹 • rws1 䛻䝻䜾䜲䞁䛧䛶䝣䜯䜲䝹㻌pexpect 䜢సᡂ Python ධ㛛 249 import pexpect child = pexpect.spawn(telnet -­‐l yuiito rws1) child.expect(Password:) child.sendline(cisco123) child.expect(yuiito]$) child.sendline(touch pexpect)
  • 250. pexpext䛾䝃䞁䝥䝹 • Nexus䛾タᐃ䜢䝣䜯䜲䝹ྡ䜢᪥᫬䛻䛧䛶FTP䝃䞊䝞䛻ಖᏑ Python ධ㛛 250 import pexpect, datecme def getLines(string): lines = lines = [x.strip() for x in string.split(n)] return filter(lambda x: x!= '', lines) def getHostName(exp): exp.sendline(show hostname) exp.expect(#) return getLines(exp.before)[1] def getToDay(): d = datecme.datecme.today() return {}_{}_{}-­‐{}_{}_{}.format(d.year, d.month, d.day, d.hour, d.minute, d.second) def makeFileName(exp): hostName = getHostName(exp) today = getToDay() return {}-­‐{}.conf.format(hostName, today) def login(exp, password): exp.expect(Password:) exp.sendline(password) exp.expect(#) exp.sendline(terminal length 0) exp.expect(#) def saveFile(exp, saveFrom, saveTo, vrf, username, password): command = copy {} {}.format(saveFrom, saveTo) exp.sendline(command) exp.expect(Enter vrf) exp.sendline(vrf) exp.expect(Enter username:) exp.sendline(username) exp.expect(Password:) exp.sendline(password) exp.expect(#) child = pexpect.spawn(telnet -­‐l admin 10.71.156.229) login(child, cisco) saveTo = p://10.71.224.115/yuiito/ + makeFileName(child) saveFile(child, running-­‐config, saveTo, management, anonymous, ) 䝣䜯䜲䝹ྡ䜔䝁䝬䞁䝗䛾⏕ᡂ䛜 python ௵䛫 child.close()
  • 251. pexpect • show䝁䝬䞁䝗䜔タᐃኚ᭦䛿䜋䜌ྠ䛨䜘䛖䛺ᡭ 㡰䛷ᐇ⿦䛥䜜䜛 • ⥅ᢎ䜔䜽䝻䞊䝆䝱䜢౑䛖䛣䛸䛷ྛฎ⌮䜢ᐇ⿦ 䛩䜜䜀⡆༢䛻ᶵ⬟䜢ቑ䜔䛫䜛 Python ධ㛛 251
  • 252. ₇⩦ • Nexus䛾⌧ᅾ䛾䝞䞊䝆䝵䞁䜢ྲྀᚓ䛩䜛䝥䝻䜾 䝷䝮䜢సᡂ䛩䜛 Python ධ㛛 Nexus ip: 1.110.8.1 user-­‐name: admin pass: cisco 252 ㋃䜏ྎ ip: 10.71.224.172 user-­‐name: guest pass: c1sco123
  • 254. Python ධ㛛 Learning to Program For Cisco Network Engineers Day 4/4 Japan TAC DC Team. Yuichi Ito
  • 255. Agenda Day4 • Python䛾䝥䝻䜾䝷䝮㓄ᕸᡭἲ • Tkinter (GUI) • Tkinter 䛷Ꮫ䜆⥅ᢎ • ౛እฎ⌮ • NETWORK䝥䝻䜾䝷䝭䞁䜾 • Pexpect Python ධ㛛 255
  • 257. 䝋䞊䝇䝁䞊䝗䛾㓄ᕸ • 㛗ᡤ: 㓄ᕸ䛜⡆༢ • ▷ᡤ: 䝷䜲䝤䝷䝸䛻౫Ꮡ䚹䝁䞊䝗䛜୸ぢ䛘 Python ධ㛛 257 python 䝋䞊䝇 䝁䞊䝗 python 䝋䞊䝇 䝁䞊䝗 python 䝋䞊䝇 䝁䞊䝗 㛤Ⓨ⪅ ฼⏝⪅ ฼⏝⪅
  • 258. 䝞䜲䝘䝸໬䛧䛶ᐇ⾜⎔ቃ䛤䛸㓄ᕸ • 㛗ᡤ: ฼⏝⪅䛜౑䛔䜔䛩䛔䚹 • ▷ᡤ: సᡂ䛜㞴䛧䛔 .exe .app Python ධ㛛 258 python 䝋䞊䝇 䝁䞊䝗 㛤Ⓨ⪅ ฼⏝⪅(Windows) ฼⏝⪅(Mac)
  • 260. 㛵ᩘ䛸䝟䝷䝯䞊䝍 • 㛵ᩘ(䝯䝋䝑䝗)䛻䛿䝕䝣䜷䝹䝖್䜢౑䛘䜛 • ࿧䜃ฟ䛧ഃ䛷ᘬᩘ䜢ዲ䛝䛺㡰ᗎ䛷ᣦᐃྍ⬟ Python ධ㛛 260 def func(a, b=python, c=cisco): print(hello {} {} {}.format(a,b,c)) func(world, PYTHON, CISCO) 䚷# = hello world PYTHON CISCO func(world) # = hello world python cisco func(b=world, a=python) # = hello python world cisco ᘬᩘ b, c 䛻䛿䝕䝣䜷䝹䝖್䜢ᣦᐃ ᘬᩘ㻌a,b,c 䛻඲䛶௵ព䛾್䜢タᐃ ᘬᩘ a 䛾䜏ᣦᐃ䛧䛶࿧䜃ฟ䛧 ௵ព䛾㡰ᗎ䛷ᘬᩘ䜢ᣦᐃ
  • 261. GUI TKINTER Python ධ㛛 261
  • 262. GUI 䛾Ꮫ⩦┠ⓗ • 䜰䝥䝸䜿䞊䝅䝵䞁䛻GUI୍⯡ⓗ • ぢ䛯┠䛻ศ䛛䜚䜔䛩䛔䛾䛷୰⣭⪅ྥ䛝 • ⥅ᢎ䛾ᴫᛕ䛺䛹䜢⌮ゎ䛩䜛䛾䛻᭱㐺䛺㢟ᮦ Python ධ㛛 262
  • 263. Tkinter • Python 䛾ᶆ‽ⓗ䛺GUI䝷䜲䝤䝷䝸 • 䜲䞁䝇䝖䞊䝹䛺䛧䛻฼⏝ྍ⬟ • ௚䛾GUI䝷䜲䝤䝷䝸(Qt, wxpython)䛸ᴫᛕ䛜ఝ䛶䛔䜛 Python ධ㛛 263
  • 264. Hello Tkinter Python ධ㛛 264 import Tkinter as tk font=(Helevecca, 32, bold) label = tk.Label(text=Hello Cisco, font=font, bg=red) label.pack() label.mainloop()
  • 265. Hello Tkinter • GUI సᡂ䛾ὶ䜜 – Widget䛸࿧䜀䜜䜛䝟䞊䝒䜢ึᮇ໬ – 䝟䞊䝒䜢㓄⨨ – GUI䜰䝥䝸䜿䞊䝅䝵䞁䜢㉳ື Python ධ㛛 265 Widget 䛿GUI䛾㒊ရ䚹 䝪䝍䞁䜔䝔䜻䝇䝖䝪䝑䜽䝇䛺䛹䛾඲䛶䛾䝟䞊䝒䛿Widget
  • 266. Hello Tkinter 䛾ゎㄝ Python ධ㛛 266 import Tkinter as tk font=(Helevecca, 32, bold) label = tk.Label(text=Hello Cisco, font=font, bg=red) label.pack() label.mainloop() Tkinter 䛸䛔䛖䝰䝆䝳䞊䝹䜢䜲䞁䝫䞊䝖䚹࿧䜃ฟ䛧᫬䛿㻌tk 䛸䛔䛖ู⛠䜢౑䛖 Widget 䛾㻌Label 䜢సᡂ䛩䜛䚹ᘬᩘ䛸䛧䛶ᩥᏐิ䛸䝣䜷䞁䝖䚸⫼ᬒⰍ䜢ᣦᐃ pack() 䝯䝋䝑䝗䛷㓄⨨ mainloop() 䝯䝋䝑䝗䜢࿧䜃ฟ䛩䛣䛸䛷䜰䝥䝸䜿䞊䝅䝵䞁䜢ᐇ⾜
  • 267. ₇⩦1 • 䝷䝧䝹䛻⾲♧䛩䜛ᩥᏐิ䜢 Hello Python 䛻ኚ᭦ • 䝣䜷䞁䝖䝃䜲䝈䜢 44 䛻䛩䜛 • ⫼ᬒⰍ䜢 blue 䛻䛩䜛 Ⓨᒎ • 䜰䞁䝎䞊䝷䜲䞁䜢௜䛡䜛 • ᖜ䜢50䛻䛩䜛 Python ධ㛛 267 An IntroducYon to Tkinter (Work in Progress) h7p://eot.org/tkinterbook/
  • 268. Frame • Widget 䜢㓄⨨䛩䜛䛯䜑䛾 Widget • Frame in Frame 䜒ྍ⬟(」㞧䛺ᵓᡂ䜢స䜜䜛) Python ධ㛛 268 Frame1 Frame2 Frame3 widget 1 widget 2 widget 3 widget 4 widget 5 widget 6 Frame1 | |-­‐-­‐ Frame 2 | | -­‐-­‐ Widget1 | | -­‐-­‐ Widget2 | | -­‐-­‐ Widget3 | | -­‐-­‐ Widget4 | | -­‐-­‐ Frame3 | -­‐-­‐ Widget5 | -­‐-­‐ Widget5
  • 269. Hello Frame Python ධ㛛 269 import Tkinter as tk frame = tk.Frame() font=(Helevecca, 32, bold) label1 = tk.Label(frame, text=Hello Cisco, font=font, bg=red) label2 = tk.Label(frame, text=Hello Python, font=font, bg=blue) label1.pack() label2.pack() frame.pack() frame.mainloop()
  • 270. Hello Frame 䛾ゎㄝ • Frame 䛾䜲䞁䝇䝍䞁䝇䜢సᡂ • Frame 䛾Ꮚせ⣲(୰䛻䛒䜛)䛿➨୍ᘬᩘ䜢 Frame 䛾 䜲䞁䝇䝍䞁䝇䛻䛩䜛 • 䝖䝑䝥䝺䝧䝹䛾 widget 䛷㻌mainloop() 䜢࿧䜃ฟ䛩 Python ධ㛛 270 import Tkinter as tk frame = tk.Frame() font=(Helevecca, 32, bold) label1 = tk.Label(frame, text=Hello Cisco, font=font, bg=red) label2 = tk.Label(frame, text=Hello Python, font=font, bg=blue) label1.pack() label2.pack() frame.pack() frame.mainloop()
  • 271. ₇⩦ • 3䛴䛾䝷䝧䝹䛜⾲♧䛥䜜䜛䜘䛖䛻ᨵ㐀䛩䜛 • 3␒┠䛾⫼ᬒⰍ䛿 green Python ධ㛛 271 • Hello Cisco, Hello World 䛾ᶓ䛾✵ⓑ䜢ᇙ䜑䜛 • Hint: widget䛾 pack 䛻 fill=tk.X 䜢ᣦᐃ Ⓨᒎ⦅
  • 272. Frame 䛾⦪ᶓ • Frame䛜୪䜆䛾䛿䝕䝣䜷䝹䝖⦪᪉ྥ • Ꮚせ⣲䛾 pack() 䝯䝋䝑䝗䛾ᘬᩘ䛻᪉ྥ䜢ᣦᐃ • ᶓ᪉ྥ䛿 side=LEFT 䜢ᣦᐃ 272 import Tkinter as tk frame = tk.Frame() font=(Helevecca, 32, bold) label1 = tk.Label(frame, text=Hello Cisco, font=font, bg=red) label2 = tk.Label(frame, text=Hello Python, font=font, bg=blue) label1.pack(side=tk.LEFT) label2.pack(side=tk.LEFT) frame.pack() frame.mainloop()
  • 273. ₇⩦ • Frame 䛻 Frame 䜢ධ䜜䛶௨ୗ䛾䜴䜱䞁䝗䜴䜢 సᡂ䛩䜛 Python ධ㛛 273
  • 274. ₇⩦䛾ゎ⟅౛ Python ධ㛛 274 import Tkinter as tk frame1 = tk.Frame() frame2 = tk.Frame() font=(Helevecca, 32, bold) label1 = tk.Label(frame2, text=Hello Cisco, font=font, bg=red) label2 = tk.Label(frame2, text=Hello Python, font=font, bg=blue) label1.pack(side=tk.LEFT) label2.pack(side=tk.LEFT) label3 = tk.Label(frame1, text=Hello Wodld, font=font, bg=green) label3.pack() frame2.pack() label3.pack() frame1.pack() frame1.mainloop()
  • 276. ⥅ᢎ Python ධ㛛 276 • 䜽䝷䝇䛜ู䛾䜽䝷䝇䛾≉ᛶ䜢ᘬ䛝⥅䛠䛯䜑䛾ᶵ⬟ • ඖ䛾䜽䝷䝇䜢䝇䞊䝟䞊䜽䝷䝇(ぶ䜽䝷䝇)䛸࿧䜆 • ὴ⏕䛥䜜䛯䜽䝷䝇䜢䝃䝤䜽䝷䝇(Ꮚ䜽䝷䝇)䛸࿧䜆 ぶ䜽䝷䝇(㌴) Ꮚ䜽䝷䝇(䝆䞊䝥) Ꮚ䜽䝷䝇(䝇䞊䝟䞊䜹䞊)
  • 277. TKinter 䛾⥅ᢎ • Label 䜔 Bu7on 䛿⥅ᢎ䛷సᡂ䛥䜜䛶䛔䜛 • ⥅ᢎඖ䛷᰿ᮏ䛾ᥥ⏬ฎ⌮䛺䛹䜢ᐇ⿦ • Label 䜔 Bu7on 䛿ᕪศ䛾䜏ᐇ⿦ Python ධ㛛 277 Widget Bu7on Hello World ᥥ⏬䛾௙⤌䜏䛾ᐇ⿦ 䝪䝍䞁≉᭷䛾ᶵ⬟䛾ᐇ⿦ 䝷䝧䝹≉᭷䛾ᶵ⬟䛾ᐇ⿦
  • 278. ⥅ᢎ䛾䝯䝸䝑䝖 • Ꮚ䜽䝷䝇䛾ᐇ⿦䜢༢⣧໬䛷䛝䜛 • 䜽䝷䝇⮬య䜒䛭䜜䜢౑䛖ഃ䜒䝁䞊䝗䛾ಖᏲᛶ䛜ቑ䛩 䛾䝇䞊䝟䞊䜽䝷䝇䛜 䛾Ꮚせ⣲䛸䛺䜛䛣䛸䛜䛷䛝䜛 Python ධ㛛 278 Label 䛸 Bu7on Frame Label 䜒 Bu7on 䜒 Frame 䛾 Ꮚせ⣲䛸䛺䜛䛣䛸䛜䛷䛝䜛
  • 279. Tkinter 䛾⮬స䜽䝷䝇 • ⥅ᢎ䛧䛶䜽䝷䝇䜢స䜛䛸䚸Frame䛻䛔䜜䜛䛣䛸 䛜䛷䛝䜛䝟䞊䝒䛜స䜜䜛 Python ධ㛛 279 Frame Frame ⥅ᢎ䛧䛶䛔䛺䛔ሙྜ Frame 䛻䛔䜜䜙䜜䛺䛔 ⥅ᢎ䛧䛶䛔䜛ሙྜ Frame 䛻䛔䜜䜙䜜䜛
  • 280. ⥅ᢎ䛥䜜䛯䜽䝷䝇䛾≉ᚩ • ぶ䜽䝷䝇䛾䝯䞁䝞ኚᩘ䛻䜰䜽䝉䝇ྍ⬟ • ぶ䜽䝷䝇䛾䝯䝋䝑䝗䛻䜰䜽䝉䝇ྍ⬟ • Ꮚ䜽䝷䝇䛾䝁䞁䝇䝖䝷䜽䝍ෆ䛷ぶ䜽䝷䝇䛾䝁䞁 䝇䝖䝷䜽䝍䜢࿧䜃ฟ䛩 Python ධ㛛 280 Ꮚ䜽䝷䝇 ぶ䜽䝷䝇 ⥅ᢎ䛾䜲䝯䞊䝆
  • 282. 䜽䝷䝇⥅ᢎ䛾సἲ 䜽䝷䝇䛾ᐉゝ class Ꮚ䜽䝷䝇(ぶ䜽䝷䝇): Ꮚ䜽䝷䝇䛾䝁䞁䝇䝖䝷䜽䝍䛷ぶ䜽䝷 䝇䛾䝁䞁䝇䝖䝷䜽䝍䜢࿧䜃ฟ䛩 ぶ䜽䝷䝇䛾䝁䞁䝇䝖䝷䜽䝍䛾ᘬᩘ 䛿඲䛶ᣦᐃ䚹 Ꮚ䜽䝷䝇䛿ぶ䜽䝷䝇䛾ኚᩘ䛸䝯 䝋䝑䝗䛻䜰䜽䝉䝇ྍ⬟ (ୖ᭩䛝䜒ྍ⬟) Python ධ㛛 282 class Parent: text = def __init__(self, text): print(Parent.__init__()) self.text = text def get_text(self): return self.text class Child(Parent): def __init__(self, text): print(Child.__init__()) Parent.__init__(self, text) def double(self): self.text *= 2
  • 283. ₇⩦ • 䜽䝷䝇 Counter 䜢⥅ᢎ䛧䛶 Counter2 䜢స䜛 • Counter2 䛻௨ୗ䛾䝯䝋䝑䝗䜢ᐇ⿦䛩䜛 -­‐ double: ⌧ᅾ䛾䜹䜴䞁䝍䞊䛾್䜢2ಸ䛩䜛 -­‐ clear_counter: 䜹䜴䞁䝍䞊䜢ึᮇ໬䛩䜛 Python ධ㛛 283 class Counter: value = -­‐1 def __init__(self, value): self.value = value def get_value(self): return self.value def count_up(self): self.value += 1
  • 284. Tkinter 䛾 Bu7on • 䝪䝍䞁䜢ᢲ䛥䜜䛯㝿䛻≉ᐃ䛾㛵ᩘ(䝯䝋䝑䝗)䜢࿧䜃ฟ䛩 • Bu7on(command=䛂࿧䜃ฟ䛩㛵ᩘ(䝯䝋䝑䝗)ྡ䛃) Python ධ㛛 284 import Tkinter as tk def clicked(): print(clicked) bu7on = tk.Bu7on(text=Click, command=clicked) bu7on.pack() bu7on.mainloop() clicked click 䛩䜛 ⾲♧䛥䜜䜛
  • 285. ⥅ᢎ䜢౑䜟䛺䛔䜹䜴䞁䝍䞊䛾ᐇ⿦ Python ධ㛛 285 import Tkinter as tk class Counter: label = None value = -­‐1 def clicked(self): # Click᫬䛻࿧䜀䜜䜛 self.value += 1 self.label.configure(text=self.getText()) def getText(self): # ⾲♧䛩䜛ᩥᏐ䜢సᡂ return Count:{}.format(self.value) def __init__(self, value): self.value = value frame = tk.Frame() font = (Helevecca, 32, bold) self.label = tk.Label(frame, text=self.getText(), font=font, bg=red) bu7on = tk.Bu7on(frame, text=Click, command=self.clicked) self.label.pack() bu7on.pack() frame.pack() frame.mainloop() c = Counter(0)
  • 286. ⥅ᢎ䜢౑䛖䜹䜴䞁䝍䛾ᐇ⿦ Python ධ㛛 286 import Tkinter as tk class Counter(tk.Frame): label = None value = -­‐1 def clicked(self): self.value += 1 self.label.configure(text=self.getText()) def getText(self): return Count:{}.format(self.value) def __init__(self, master=None, value=0): tk.Frame.__init__(self, master) self.value = value font = (Helevecca, 32, bold) self.label = tk.Label(self, text=self.getText(), font=font, bg=red) bu7on = tk.Bu7on(self, text=Click, command=self.clicked) self.label.pack() bu7on.pack() c = Counter(value=0) c.pack() c.mainloop()