"for x in list" Assigns each list item to an x, Then how does it work "for i in range (10):" and why do you need "i"?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

"for x in list" Assigns each list item to an x, Then how does it work "for i in range (10):" and why do you need "i"??

just don't have to explain to me that "for i in range (10)" repeats the cycle 10 times, I know that.

1st Sep 2020, 1:07 PM
иван
иван - avatar
12 Answers
+ 16
range() is a functions that generates numbers upto the limit Here , for i in range(10) In this after in* you can use anything which u need to iterate ,we use range() because range can have 1 2 or 3 parameters >If range(10) // then it generates 0 to 9 >If range(1,10) //then it generates same number but starting point will be 1 ...so it will be 1 to 9 >If range(1,10,2) // then it will start from 1 and adds 2 for each iteration...it generates 1,3,5,7,9.... And "i" is a variable which we use to step each element as iteration flows in the list ,tuple or watever which is valid for the for loop! I hope u will understand;)
1st Sep 2020, 1:22 PM
chaithra Gowda
chaithra Gowda - avatar
+ 4
1. For i in list => i = element of the list 2. For i in range(len(list)) => i = number that represents index so here list[i] = element of the list! It is more preferrable to use the 2nd option because in the first option the index is unknown but in the second option both element and index are known!
1st Sep 2020, 2:43 PM
Namit Jain
Namit Jain - avatar
+ 2
You need a variable to use that specific value in further lines..
1st Sep 2020, 1:12 PM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 2
range(10) is a returns an iterable of value from 0 to 10-1. So each value is assigned to i in a iterable way... i =0 .. .. i = 9
1st Sep 2020, 1:18 PM
Jayakrishna 🇮🇳
+ 1
I changed the question
1st Sep 2020, 1:12 PM
иван
иван - avatar
+ 1
I don't fully understand how "for i in range" works
1st Sep 2020, 1:13 PM
иван
иван - avatar
+ 1
in case of range, it is loop!!! it iterates the same code, function, logic, comparison whatever you put in loop.... from start to end... Usually start point is 0 and step is 1, but you can define other start point and step
1st Sep 2020, 1:22 PM
Shadoff
Shadoff - avatar
+ 1
иван In a cycle way, in each cycle the temparary variable is assigned next value so that is called here as a current iterative value.. In for i in range(10): initially i is assigned 0. In next ireation, I assigned next value 1, so this repeats until reach max value.. For all these itraration code in loop executed.. If still not clear, then Do you asking about loop or range function? Or write that code and observe output of that.. You can understand it more clearly..
1st Sep 2020, 1:56 PM
Jayakrishna 🇮🇳
+ 1
for i in range(10) has a similar meaning to for(i=0,i<10,i++) in other languages
1st Sep 2020, 5:41 PM
Joey Lim
Joey Lim - avatar
0
i, x are tempary user defined variables there. You can use any valid variable. Ex : for num in range(10): for j in range(10):
1st Sep 2020, 1:10 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 what does iterative value mean?
1st Sep 2020, 1:20 PM
иван
иван - avatar