Inside the ‘FOR’ loop the n is incremented automatically. Say, n in range (0,10) prints all the numbers upto 9, not 10, why?

Because the inbuild range() function is written in such a way that it should start from first parameter value(here first parameter value is 0) to second parameter value -1(here second parameter value is 10.so it becomes 10 -1 = 9). so that its is printing the values from 0 to 9.

To know more about range() function you can refer this : Python range() Function Explained with Examples

Because it generates numbers up to the stop number but **never includes the stop number in its result. It it inbuilt function in python. It was developed like this to C,C++ users can easily use this.

In Python, Using a for loop with range() generates numbers up to the stop number but never includes the stop number in its result. As you can see it starts from 0 and does not print the number 6 but in total it prints 6 numbers as spacing by default is 1.

for (int i = 1; i<=10; i++){

Serial.println(i);

}

This is because the inbuilt range function in python. Let’s take an example to better understanding like for i in range (0, N) here starting index is 0 and ending index is N-1 and incremented by 1 by default.

In python For loop the indexing starts from 0, so in range (0,10) you specify 10 increments. If count 0 as first increment you will end up in getting 10th count on nine and hence the result you are getting is ,

0,1,2,3,4,5,6,7,8,9.

You can refer to python function in more detail

https://www.geeksforgeeks.org/python-range-function/

In Python, Using a for loop with range(n), we can repeat an action a specific number of times. range(n) generates an iterator to progress the integer numbers starting with 0 and ending with (n -1). If the range is range (0,10) , it will print only 0 to 9.

hello @biasbhadra

range() function of For loop is written in such a way that first parameter to second parameters value - 1

for example:

for n in range (0 to 5):

print(n)

it will give you the output like

0

1

2

3

4

because as you can see the range function have two parameters 0 and 5

so it will start from the first parameter i.e 0 till the value of second parameter - 1 i.e 5-1 = 4, Therefore it prints only till number 4 and not number 5

We Give Two Parameter In The Range Function.The Program Will Start Giving You Number From The First Parameter Till The Second Parameter.So If You Want The Number 10 To Printed You Have To Say n in range(0,11)

The for loop works in such a way that it considers an inclusive boundary for the starting value of the range, and an exclusive boundary for the ending value of the range, just like your example of the range being (0,10). 0 will be included, and 10 will be excluded from the range.

This is not an issue with for loop. It is the property of range() function. When you a pass a single argument “6”, the output is six number of integers starting from 0 and ending with 6-1. When you input argument for starting value(1) along with end value (6), it starts with the starting number(1) but ends with 6-1.

In Computer Science we consider that there is 0 which can be counted when there is nothing , so we start our range with 0 to that number so we ignore the last digit if we start the range with 0 so that in total 10 numbers can be used from 0 to 9 inc.

But if you want your range to start with 1 then go for the upper bound as 11, so that in total still remains to be 10.

in python the for loop is prints the values from the first paramater and it ends at the second paramater-1 number because the first parameter is taken and the second parameter is not taken, we taking the numbers between the first and second parameter

Range is inbuilt in python in which the first argument is where the loop count starts and loop runs till second argument - 1. so, for example

for i in range (0,10):

print(i)

In this case output will be from 0 to 10-1 (i.e) 0 to 9.

Range is a pre-designed function which returns values for the starting value(0 in this case) to the ending value -1 (10 - 1 which is 9). That’s why the loops goes till 9.

When you use for loop it takes from first argument to second argument-1 and automatic incrementation is possible in for but it is not same in case of while, in while u have to provide seprate statement inside the loop but in the case of for it itakes while entering into the loop itself.

To get difference in accessing the numbers give a third parameter with the differenece

Ex: for i in range(0,10,3)

Because Python considers indices with (n-1) values for any iteration. Suppose in your example the value of n is 10, so the interpreter is built in such a way to compile the body of the loop up to n-1 values i.e., 9. If you want to compile your body for n=10 then give n=11 as upper limit. Even R programming has this same algorithm.

Python takes into account indices with (n-1) values for every iteration. Assuming that n in your example is 10, the interpreter is designed to compile the body of the loop up to n-1 values, or 9. If you wish to build your body for n=10, set the upper limit to n=11. This algorithm is used in R programming as well.

Because when n reaches 10, the loop stops.