Learn Python 3 - Loops Cheatsheet - Codecademy
Learn Python 3 - Loops Cheatsheet - Codecademy
Loops
break Keyword
In a loop, the break keyword escapes the loop, regardless of the iteration number. numbers = [0, 254, 2, -1, 3]
Once break executes, the program will continue to execute after the loop.
In this example, the output would be:
0 for num in numbers:
254 if (num < 0):
2 print("Negative number detected!")
Negative number detected!
break
print(num)
# 0
# 254
# 2
# Negative number detected!
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-loops/cheatsheet 1/5
09/12/2023, 08:08 Learn Python 3: Loops Cheatsheet | Codecademy
Python list comprehensions provide a concise way for creating lists. It consists of # List comprehension for the squares of all even numbers
brackets containing an expression followed by a for clause, then zero or more for or if
between 0 and 9
clauses: [EXPRESSION for ITEM in LIST <if CONDITIONAL>] .
The expressions can be anything - any kind of object can go into a list. result = [x**2 for x in range(10) if x % 2 == 0]
A list comprehension always returns a list.
print(result)
# [0, 4, 16, 36, 64]
A Python for loop can be used to iterate over a list of items and perform a set of for <temporary variable> in <list variable>:
actions on each item. The syntax of a for loop consists of assigning a temporary value
<action statement>
to a variable on each successive iteration.
When writing a for loop, remember to properly indent each action, otherwise an <action statement>
IndentationError will result.
#each num in nums will be printed below
nums = [1,2,3,4,5]
for num in nums:
print(num)
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-loops/cheatsheet 2/5
09/12/2023, 08:08 Learn Python 3: Loops Cheatsheet | Codecademy
In Python, the continue keyword is used inside a loop to skip the remaining code big_number_list = [1, 2, -1, 4, -5, 5, 2, -9]
inside the loop code block and begin the next loop iteration.
In Python, a for loop can be used to perform an action a specific number of times in # Print the numbers 0, 1, 2:
a row.
for i in range(3):
The range() function can be used to create a list that can be used to specify the
number of iterations in a for loop. print(i)
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-loops/cheatsheet 3/5
09/12/2023, 08:08 Learn Python 3: Loops Cheatsheet | Codecademy
Infinite Loop
An infinite loop is a loop that never terminates. Infinite loops result when the
conditions of the loop prevent it from terminating. This could be due to a typo in the
conditional statement within the loop or incorrect logic. To interrupt a Python program
that is running forever, press the Ctrl and C keys together on your keyboard.
In Python, a while loop will repeatedly execute a code block as long as a condition # This loop will only run 1 time
evaluates to True .
hungry = True
The condition of a while loop is always checked first before the block of code runs. If
the condition is not met initially, then the code block will never run. while hungry:
print("Time to eat!")
hungry = False
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-loops/cheatsheet 4/5
09/12/2023, 08:08 Learn Python 3: Loops Cheatsheet | Codecademy
In Python, loops can be nested inside other loops. Nested loops can be used to access groups = [["Jobs", "Gates"], ["Newton", "Euclid"],
items of lists which are inside other lists. The item selected from the outer loop can be
["Einstein", "Feynman"]]
used as the list for the inner loop to iterate over.
# This outer loop will iterate over each list in the groups
list
for group in groups:
# This inner loop will go through each name in each list
for name in group:
print(name)
Print Share
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-loops/cheatsheet 5/5