zip、再帰還の使い方

複数のデータをZIP関数で、グル-プとして取り出すorder=[1,2,3]
mountains=[“富士”,”大山”,”八ヶ岳”]
height=[3776, 1525, 1800]
for odr, mt, ht in zip(order, mountains, height):
print(odr, mt, ht)
“””
1 富士 3776
2 大山 1525
3 八ヶ岳 1800
“””
———————————

再帰関数.py 6/3/2023

再帰関数の特徴:codeが短くなる、代入操作が不要、分かりにくくコストがかかる。
—>for文の方がが一般的


For文を使う例
x=0
for i in range(1, 11):
x += i
print(x) # 55

再帰関数を使う_1

def total_1(x):
if x == 0:
return x
else:
return x + total_1(x-1)

result_1 = total_1(10)
print(result_1) # 55

再帰関数を使う_2

def total_2(x, accumulator):
if x == 0:
return accumulator
else:
return total_2(x-1, accumulator+x)

result_2 = total_2(10, 0)
print(result_2) # 55