■ 끝내주게 숨쉬기
■ 간지나게 자기
■ 작살나게 밥먹기
■ 간드러지게 과제하기 (24시간 같은 48시간)
# (p.287) 범위 내부의 정수를 모두 더하는 함수
def sum_all(start, end): # 함수 선언
output = 0 # 변수 선언
for i in range(start, end + 1): # 반복문
output += i
return output # 반환
# 함수 호출
print("0 to 100:", sum_all(0, 100))
print("0 to 100:", sum_all(0, 1000))
print("0 to 100:", sum_all(50, 100))
print("0 to 100:", sum_all(500, 1000))
(작성코드)
(구조)
■ 멋지게 과제 미루기 (대성공)
1. 함수 기본
def 함수명(매개변수):
변수 = 초깃값
code
return 변수
# (p 275) 기본적인 함수
def put_off(): # 과제 미루기 함수
print('>>> 끝내주게 숨쉬기')
print('>>> 간지나게 자기')
print('>>> 작살나게 밥먹기')
put_off()
>>> 끝내주게 숨쉬기
>>> 간지나게 자기
>>> 작살나게 밥먹기
2. 매개 변수
def 함수명(매개변수, 매개변수, ...)
code
# (p 276) 매개변수 기본
def print_n_times(value, n):
for i in range(n):
print(value)
print_n_times('과제 미루는 법', 1)
put_off()
과제 미루는 법
>>> 끝내주게 숨쉬기
>>> 간지나게 자기
>>> 작살나게 밥먹기
# 정해진 매개변수보다 적게 입력했을 때
def put_off(value, n): # 매개변수 2개
for i in range(n):
print(value)
put_off('과제 미루는 법') # 매개변수 n에 할당되는 자료가 없음 > Type Error
# 정해진 매개변수보다 많게 입력했을 때
def put_off(value, n):
for i in range(n):
print(value)
put_off('과제 미루는 법', '그딴 건 없다', 2) # 정해진 매개변수보다 할당하고자 하는 자료가 많음 > Type Error
3. 가변 매개변수
def 함수명(매개변수, 매개변수, *가변 매개변수):
code
# (p 278) 가변 매개변수 함수
def put_off(n, *values): # 가변 매개변수 지정
for i in range(n):
for value in values:
print(value)
print()
put_off(2, '과제 미루는 법', '끝내주게 숨쉬기, 간지나게 자기, 작살나게 밥먹기') # 별도의 할당 없이 매개변수 할당 값 설정
과제 미루는 법
끝내주게 숨쉬기, 간지나게 자기, 작살나게 밥먹기
과제 미루는 법
끝내주게 숨쉬기, 간지나게 자기, 작살나게 밥먹기
4. 기본 매개변수
def put_off(value, n=2):
for i in range(n):
print(value)
put_off('과제 미루는 법이 궁금해?') # 기본 매개변수(n=2)
과제 미루는 법이 궁금해?
과제 미루는 법이 궁금해?
5. 키워드 매개변수
# (p 283) 키워드 매개변수
def put_off(*values, n=2):
for i in range(n):
for value in values:
print(value)
print()
put_off('과제 미루는 법', '끝내주게 숨쉬기, 간지나게 자기, 작살나게 밥먹기', n=1) # 키워드 매개변수(n=1)
과제 미루는 법
끝내주게 숨쉬기, 간지나게 자기, 작살나게 밥먹기
# (p 283) 여러 가지 함수 호출 형태
def test(a, b=10, c=100):
print(a+b+c)
test(10, 20, 30) # 매개변수 미 지정, 변수 개수 충족
test(a=10, b=200, c=300) # 매개변수 지정, 변수 개수 충족
test(c=300, b=20, a=10) # 매개변수 순서 지정, 변수 개수 충족
test(10, c=30) # 매개변수 지정, 변수 개수 미 충족(헤당 경우는 b가 기본 매개변수로 지정되어 있어 실행에 문제가 되지 않음)
60
510
330
50
구조 | 변수 지정 | 순서 지정 | 개수 충족 |
---|---|---|---|
test(10, 20, 30) | X | O | O |
test(a=10, b=200, c=300) | O | O | O |
test(c=300, b=20, a=10) | O | X | O |
test(10, c=30) | - | - | X |
# (p 285) 자료 없이 리턴하기
def return_test():
print('A 위치')
return # return_test def는 해당 명령어에서 실행이 종료됨
print('B 위치')
return_test()
A 위치
# (p 288) 기본 매개변수와 키워드 매개변수를 활용해 범위의 정수를 더하는 함수
def sum_all(start=0, end=100, step=1):
output=0
for i in range(start, end+1, step):
output += i
return output # for 최종 결과 반환
print('A:', sum_all(0, 100, 10))
print('B:', sum_all(end=100))
print('C:', sum_all(end=100, step=2))
A: 550
B: 5050
C: 2550
# (p 291) 확인문제 2
# mul def 내 구조를 설계하여 전달된 값을 모두 리턴하는 가변 매개변수 함수를 만들 것
def mul(*values):
output = 1 # 곱셈/나눗셈을 하는 경우 초기 값은 0을 가지면 안 됨
for i in values:
output *= i
return output # for 최종 결과 반환
print(mul(5, 7, 9, 10))
3150
실행자료: https://colab.research.google.com/drive/1ep0FIpTztQCU_7R1ajCgrUYZ48n1Zi-w?usp=sharing