0
Single list comprehension
I have been stuck on this question for days. I'm not sure what exactly is needed to solve this problem. Do I need to create a for loop and multiply it by 7? Do I need to delete the pass and add the print (multiples_of(7, 5)) # [7, 14, 21, 28, 35] with the class? def multiples_of(number, length): pass print (multiples_of(7, 5)) # [7, 14, 21, 28, 35]
5 odpowiedzi
+ 3
You make it seems so simple. Thank you!
+ 2
def multiples_of(number, length):
list = [i*number for i in range(1, length + 1)]
return list
print(multiples_of(7,5))
'''
Ok, let me explain.
At first, I defined a function named multiples_of (𝐿𝑒𝑎𝑟𝑛 𝑚𝑜𝑟𝑒 𝑎𝑏𝑜𝑢𝑡 "𝑭𝒖𝒏𝒄𝒕𝒊𝒐𝒏𝒔" 𝑖𝑛 P͟y͟t͟h͟o͟n͟ C͟o͟r͟e͟ ʟᴇssᴏɴ 63.1)
def multiples_of
It has 2 arguments (number and length)
def multiples_of(number, length)
Inside the function I created a list.
list = [i*number for i in range(1, length + 1)]
# This list is a shortcut to the following code:
list = []
for i in range(1, length+1):
list.append(i*number)
(𝐿𝑒𝑎𝑟𝑛 𝑚𝑜𝑟𝑒 𝑎𝑏𝑜𝑢𝑡 "𝑭𝒐𝒓 𝒍𝒐𝒐𝒑𝒔" 𝑖𝑛 P͟y͟t͟h͟o͟n͟ C͟o͟r͟e͟ ʟᴇssᴏɴ 27.1)
Now to get this list I used
return list
ℍ𝕒𝕡𝕡𝕪 ℂ𝕠𝕕𝕕𝕚𝕟𝕘 ㋡
'''
+ 2
Brit you can let range() generate the multiples, and then convert the range to a list.
Here is how: Start the range at number and add a step value of number until it exceeds number*length. The step value is the third parameter in range(start, end, step). Finally, use the list() function to convert the range type into a list type.
def multiples_of(number, length):
return list(range(number, number*length + 1, number)
+ 1
Ipang Thanks for your comment 🙂. I added the explanation to my answer.
0
Brit Welcome 🤗