Idiomatic Python Flashcards

1
Q

x = y = z = 10
x = 11
Whats the value of y, z now?

A

its still 10

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

How to swap values a and b

A

(a,b) = (b,a)

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

concatenate_this_list = [“FistString”, “SecondString”, “ThirdString”]
concatenated_string = ‘ ‘.join(concatenate_this_list)
print (concatenated_string)

A

’ ‘ leaves space between list elements so the result is
FirstString SecondString ThirdString

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

Whats the pythonic way of this:
x=y=z=10
if x<=y and y<=z:
print (“All equal”)

A

if x<=y<=z

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

Whats the pythonic way of this?
if name == “Tom” or name == “Nick” or name == “Harry”:
is_generic_name = True

A

is_generic_name = name in [“Tom”, “Nick”, “Harry”]

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

Empty squences like (), [], {} are falsy?

A

Yes

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

None is falsy?

A

Yes

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

Numeric 0 and emtpy strings are falsy?

A

yes

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

Whats the pythonic for this?
colors = [“red”, “green”, “blue”, “yellow”]
for i in range(len(colors)-1, -1, -1):
print (colors[i])

A

for color in reversed(colors)

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

Output?
fruits = [“strawberry”, “watermelon”, “banana”]
colors = [“red”, “green”, “yellow”, “blue”]
for fruit, color in zip(fruits, colors):
print (f”{fruit} –> {color}”)

A

strawberry –> red
watermelon –> green
banana –> yellow

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

original_list = range(4, -4, -1)
has_some_positive = any(element >0 for element in original_list)

A

True

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

fruit_colors = {“watermelon”: “green”, “strawberry”: “red”, “banana”: “yellow”}

for fruit, color in fruit_colors.items():
print (f”Fruit: {fruit} is {color}”)

A

.keys if u dont care about the values
.items if you care both about the keys and values

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

fruits = [“strawberry”, “watermelon”, “banana”]
colors = [“red”, “green”, “yellow”]

fruit_colors = dict(zip(fruits, colors))

print (fruit_colors)

A

Creates a dict out of lists

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

person_name = {“first_name”: “Nikos”, “surname”: “Giatrakos”}
person_characteristics = {“age”: 40, “height”: 1.85}

person = {**person_name, **person_characteristics}
person

A

merge dictionaries

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

Pythonic of :

def check_generic_name(name):
if name in [“Tom”, “Nick”, “Harry”]:
return name
return “Not included”

print (check_generic_name(“Nick”))

A

def check_generic_name(name):
return name if name in [“Tom”, “Nick”, “Harry”] else “Not included”

print (check_generic_name(“Nikos”))

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