파이썬 리턴
-
59 - (파이썬) 조기 리턴과 리스트 평탄화하는 재귀 함수study with Q - 파이썬 2024. 6. 23. 12:21
# 조기 리턴memo = {1: 1, 2: 1}def fbmm(n): if n in memo: return memo[n] elif n >= 3 : temporary = fbmm(n-1) + fbmm(n-2) memo[n] = temporary return temporaryprint(fbmm(5)) 파이썬은 조건문이나 반복문을 사용하게 되면 들여쓰기가 들어가게 되는 데, 요걸 줄이는 것이 힘이 많이 들어간다.→ 위의 코드에서 같은 실행값을 얻지만 조건문 하나가 줄어드는 형식으로 바꾸면 다음과 같다.memo = {1: 1, 2: 1}def f(n): if n in memo: return memo[n] temp = f(n-1) + f(n-2) memo[n] = temp r..
-
53 - (파이썬) 함수 리턴과 5-1절 확인 문제study with Q - 파이썬 2024. 6. 10. 22:11
def f(x) : #값을 들고 돌아가슈 return x+1print(f(10))print(f(20))>>>1121 def f(x) : #return 뒤에 아무 값도 넣지않으면 return # None을 들고 돌아가게 됨print(f(10))print(f(20))>>>NoneNone #함수 사용해보기: 요론 모양def 함수(매개변수): 변수 = 초기화 #처리 #처리 처리 #함수 처리 return 변수 start부터 end까지 더하는 함수 def sum_all(start, end): output = 0 for i in range(start, end+1): output += i return outputprint(sum_all(1, 10))print(sum_all(1, 100)..