FAQ: Learn Python: Loops - Create a For Loop

This community-built FAQ covers the “Create a For Loop” exercise from the lesson “Learn Python: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Create a For Loop

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

5 posts were split to a new topic: What is the type of a temporary variable in a for loop?

Just a question about what can used in the for loop. From the lesson,

The general way of writing a for loop is:

for <temporary variable> in <list variable>:
    <action>

Is “list” saying that the variable has to be a list type or is it casting the variable to a list? Couldn’t you use any thing you could iterate on like a range(x)?

python has no concept of casting

the specification of what you iterate through is an expression, it does not need to be a list it does not need to be a variable it does need to be iterable

the variable is not temporary, it exists after the loop as well. it’s a variable, nothing special

This is a separate question.

I am just wondering why we can’t assign more than 1 word to the temporary variable?

For example, ‘board game’ instead of just ‘game’?

The one way I found that I can resolve it is putting an underscore in between (board_game).

“Why can’t we assign more than one word to the temporary variable?”

Remember a variable is like a box, the specific collection of symbols/characters is the label that tells us what’s in the box.
Each collection of symbols has a specific definition/purpose. By using a space you’re using two separate collections (aka words) that then require two separate definitions. Remember computers don’t speak english, they don’t know what a word (variable) means they just know that this collection of symbols refers to this other collection of symbols.

How would you separate the two lists with a space?

I know of the the \n method but when you put it in the for-loop’s print statement, it’ll just skip the lines between each element.

I put print(‘\n’) between the loops and it worked but is there another option?

Hello! I just want to ask about space before “for” loop. As you can see Ive made a mistake:

Can you explain this behaviour of the code?

Thank you. :slight_smile:

Programming best practices suggest we make our temporary variables as descriptive as possible. Since each iteration (step) of our loop is accessing an ingredient it makes more sense to call our temporary variable ingredient rather than i or item.

grammar.exe has failed
oh and is this statement true? ‘i’ or ‘item’ is perfectly fine