For loop in python | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

For loop in python

Hey, I've seen some for loops that have a variable name before the for statement. Can you give me an example and explain what it does?

26th Sep 2020, 8:45 PM
Yahel
Yahel - avatar
7 Antworten
+ 3
Maybe it was a list comprehension? lst = [i for i in range(10)] # something like this? This creates a list populated by each value that i takes in the range given. print(lst) # [0,1,2,3,4,5,6,7,8,9]
26th Sep 2020, 10:00 PM
Russ
Russ - avatar
+ 1
Ipang I cant find it.. that's why I've asked.. sorry
26th Sep 2020, 9:37 PM
Yahel
Yahel - avatar
+ 1
Russ yes! Thank you
26th Sep 2020, 10:22 PM
Yahel
Yahel - avatar
+ 1
Um, no. Not to get the same result. Have you tried it yourself?
26th Sep 2020, 10:36 PM
Russ
Russ - avatar
0
yahel Can you instead copy/paste that code you seen in the Description (just the loop part)? I think it makes more sense for others to explain ...
26th Sep 2020, 9:23 PM
Ipang
0
Russ but cant I just do it without the i before the for keyword?
26th Sep 2020, 10:23 PM
Yahel
Yahel - avatar
0
Russ ok, thanks!
26th Sep 2020, 11:14 PM
Yahel
Yahel - avatar