Cheatsheets / Learn Python 3
Bucles
break Palabra clave
En un bucle, la break palabra clave escapa del bucle,
independientemente del número de iteración. Una vez numbers = [0, 254, 2, -1, 3]
que se break ejecuta, el programa continuará
ejecutándose después del ciclo. for num in numbers:
En este ejemplo, la salida sería: if (num < 0):
print("Negative number detected!")
● 0
break
● 254
print(num)
● 2
● Negative number detected!
# 0
# 254
# 2
# Negative number detected!
Comprensión de la lista de Python
Las listas por comprensión de Python proporcionan una
forma concisa de crear listas. Se compone de soportes # List comprehension for the squares of
que contienen una expresión seguida de una cláusula all even numbers between 0 and 9
para, a continuación, cero o más para o si cláusulas: result = [x**2 for x in range(10) if x % 2
[EXPRESSION for ITEM in LIST <if CONDITIONAL>] . == 0]
The expressions can be anything - any kind of object can
go into a list.
print(result)
A list comprehension always returns a list.
# [0, 4, 16, 36, 64]
Python For Loop
A Python for loop can be used to iterate over a list of
items and perform a set of actions on each item. The for <temporary variable> in <list
syntax of a for loop consists of assigning a temporary variable>:
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.
#each num in nums will be printed below
nums = [1,2,3,4,5]
for num in nums:
print(num)
The Python continue Keyword
In Python, the continue keyword is used inside a loop to
skip the remaining code inside the loop code block and big_number_list = [1, 2, -1, 4, -5, 5, 2,
begin the next loop iteration. -9]
# Print only positive numbers:
for i in big_number_list:
if i < 0:
continue
print(i)
Python Loops with range().
In Python, a for loop can be used to perform an action
a speci c number of times in a row. # Print the numbers 0, 1, 2:
The range() function can be used to create a list that for i in range(3):
can be used to specify the number of iterations in a for print(i)
loop.
# Print "WARNING" 3 times:
for i in range(3):
print("WARNING")
In nite Loop
An in nite loop is a loop that never terminates. In nite
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.
Python while Loops
In Python, a while loop will repeatedly execute a code
block as long as a condition evaluates to True . # This loop will only run 1 time
The condition of a while loop is always checked rst hungry = True
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
# This loop will run 5 times
i = 1
while i < 6:
print(i)
i = i + 1
Python Nested Loops
In Python, loops can be nested inside other loops. Nested
loops can be used to access items of lists which are inside groups = [["Jobs", "Gates"], ["Newton",
other lists. The item selected from the outer loop can be "Euclid"], ["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)