Range function with step higher than stop

Issue

Could someone explain me why the output is 22 here:

def fun():
    for x in range(22,23,24):
        print(x)
fun()

Solution

The syntax for range is range(start, stop, step)

The range of numbers to be printed starts from 22, to 23 (end point not included), in steps of 24.

The first number is 22, which gets printed.

The next number will be 22 + 24 = 46, which is greater than 23, so it doesn’t get printed and the loop terminates.

Answered By – Deepthi Tabitha Bennet

Answer Checked By – Mary Flores (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.