forked from makersacademy/intro-to-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
035_mapping.py
45 lines (30 loc) · 1.24 KB
/
035_mapping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Video alternative: https://vimeo.com/954334322/c5a36d4407#t=0
from lib.helpers import check_that_these_are_equal
# Mapping is going through a list and converting ('mapping')
# each item to another item. This is useful when you want
# to perform the same operation across a list of items.
# For example:
# * Getting the price of each in a list of products
# * Making each in a list of words uppercase
# * Finding the first letter of each in a list of words
# Here's an example:
words = ['I', 'need', 'another', 'five', 'years']
first_letters = [] # This is our accumulator again
for word in words: # We go through each word
first_letter = word[0] # Get the first letter
# And append it to our accumulator list:
first_letters.append(first_letter)
print(words)
print(first_letters)
# @TASK: run this program to see what it does.
# @TASK: Complete this exercise.
print("")
print("Function: add_one_hundred_to_numbers")
# Return a new list of each number with 100 added
def add_one_hundred_to_numbers(numbers):
pass
check_that_these_are_equal(
add_one_hundred_to_numbers([1, 2, 3, 4]), [101, 102, 103, 104])
check_that_these_are_equal(
add_one_hundred_to_numbers([2, 3, 4, 5]), [102, 103, 104, 105])
# When you're done, move on to 036_filtering.py