0

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.

1
  • What do you expect to output? Commented May 5, 2020 at 15:27

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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!

5 Comments

The two parameter version of range is range(start, stop) not range(start, step)
@SpoonMeiser I was simply speaking about the OP's code, not in general.
No, I mean that your paragraph about the second parameter to range is just incorrect; the answer would be improved by fixing that.
@SpoonMeiser OK I will fix it. Thanks for pointing out the error!
I really do appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.