Last updated: Apr 10, 2024
Reading timeยท4 min

To call a function N times:
range() class to create a range object of length N.for loop to iterate over the range object.def print_message(message): print(message) number = 1 for _ in range(5): number = number * 2 print_message('bobbyhadz.com') print(number) # ๐๏ธ 32

We used a for loop to iterate over a range
object N times.
The range() class is commonly used for looping
a specific number of times in for loops and takes the following arguments:
| Name | Description |
|---|---|
start | An integer representing the start of the range (defaults to 0) |
stop | Go up to, but not including the provided integer |
step | Range will consist of every N numbers from start to stop (defaults to 1) |
If you only pass a single argument to the range() constructor, it is
considered to be the value for the stop parameter.
for n in range(5): print(n) # ๐๏ธ 0 1 2 3 4 result = list(range(5)) # ๐๏ธ [0, 1, 2, 3, 4] print(result)
start argument is omitted, it defaults to 0 and if the step argument is omitted, it defaults to 1.If values for the start and stop parameters are provided, the start value
is inclusive, whereas the stop value is exclusive.
result = list(range(1, 5)) # ๐๏ธ [1, 2, 3, 4] print(result)
On each iteration of the for loop, we call a function and reassign a variable.
def print_message(message): print(message) number = 1 for _ in range(5): number = number * 2 print_message('bobbyhadz.com') print(number) # ๐๏ธ 32
You can use this approach to perform any other action N times.
_ for the variable's name. There is a convention to use an underscore _ for placeholder variables we don't intend to use.Alternatively, you can use the itertools.repeat() class.
This is a three-step process:
itertools.repeat() class to create an iterator of length N.for loop to iterate over the iterator.from itertools import repeat def print_message(message): print(message) number = 1 for _ in repeat(None, 5): number = number * 2 print_message('bobbyhadz.com') print(number) # ๐๏ธ 32

The itertools.repeat() class takes an object and the number of times the object should be repeated as arguments.
from itertools import repeat # ๐๏ธ ['x', 'x', 'x', 'x', 'x'] print(list(repeat('x', 5)))
The method returns an iterator containing the object repeated N times.
The itertools.repeat() class is a little faster than the range() class in
this scenario because it doesn't have to create an int object on each
iteration.
However, the difference in performance is negligible (~15%) for most applications.
This is a two-step process:
range object with length N.def my_function(number): return number * 5 results = [my_function(10) for _ in range(3)] print(results) # ๐๏ธ [50, 50, 50]

We used a
list comprehension to
iterate over the range object.
On each iteration, we call the function and return the result.
for loopYou can also use a for loop to call a function N times and store the results
in a list.
def increment(num): return num + 100 results = [] for i in range(1, 6): results.append(increment(i)) print(results) # ๐๏ธ [101, 102, 103, 104, 105]
On each iteration of the for loop, we call the function and append the result
to the list.
After the last iteration, the list contains the output of all of the function's invocations.
You can also use the map() function to call a function N times.
def increment(num): return num + 100 list_of_arguments = [1, 2, 3, 4, 5] results = list(map(increment, list_of_arguments)) print(results) # ๐๏ธ [101, 102, 103, 104, 105]
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
We passed the increment function to map, so it gets called with each item
from the list.
The last step is to use the list() class to convert the map object to a
list.
while loopYou can also use a while loop to call a function N times.
def print_message(message): print(message) n = 3 while n > 0: print_message('bobbyhadz.com') n -= 1 # bobbyhadz.com # bobbyhadz.com # bobbyhadz.com
We initialized the n variable to 3 and used a while loop to iterate for as
long as the variable is greater than 0.
On each iteration, we call the print_message function and decrement the n
variable.
Once the n variable is set to 0 or less than 0, the condition is no longer
met and the while loop exits.
You can learn more about the related topics by checking out the following tutorials: