Exercise 11
Write an algorithm in python as a function which takes as parameters a tuple (L, n) formed by a list L of numbers and an integer n and which returns the list obtained from L by multiplying its elements by n .
Example if L = [13, 11, 7, 25] and n = 2 the function returns the list: [26, 22, 14, 50]
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def listMultiply(L,n): # initialization of the desired list l_mult = [] # iterate through and multiply list elements by n for x in L: l_mult.append(n*x) return l_mult # Example n = 2 L = [13, 11, 7, 25] print(listMultiply(L,n)) # The output is: [26, 22, 14, 50] |
Younes Derfoufi
CRMEF OUJDA
Acheter sur Très Facile !
-
Langage Python pour les Nuls, 3e éd - John Paul Mueller
€ 24,00 Acheter le livre -
Tenda Routeur WiFi 6 RX12 Pro, Router Tenda WiFi AX3000 Bi-Bande, 5 * 6dBi Antennes à Haut Puissance, Large Couverture,
€ 57,00 Acheter le livre -
Python pour les Nuls, 4è édition - John Paul Mueller
€ 25,00 Acheter le livre
1 thought on “Solution Exercise 11: multiply the elements of a Python list”