ν¨μμ μ μ
- μ¬λ¬ κ°μ λ¬Έμ₯λ€μ νλμ μ΄λ¦μΌλ‘ λ¬Άμ΄μ μ΄λ¦λ§ νΈμΆν΄μ μ¬μ©νκΈ° μν κ°λ
- μ΄λ¦λ§ νΈμΆνλ©΄ λλ―λ‘ μ½λμ μ€λ³΅μ μ κ±°
- μ½λμ μΌμ λΆλΆμ λ³λμ λ
Όλ¦¬μ μΈ κ°λ
μΌλ‘ λΆλ¦¬νκΈ° μν λͺ©μ μΌλ‘λ μ¬μ©
- ν¨μμ μ’
λ₯λ λ³΄ν΅ Maker Function(λ΄μ₯ ν¨μ) κ³Ό μ¬μ©μ μ μ ν¨μλ‘ κ΅¬λΆ
- νμ΄μ¬μμλ ν¨μλ νλμ μλ£νμΌλ‘ κ°μ£Ό
- ν¨μμ μ΄λ¦μ ν¨μμ μ£Όμκ° λκ³ ν¨μμ΄λ¦()μ ν¨μμ νΈμΆ(μ€ν)λ¨
- ν¨μλ νλμ λ°μ΄ν°μ΄λ―λ‘ λ³μμ λμ
ν μ μκ³ λ§€κ°λ³μλ‘ μ¬μ©ν μ μμ
λ΄μ₯ ν¨μ
- λ³λμ λͺ¨λ μΆκ°μμ΄ μ¬μ©ν μ μλ κΈ°λ³Έμ μΌλ‘ μ 곡λλ ν¨μλ€
- λ΄μ₯ ν¨μ : https://docs.python.org/3/library/functions.html
- dir(__builtins__)λ₯Ό μ΄μ©ν΄ νμΈ κ°λ₯
νμ΄μ¬μμλ ν¨μλ λ©μλλ₯Ό μ μν λ
- definitionλ₯Ό μ€μΈ ν€μλ defλ₯Ό μ¬μ©
- def λ€μμ ν¨μλͺ
κ³Ό μΈμλ€μ λμ΄νκ³ :λ₯Ό κΈ°μ νκ³ λͺΈμ²΄λ₯Ό μ μ ο§ νμ΄μ¬μ μ΄λ€ νμμ λ°μ΄ν°λ
μΈμλ‘ μ λ¬ν μ μκΈ° λλ¬Έμ μΈμμ μλ£νμ κΈ°μ μν¨
- returnμ κ²°κ³Όλ₯Ό λλ €μ£Όκ³ μ ν λ κ²°κ³Όλ₯Ό ν¨μλ₯Ό νΈμΆν κ³³μΌλ‘ λλλ €μ€ λ μ¬μ©
ν¨μ μμ
# μΌλ°μ μΈ ν¨μ
def sum(a,b):
result = a+b
return result
a = sum(91515,5685)
print(a)
# μ
λ ₯ κ°μ΄ μλ ν¨μ
def say():
return 'hello world'
a = say()
print(a)
# κ²°κ³Όκ°μ 리ν΄νμ§ μλ ν¨μ(λ©μΈμΌλ‘)
def sum(a,b):
print("%dμ %dμ ν©μ %dμ
λλ€."%(a,b,a+b))
sum(3,5)
#μ
λ ₯ κ°λ κ²°κ³Ό κ°λ μλ ν¨μ
def say():
print('hi')
say()
κ²°κ³Ό
κ°λ³ 맀κ°λ³μ(ν¨μμμ μ¬λ¬ κ°μ μ λ¬ ν λ) μμ
#맀κ°λ³μκ° κ°λ³μΌ λ(κ°λ³ 맀κ°λ³μ)
def sum_many(*args):
sum = 0
for i in args:
sum += i
return sum
total = sum_many(1,2,3,4,5)
print("1~5κΉμ§ ν© : %d"%total)
total = sum_many(1,3,5,7,9)
print("10κΉμ§ νμμ ν© : %d"%total)
κ²°κ³Ό
μ¬λ¬κ°μ μ
λ ₯ κ° (μμ)
def add_mul(choice,*a):
if choice == "+":
result = 0
for i in a:
result = result +i
elif choice == "*":
result = 1
for i in a:
result = result * i
return result
result = add_mul('+',1,2,3,4,5)
print(result)
result = add_mul('*',1,3,2)
print(result)
κ²°κ³Ό
ν¨μ κ²°κ³Ό κ°
νκ΅ ppt μλ£
ν¨μ μμ)
def say_myself(name, old, man=True):
print("λμ μ΄λ¦μ %sμ
λλ€."%name)
print("λμ΄λ %dμ
λλ€."%old)
if man:
print("λ¨μ μ
λλ€.")
else:
print("μ¬μ μ
λλ€.")
say_myself("νκΈΈλ",45)
print()
say_myself("νκΈΈλ",45,True)
say_myself("νκΈΈμ",22,False)
κ²°κ³Ό
ν¨μ μμ ) κ³μ°κΈ°
def add(choice,x,y):
ad = x+y
print("%d %s %d = %d"%(x,choice,y,ad))
def sub(choice,x,y):
su = x-y
print("%d %s %d = %d"%(x,choice,y,su))
def mul(choice,x,y):
mu = x*y
print("%d %s %d = %d"%(x,choice,y,mu))
def div(choice,x,y):
di = x//y
print("%d %s %d = %d"%(x,choice,y,di))
x = int(input("aκ° μ
λ ₯ : "))
y = int(input("bκ° μ
λ ₯ : "))
choice = input("μ°μ° κΈ°νΈ μ
λ ₯ : ")
if choice == "+":
add(choice, x, y)
elif choice == "*":
mul(choice, x, y)
elif choice == "-":
sub(choice, x, y)
elif choice == "//":
div(choice, x, y)
else:
print("\nμ°μ°κΈ°νΈλ₯Ό λ€μ μ
λ ₯ νμΈμ.")