FAQ: Learn Python: Loops - Review

This community-built FAQ covers the “Review” 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 Review

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!

11 posts were split to a new topic: I’m struggling to understand this exercise?

Apart from others, what I’ve realised so far about loops, is that great attention must be paid when we use “print”. Sometimes it is necessary to “print” inside the loop with indentation, but in other cases this brings trouble and “print” should be written outside loop without indentation.

To be more precise, about the loop with digits:
for item in single_digits:
…print(item)
…squares.append(item**2)
print(squares)
Here, the output seems normal. The first print(inside loop), gives the items from 0 to 9 in a vertical order and then, the second print (outside loop) gives the all squares in a list [0,1,4,9,16,…81]

However, if both “print” are inside loop, the output is complex:
0
[0]
1
[0, 1]
2
[0, 1, 4]
3
[0, 1, 4, 9] and so on…

I suppose this is because of the iterations. It is like every iteration in the loop consists of two “sub-iterations”, one for the print items and one for the print squares. Is it true?
If this is the case, why the result of the second “sub-iteration” is a list including all the squares so far and not the result of the squared item? i.e.

2
4
3
9
4
16…

Hello,guys!Help me with the code,please!Why can`t I use

single_digits=range(10)
for item in single_digits:
  print(item)
  squares=[]
for element in squares:
  squares.append(item**2)
  print(squares)
  cubes=[item**3 for item in single_digits]
  print(cubes)

instead of

single_digits = range(10)
squares = []

for item in single_digits:
  print(item)
  squares.append(item**2)
  
cubes = [item**3 for item in single_digits]
print(cubes)