Learn

Good job! In this lesson, you learned

  • How to write a for loop.
  • How to use range in a loop.
  • How to write a while loop.
  • What infinite loops are and how to avoid them.
  • How to control loops using break and continue.
  • How to write elegant loops as list comprehensions.

Let’s get some more practice with these concepts!

Instructions

1.

Create a list called single_digits that consists of the numbers 0-9 (inclusive).

2.

Create a for loop that goes through single_digits and prints out each one.

3.

Before the loop, create a list called squares. Assign it to be an empty list to begin with.

4.

Inside the loop that iterates through single_digits, append the squared value of each element of single_digits to the list squares. You can do this before or after printing the element.

5.

After the for loop, print out squares.

6.

Create the list cubes using a list comprehension on the single_digits list. Each element of cubes should be an element of single_digits taken to the third power.

7.

Print cubes.

Good job!

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?