Can someone u make me understand for loop in Python, i have studied C++ and comparing to C++ for loop its a bit confusing ! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can someone u make me understand for loop in Python, i have studied C++ and comparing to C++ for loop its a bit confusing !

Like in c++ For(i=0; i<=10; i++) but in Python its something like for i in range (10) but there's no increment and why always we have to use range ???

14th Mar 2020, 10:35 AM
Zeeshan Ahmad
Zeeshan Ahmad - avatar
12 Answers
+ 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]
14th Mar 2020, 11:24 AM
Kostia Gorbach
Kostia Gorbach - avatar
+ 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
14th Mar 2020, 11:14 AM
Rei
Rei - avatar
+ 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.
15th Mar 2020, 1:02 PM
Sonic
Sonic - avatar
+ 1
if you dont specify it its 1 dy default. For example: for x in range(0, 10, 4): # here the increment is 4
14th Mar 2020, 11:20 AM
Rei
Rei - avatar
+ 1
It can be a negative number as well: for x in range(9, -1, -1): # here it goes from 9 to 0
14th Mar 2020, 11:22 AM
Rei
Rei - avatar
+ 1
Yes
14th Mar 2020, 11:22 AM
Rei
Rei - avatar
+ 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
14th Mar 2020, 11:23 AM
HonFu
HonFu - avatar
+ 1
Thank you everyone ❤️ i get it
14th Mar 2020, 11:27 AM
Zeeshan Ahmad
Zeeshan Ahmad - avatar
0
Rei i understand what is Range but is increment in for loop is done by default ?
14th Mar 2020, 11:18 AM
Zeeshan Ahmad
Zeeshan Ahmad - avatar
0
Rei so thats means here the increment part is done by the Range function for the "for loop" ?
14th Mar 2020, 11:22 AM
Zeeshan Ahmad
Zeeshan Ahmad - avatar
0
https://wiki.python.org/moin/ForLoop. Please go through this.This may help you.
15th Mar 2020, 4:38 PM
narayanaprasad
narayanaprasad - avatar
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👍
16th Mar 2020, 5:54 AM
Muhammed Shafi
Muhammed Shafi - avatar