PARTH DUBEY
PARTH DUBEY

Reputation: 53

What is the output of the loop in python?

This is a loop in PYTHON 3.X.

for i in range(2, 2):
   print(i)

The result that I got after running this program was a blank console. What type of output we get from this loop.

Upvotes: 0

Views: 94

Answers (2)

10 Rep
10 Rep

Reputation: 2270

The reason you're getting blank output is because what the range() function does.

The first argument in the range() function tells the function which number to iterate to.

So, if this was your code:

for i in range(2):
    print(i)

Then your output would be:

0
1

It starts from 0 and prints two numbers, because of the two.

However, the second parameter in your code tells it what to count by. For example, you added a 2 for the second parameter. This tells the program to skip count by 2, and since you have only 2 output in the first place, the program doesn't print anything

Read up on range in python 3.

Hopefully this helps!

Upvotes: 1

Blake
Blake

Reputation: 51

The range of numbers returned are integers from start to stop. ( the first param being start, and second being stop when 2 are given)

It includes the start int, but not the stop int.

hence, range(2,2) contains no numbers in between.

range(2,4) returns 2,3 but not 4.

Documentation here.

Upvotes: 1

Related Questions