def square(x):
print("함수 시작")
return x * x
print("함수 끝") # dead code
print(square(3))
print("Hello world!")
def print_square(x):
print(x * x)
def get_square(x):
return x * x
get_square(3) #출력값은 없음
print(get_square(3)) # 9 출력
print(print_square(3)) #함수 결과값인 9와 리턴문이 없다는 의미의 None 출력됨
: 파라미터에게 ‘기본값(default value)’를 설정할 수 있음. 기본값을 설정해두면 함수 호출시 꼭 파라미터에 값을 넘겨주지 않아도 되서 이런 파라미터를 옵셔널 파라미터라고 함.
def myself(name, age, nationality="한국"):
print("내 이름은 {}".format(name))
print("나이는 {}살".format(age))
print("국적은 {}".format(nationality))
myself("ash", 20, "미국") # 옵셔널 파라미터를 제공하는 경우
print()
myself("ash", 20) # 옵셔널 파라미터를 제공하지 않는 경우, 기본값으로 출력
-파이썬은 PEP8에 나와있는 규칙을 참고하여 따르는 것이 좋아요 🙏🏻
https://pep8.org/
https://www.python.org/dev/peps/pep-0008
1.이름 규칙
-변수 : 모든 변수와 함수의 이름은 소문자로 작성, 여러 단어일 경우 _로 나누기
-상수 : 모든 상수이름은 대문자로 작성, 여러 단어일 경우 _로 나누기
-의미있는 이름 사용! : x, y, a, b 등 남발 금지. say_hello 등 유추 가능하도록 이름 지정
2.화이트 스페이스
-들여쓰기 : 들여쓰기는 무조건 스페이스 4개
-함수정의 : 함수의 정의 위아래로 빈 줄이 두 줄씩. 하지만 파일의 첫 줄이 함수 정의인 경우 해당 함수 위에는 빈 줄이 없어도 된다.
# bad
def a():
print('a')
def b():
print('b')
def c():
print('c')
# good
def a():
print('a')
def b():
print('b')
def c():
print('c')
-괄호 안 : 괄호 바로 안에는 띄어쓰기X
# bad
spam( ham[ 1 ], { eggs: 2 } )
# good
spam(ham[1], {eggs: 2})
-함수 괄호 : 함수를 정의하거나 호출할 때, 함수 이름과 괄호 사이에 띄어쓰기X
# bad
def spam (x):
print (x + 2)
# good
def spam(x):
print(x + 2)
-쉼표 : 쉼표 앞에는 띄어쓰기X
-지정연산자 : 지정 연산자 앞뒤로 띄어쓰기를 하나씩만
-연산자 : 기본적으로는 연산자 앞뒤로 띄어쓰기를 하나씩,
하지만 연산의 "우선 순위"를 강조하기 위해서는, 연산자 앞뒤로 띄어쓰기를 붙이기
# bad
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
# good
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
-코멘트 : 일반 코드와 같은 줄에 코멘트를 쓸 경우, 코멘트 앞에 띄어쓰기 최소 두 개
# bad
x = x + 1# 코멘트
# good
x = x + 1 # 코멘트
1.처음 답안
def calculate_change(payment, cost):
change = payment - cost
five_manwon = int(change / 50000)
manwon = int((change - (five_manwon*50000))/ 10000)
five_cheonwon = int((change - (five_manwon*50000) - (manwon*10000)) / 5000)
cheonwon = int((change - (five_manwon*50000) - (manwon*10000) - (five_cheonwon*5000))/ 1000)
print("50000원 지폐: {}장".format(five_manwon))
print("10000원 지폐: {}장".format(manwon))
print("5000원 지폐: {}장".format(five_cheonwon))
print("1000원 지폐: {}장".format(cheonwon))
# 테스트
calculate_change(100000, 33000)
print()
calculate_change(500000, 378000)
2.//와 %사용하여 개선하기
def calculate_change(payment, cost):
change = payment - cost # 거스름돈 총액
five_manwon = change // 50000 # 50,000원 지폐
manwon = (change % 50000) // 10000 # 10,000원 지폐
five_cheonwon = (change % 10000) // 5000 # 5,000원 지폐
cheonwon = (change % 5000) // 1000 # 1,000원 지폐
print("50000원 지폐: {}장".format(five_manwon))
print("10000원 지폐: {}장".format(manwon))
print("5000원 지폐: {}장".format(five_cheonwon))
print("1000원 지폐: {}장".format(cheonwon))
# 테스트
calculate_change(100000, 33000)
print()
calculate_change(500000, 378000)
→ 노가다 하지 말고 연산자 잘 사용하기..!!, 코멘트 달아서 보기 쉽게 만들기.
허걱 파이썬가지 하시다녀..!! ㅠㅠ !! 대단하십니다
늘 응원하고 있습니다. 화이튕 😇
오늘도 잘 보구 가용