def : function을 정의할 때 :를 이용하여 작성
python은 항상 tab을 잘 이용해야 한다
들여쓰기가 문법을 결정 짓기 때문이다
def say_hello(who):
print("hello", who)
say_hello("Jimin")
def plus(a,b):
print(a + b)
def minus(a,b):
print(a - b)
plus(2, 3)
minus(2, 3)
def say_hello(name = "annoymous"):
print("hello",name)
say_hello()
say_hello("Jimin")
def p_plus(a, b):
print(a + b)
def r_plus(a, b):
return a + b
p_result = p_plus(2,3)
r_result = r_plus(2,3)
print(p_result, r_result)
return은 function의 값을 바깥으로 보낼 수 있다
p_plus는 그저 print만 해주는 function이기 때문에 print 해주고 종료
return 하는 값이 없는 p_plus는 None을 출력
return은 단 한 번만 할 수 있고 return하는 순간 function은 종료됨
그 아랫줄 코드는 실행할 수 없다
def plus(a, b):
return a - b
result = plus(a = 1, b = 30)
print(result)
보통 argument는 들어가는 순서대로 위치가 정해지지만 python에서는 이름에 따라 argument가 들어간다
위의 결과는 그래서 -29
keyword argument라고 한다
def say_hello(name, age):
return f"Hello {name} you are {age} years old"
hello = say_hello("Jimin", "12")
print(hello)
print에서 variable을 사용하는 방법
"" 앞에 f (format)을 작성하고 {variable}을 이용하여 작성할 수 있다
or
"" + variable +"" 이런식으로 쓸 수 있다
'Study > Python' 카테고리의 다른 글
| [python] python의 self와 __init__의 이해 (0) | 2023.03.18 |
|---|