if
문의 expression 이 True
일때 실행되야 하는 코드들은 항상 if문 보다 시작 간격이 더 안으로 들어와 있어야한다
if
문과 동일 간격 선상에 있는 구문은 if
문과 관계 없는 독립적인 구문으로 취급
condition1 = 23
if condition1 == 23:
print('이건 if가 참일때 출력되는거에요!!')
print('이건 들여쓰기 안된거니까 if문에 관계없이 무조건 출력!!')
# Output
이건 if가 참일때 출력되는거에요!!
이건 들여쓰기 안된거니까 if문에 관계없이 무조건 출력!!
큰 따옴표("")와 작은 따옴표('')가 붙어있지 않은 True, False는 boolean 값이다.
항상 명심하자.
def quiz(world):
if world == 'real':
return(True)
if world != 'real':
return(False)
print(quiz('real'))
# Output
True #string이 아닌 boolean값
#
: 한줄 코멘트
''' '''
: 여러줄 코멘트
그러나 여러줄 코멘트를 쓸 때 줄마다 #
을 사용하는게 일반적
# 나는 한줄 코멘트!
'''
나는 여러줄 코멘트!
'''
# 하지만 다수의 코멘트를 출력할때에도
# 이렇게 #을 이용해서 하는데
# 이 방법이 더 일반적
return
함수 내에서 정의된 값을 함수 바깥으로 반환
current_budget = 3500.75
def print_remaining_budget(budget):
print("Your remaining budget is: $" + str(budget))
print_remaining_budget(current_budget)
# (budget - expense) 값을 함수 deduct_expense의 반환값으로 지정
def deduct_expense(budget, expense):
return budget - expense
shirt_expense = 9
# new_budget_after_shirt = (current_budget - shirt_expense)
new_budget_after_shirt = deduct_expense(current_budget, shirt_expense)
print_remaining_budget(new_budget_after_shirt)
# Output: Your remaining budget is: $3500.75
# Output: Your remaining budget is: $3491.75
# Positional arguments
def player_of_the_match(name):
print('Player of the match is... ' + name + '!')
player_of_the_match('Tierny')
# Output: Player of the match is... Tierny!
# 지정된 parameter에 지정된 argument 출력
# Keyword arguments
def ticket_price(price, person, discount):
print(price * person - discount)
ticket_price(person = 5, discount = 30, price = 20)
# Output: 70
# parameter 순서에 상관없이 argument 지정
# Default arguments
def ticket_price(price, person, discount = 10):
print(price * person - discount)
ticket_price(20, 5)
ticket_price(20, 5, 20)
# Output: 90
# default argument 생략 가능
# Output: 80
# 때에 따라 default argument에 새로운 값을 덮어씌울 수 있다
return
의 필수성return
이 반드시 fucntion
안에 들어가야하는 것은 아니지만, 함수의 근본적인 목적은 입력을 통한 출력이기에 출력값을 외부에 반환시키기 위해서는 return
이 필요하다.
return
값이 지정되지 않은 함수는 외부에 none
으로 출력된다고 한다.
👍