5. ⿵㊊
䝖䝺䞊䝙䞁䜾䛾ෆᐜ䛿
Alan
Gauld
䛥䜣䛾
Learning
to
proguram
ver
2
䜢ཧ↷䛧䛶సᡂ䚹
䛾䛂ึ䜑䛶䛾Python䛃䜒ཧ↷䛧䛶
Python
ධ㛛
5
䜎䛯䚸O’REILLY
䛔䜎䛩䚹
Learning
to
Program
h7p://www.alan-‐g.me.uk/
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
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
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
ᆺ
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)
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
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
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]