Pythonic Code Flashcards

1
Q

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)

A

for item in my_list:
print(item)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which is considered more “Pythonic”

a, b = b, a

or

temp = a
a = b
b = temp

A

a, b = b, a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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]

A

evens = [x for x in numbers if x % 2 == 0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

for i, item in enumerate(items):
print(i, item)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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)

A

for name, score in zip(names, scores):
print(name, score)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which is considered more “Pythonic”

found = “apple” in items

or

found = False
for item in items:
if item == “apple”:
found = True
break

A

found = “apple” in items

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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)

A

found = any(word in text for word in words)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which is considered more “Pythonic”

x, y = coords

or

x = coords[0]
y = coords[1]

A

x, y = coords

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which is considered more “Pythonic”

f = open(“file.txt”)
data = f.read()
f.close()

or

with open(“file.txt”) as f:
data = f.read()

A

with open(“file.txt”) as f:
data = f.read()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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()

A

commands = {
“start”: start,
“stop”: stop,
“pause”: pause
}
commands[command] ()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which is considered more “Pythonic”

if “user” in data:
username = data[“user”]
else:
username = “Guest”

or

username = data.get(“user”, “Guest”)

A

username = data.get(“user”, “Guest”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which is considered more “Pythonic”

if name in {“Alice”, “Bob”, “Charlie”}:

or

if name in [“Alice”, “Bob”, “Charlie”]:

A

if name in {“Alice”, “Bob”, “Charlie”}:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which is considered more “Pythonic”

users.sort(key=lambda x: x.age)

or

sorted_users = sorted(users, key=lambda x: x.age)

A

sorted_users = sorted(users, key=lambda x: x.age)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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)

A

from collections import Counter
counts = Counter(text)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which is considered more “Pythonic”

for _ in range(5):
do_something()

or

for i in range(5):
do_something()

A

for _ in range(5):
do_something()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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!”)

A

for x in items:
if x == target:
break
else:
print(“Target not found!”)

17
Q

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]

A

first, *middle, last = [1, 2, 3, 4, 5]

18
Q

def make_negative(number):

Write the body of this function to return a negative number - even if given a negative number!

A

return -abs(number)

abs() removes the sign of the number - always making it positive (or zero)

This is the most Pythonic way