Pythonic Code Flashcards
Which is considered more “Pythonic”
i = 0
while i < len(my_list):
print(my_list[i])
i += 1
or
for item in my_list:
print(item)
for item in my_list:
print(item)
Which is considered more “Pythonic”
a, b = b, a
or
temp = a
a = b
b = temp
a, b = b, a
Which is considered more “Pythonic”
evens = []
for x in numbers:
if x % 2 == 0:
evens.append(x)
or
evens = [x for x in numbers if x % 2 == 0]
evens = [x for x in numbers if x % 2 == 0]
Which is considered more “Pythonic”
for i, item in enumerate(items):
print(i, item)
or
i = 0
for item in items:
print(i, item)
i += 1
for i, item in enumerate(items):
print(i, item)
Which is considered more “Pythonic”
for i in range(len(names)):
print(names[i], scores[i])
or
for name, score in zip(names, scores):
print(name, score)
for name, score in zip(names, scores):
print(name, score)
Which is considered more “Pythonic”
found = “apple” in items
or
found = False
for item in items:
if item == “apple”:
found = True
break
found = “apple” in items
Which is considered more “Pythonic”
found = False
for word in words:
if word in text:
found = True
break
or
found = any(word in text for word in words)
found = any(word in text for word in words)
Which is considered more “Pythonic”
x, y = coords
or
x = coords[0]
y = coords[1]
x, y = coords
Which is considered more “Pythonic”
f = open(“file.txt”)
data = f.read()
f.close()
or
with open(“file.txt”) as f:
data = f.read()
with open(“file.txt”) as f:
data = f.read()
Which is considered more “Pythonic”
commands = {
“start”: start,
“stop”: stop,
“pause”: pause
}
commands[command] ()
or
xxxif command == “start”:
start()
elif command == “stop”:
stop()
elif command == “pause”:
pause()
commands = {
“start”: start,
“stop”: stop,
“pause”: pause
}
commands[command] ()
Which is considered more “Pythonic”
if “user” in data:
username = data[“user”]
else:
username = “Guest”
or
username = data.get(“user”, “Guest”)
username = data.get(“user”, “Guest”)
Which is considered more “Pythonic”
if name in {“Alice”, “Bob”, “Charlie”}:
…
or
if name in [“Alice”, “Bob”, “Charlie”]:
…
if name in {“Alice”, “Bob”, “Charlie”}:
…
Which is considered more “Pythonic”
users.sort(key=lambda x: x.age)
or
sorted_users = sorted(users, key=lambda x: x.age)
sorted_users = sorted(users, key=lambda x: x.age)
Which is considered more “Pythonic”
counts = {}
for letter in text:
if letter in counts:
counts[letter] += 1
else:
counts[letter] = 1
or
from collections import Counter
counts = Counter(text)
from collections import Counter
counts = Counter(text)
Which is considered more “Pythonic”
for _ in range(5):
do_something()
or
for i in range(5):
do_something()
for _ in range(5):
do_something()
Which is considered more “Pythonic”
for x in items:
if x == target:
break
else:
print(“Target not found!”)
or
found = False
for x in items:
if x == target:
found = True
break
if not found:
print(“Target not found!”)
for x in items:
if x == target:
break
else:
print(“Target not found!”)
Which is considered more “Pythonic”
lst = [1, 2, 3, 4, 5]
first = lst[0]
middle = lst[1:-1]
last = lst[-1]
or
first, *middle, last = [1, 2, 3, 4, 5]
first, *middle, last = [1, 2, 3, 4, 5]
def make_negative(number):
Write the body of this function to return a negative number - even if given a negative number!
return -abs(number)
abs() removes the sign of the number - always making it positive (or zero)
This is the most Pythonic way