81.3k views
4 votes
In python please:

Assume the variable authors references a list. Write a for loop that displays each author in
the list.

User Cjauvin
by
4.2k points

1 Answer

5 votes

Answer:

"

for author in authors:

print(author)

"

Step-by-step explanation:

So, to go through a list, you simply use a for loop as such:

"

for element in list:

# code

"

For each iteration, the current element value will be assigned to the variable "element" in the code, and the code in the for loop will run as well.

So to print out each author in the list, you simply do:

"

for author in authors:

print(author)

"

Also it doesn't matter what I put in between the "for" and "in", it can be any variable name, although I would have to change the "print(author)" so that it prints that variable. The only restrictions you have on this, is that variable names cannot be keywords like "in", "for", "pass", "def", etc...