Created
July 28, 2022 05:45
-
-
Save ioanzicu/8fbd9f1c55f7ac9fabc9a21d829b6ba1 to your computer and use it in GitHub Desktop.
Get the sum of numbers in a list recursively in Python 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum_nums(nums): | |
if not nums: | |
return 0 | |
return nums[0] + sum_nums(nums[1:]) | |
nums = [1, 2, 3, 4, 5] | |
print(sum_nums(nums)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment