sds2019 icon indicating copy to clipboard operation
sds2019 copied to clipboard

What do I write after the for in a for loop?

Open Emilie-student opened this issue 6 years ago • 1 comments

When I have to create a for loop, what do I write after the for?

Ex. for x in y:

What am I supposed to write instead of the x? And is it supposed to be related to the topic?

Emilie-student avatar Jul 09 '19 11:07 Emilie-student

The object x refers to an element in y. We could also have written for a in y and then the elements of y we instead call a. Instead of x we could also write something unrelated, e.g. for dog in cats where dog is an element of cats.

After a for loop (i.e. after the colon) you write need to begin an indentation which is a new line where you press tab (or make four spaces). An example is as follow where the function my_script is executed for each element x in y.

for x in y:
    my_script()

BONUS info: this is the same you make if statements and define functions.

abjer avatar Jul 09 '19 14:07 abjer