12 ответов
+ 5
In Python you can write something like:
for i in [1, 5, 10]:
print(i)
#for every element in list [1, 5, 10] print this element
And result is 1 5 10.
range(x) function create a list with numbers from 0 to x. For example range(4) create a list [0, 1, 2, 3].
Also you can set from which number you want to start and which step.
range(3, 8, 2) will give you a list from 3 to 7 with two-digit step [3, 5, 7]
+ 2
for x in range(0 # the first parameter tell you the start index of loop like i = 0 in cpp, second parametr in your example is 10 its like i < 10 in cpp 10 its not included, third parameter its the increment, by default its 1)
Hope u understand it
+ 2
The increment is one by default buy you can provide an optional step and a start that is non zero. Refer to the Python tutorial or else Google.
+ 1
if you dont specify it its 1 dy default.
For example:
for x in range(0, 10, 4):
# here the increment is 4
+ 1
It can be a negative number as well:
for x in range(9, -1, -1):
# here it goes from 9 to 0
+ 1
Yes
+ 1
I explain (hopefully) everything about for in Python in these two tutorials - I hope they'll help you!
Also feel free to ask questions.
https://code.sololearn.com/cSdiWZr4Cdp7/?ref=app
https://code.sololearn.com/ck6HicJ6Z8jG/?ref=app
+ 1
Thank you everyone ❤️ i get it
0
Rei i understand what is Range but is increment in for loop is done by default ?
0
Rei so thats means here the increment part is done by the Range function for the "for loop" ?
0
https://wiki.python.org/moin/ForLoop. Please go through this.This may help you.
0
The loop in python and C++ are based on same logic. Eventhough there's are differences in their implementation..
In python you can use the for loop as
for i in range (0,10,1):
//Here the loop will be incremented by 1 each time and it is initialized as 0. Also remember there's no flower braces ({}) as in C++.
In C++ the same loop can be implemented as
for (i=0;i <=10;i++)
{ .......................;
...................;
}
These differences quite easy to follow....
Good luck👍