Learn Python 3_ Loops Cheatsheet _ Codecademy
Learn Python 3_ Loops Cheatsheet _ Codecademy
Loops
break Keyword
In a loop, the break keyword exits the loop immediately, numbers = [0, 254, 2, -1, 3]
regardless of the iteration number. Once break
executes, the program will continue executing from the
first line after the loop. for num in numbers:
In this example, the output would be: if (num < 0):
0 print("Negative number detected!")
254
break
2
Negative number detected! print(num)
# 0
# 254
# 2
# Negative number detected!
Python list comprehensions provide a concise way for # List comprehension for the squares of
creating lists. It consists of brackets containing an
all even numbers between 0 and 9
expression followed by a for clause, then zero or more for
or if clauses: [EXPRESSION for ITEM in LIST <if result = [x**2 for x in range(10) if x % 2
CONDITIONAL>] . == 0]
The expressions can be anything - any kind of object can
go into a list.
A list comprehension always returns a list. print(result)
# [0, 4, 16, 36, 64]
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-loops/cheatsheet 1/4
12/3/24, 3:17 PM Learn Python 3: Loops Cheatsheet | Codecademy
A Python for loop can be used to iterate over a list of for <temporary variable> in <list
items and perform a set of actions on each item. The
variable>:
syntax of a for loop consists of assigning a temporary
value to a variable on each successive iteration. <action statement>
When writing a for loop, remember to properly indent <action statement>
each action, otherwise an IndentationError will result.
In Python, the continue keyword is used inside a loop to big_number_list = [1, 2, -1, 4, -5, 5, 2,
skip the remaining code inside the loop code block and
-9]
begin the next loop iteration.
In Python, a for loop can be used to perform an action a # Print the numbers 0, 1, 2:
specific number of times in 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 print(i)
loop.
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-loops/cheatsheet 2/4
12/3/24, 3:17 PM Learn Python 3: Loops Cheatsheet | Codecademy
Infinite Loop
In Python, a while loop will repeatedly execute a code # This loop will only run 1 time
block as long as a condition 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 while hungry:
initially, then the code block will never run. print("Time to eat!")
hungry = False
In Python, loops can be nested inside other loops. Nested groups = [["Jobs", "Gates"], ["Newton",
loops can be used to access items of lists which are inside
"Euclid"], ["Einstein", "Feynman"]]
other lists. The item selected from the outer loop can be
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)
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-loops/cheatsheet 3/4
12/3/24, 3:17 PM Learn Python 3: Loops Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-loops/cheatsheet 4/4