ν¨μ
β’
ν¨μ
β’
ν¨μ μ©μ΄
β’
ν¨μ μ μ
β’
ν¨μ νΈμΆ
β’
κ°λ³ 맀κ°λ³μ
β’
λν΄νΈ 맀κ°λ³μ
β’
λ€μ€ λ°ν
β’
λ³μμ λ²μ(Scope)
ν¨μ
νΉλ³ν κΈ°λ₯μ μνν μ½λμ λͺ¨μ
ν¨μ μ©μ΄
ν¨μ μ μ
ν¨μ μ μ ν€μλ : def
def ν¨μλͺ
( 맀κ°λ³μ1, 맀κ°λ³μ2, ... ):
μ€νν λ¬Έμ₯
μ€νν λ¬Έμ₯
return (κ°)
return
return
1. ν¨μλ₯Ό μ’
λ£
2. (κ°)μ ν¨μλ₯Ό νΈμΆν μλ¦¬λ‘ λ°ν
a = 10
b = 2
def plus(a, b):
return a + b
def minus(a, b):
return a - b
def mul(a, b):
return a * b
def div(a, b):
return a / b
print('a + b = {}'.format( plus(a, b) ))
print('a - b = {}'.format( minus(a, b) ))
print('a * b = {}'.format( mul(a, b) ))
print('a / b = {}'.format( div(a, b) ))
Python
볡μ¬
ν¨μ νΈμΆ
κ°λ³ 맀κ°λ³μ
def add( *args ):
print(' {} μ ν©κ³λ {} μ
λλ€.'.format( args, sum(args) ))
add(1, 2)
add(1, 2, 3)
add(1, 2, 3, 4)
Python
볡μ¬
λν΄νΈ 맀κ°λ³μ
def greet( name, msg='μλ
νμΈμ'):
print('{} λ, {}'.format(name, msg) )
greet('ALOHA')
greet('ALOHA', 'λ°κ°μ΅λλ€')
Python
볡μ¬
λ€μ€ λ°ν
def calculate( *args ):
result1 = sum(args)
result2 = sum(args) / len(args)
result3 = max(args)
result4 = min(args)
return result1, result2, result3, result4
# λ°νκ°μ ννλ‘ μμ±νμ¬ λ°ννλ€
a, b, c, d = calculate(1,2,3,4,5)
result = calculate(1,2,3,4,5)
print('a : {}'.format(a))
print('b : {}'.format(b))
print('c : {}'.format(c))
print('d : {}'.format(d))
print('result : {}'.format(result))
Python
볡μ¬
λ³μμ λ²μ(Scope)
def func():
a = 10 # μ§μλ³μ
print('func() λ΄λΆ - a : {}'.format(a))
func() # ν¨μνΈμΆ
# print('func() μΈλΆ - a : {}'.format(a))
# μλ¬ λ°μ
# NameError : 'a' is not defined (λ³μ 'a'κ° μ μΈλμ΄μμ§ μμ΅λλ€.)
b = 10 # μ μλ³μ
def test():
print('func() λ΄λΆ - b : {}'.format(b))
test() # ν¨μνΈμΆ
print('func() μΈλΆ - b : {}'.format(b))
Python
볡μ¬