How To Repeat N Times In Python

This tutorial will showcase two main ways to repeat N times in Python. You can make use of them when there is a need to perform the same action more than once.

Here is the sample code to repeat N times in Python

n = int(input('How many times do you want to repeat:' ))
string = 'This is tutopal.com!'

#Solution 1: Use range() to repeat N times in Python
print('#Solution 1:')
for i in range(n):
    print('Results when using range(): ', string)

#Solution 2: Use itertools.repeat() to repeat N times in Python
import itertools
print('#Solution 2:')
for i in itertools.repeat(None, n):  
    print('Results when using itertools.repeat(): ', string)

Output

How many times do you want to repeat:3
#Solution 1:
Results when using range():  This is tutopal.com!
Results when using range():  This is tutopal.com!
Results when using range():  This is tutopal.com!
#Solution 2:
Results when using itertools.repeat():  This is tutopal.com!
Results when using itertools.repeat():  This is tutopal.com!
Results when using itertools.repeat():  This is tutopal.com!

View detailed program results

Repeat N Times In Python

Using range()

One of the most popular applications of Python’s for loops is to repeat a code block a fixed number of times. In this case, you will need to use it in conjunction with an iterable, which is an object that can be iterated over. Examples of iterables in Python include strings, lists, tuples, and so on.

To specify the number of times you want to repeat a block of code, you can use the function range(). It can take up to three integer arguments:

range(start, stop, step)

Simply put, this function creates an immutable sequence of integers ranging from ‘start’ up to the value right before ‘stop’, with a ‘step’ between them. If you omit the start argument, Python uses the default value 0. Similarly, the step argument defaults to 1 when it is omitted.

The function returns a range object. Compared to regular iterables in Python like lists or tuples, this range type consumes only a small constant amount of memory, no matter how big the range is.

If you give the function range() just one parameter (such as n), Python interprets it as the stop argument:

range(0, n, 1)

You can see the values of a range object with list():

>>> list(range(5))

[0, 1, 2, 3, 4]

You can combine this with a for loop to repeat any code block a certain number of times.

for i in range(n):

    # Insert your code here. It will be repeated n times.

Here is an example:

n = int(input('How many times do you want to repeat the function:' ))
string = 'This is tutopal.com!'

for i in range(n):
    print(string)

Output:

How many times do you want to repeat the function:4
This is tutopal.com!
This is tutopal.com!
This is tutopal.com!
This is tutopal.com!

The snippet above will ask for the number of repeating times from the user and convert it to an integer. This value is used by the function range() to create a range object with n elements. Finally, the for loop executes the code inside it (which prints our message) each time it goes through an element of this iterable.

Using itertools.repeat()

The function repeat() from module itertools is a great alternative to range() in many situations. This built-in module provides operations with special iterators designed for efficient looping. It allows Python developers to build complex, specialized tools that run fast and don’t consume much memory.

In particular, the function repeat() can return an object a fixed number of times or even indefinitely.

Syntax:

itertools.repeat(object[, times])

The argument times are options. If you omit it, the function will return the object over and over again. This is how you can use it to replace range():

import itertools

for i in itertools.repeat(None, n):

    # Insert your code here. It will be repeated n times.

Since we don’t need any particular object from the function, we should pass None as its main argument. We can rewrite the example above with itertools.repeat():

import itertools
n = int(input(‘How many times do you want to repeat the function:’ ))
string = ‘This is tutopal.com!’

for i in itertools.repeat(None, n):
    print(string)

Output:

How many times do you want to repeat the function:2
This is tutopal.com!
This is tutopal.com!

Conclusion

You can repeat N times in Python with functions range() and itertools.repeat(). They both create iterable objects that a for loop can go through and repeat your code a fixed number of times.

Leave a Reply

Your email address will not be published. Required fields are marked *