Created
January 19, 2022 18:27
-
-
Save artturijalli/8b0153c5950f2ebf9a8cab23b87c952d to your computer and use it in GitHub Desktop.
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
# How would you count the numbers in this list in as few lines of code as possible? | |
numbers = [1, 2, 3, 4, 5] | |
# 1. Charlie sums the numbers up using a for loop: | |
total = 0 | |
for number in numbers: | |
total += number | |
# 2. Bob sums the numbers up using the reduce function: | |
from functools import reduce | |
total = reduce(lambda x, y: x + y, numbers) | |
# 3. Alice sums up the numbers with the sum function: | |
total = sum(numbers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment