Python Format Flashcards

1
Q

Boolean Expression
if 7<0 and 8>1:
print(“true”)

elif False or (0<100):
print(“False”)

A

False

(why?: becus there is an or statement and (0<100) is true, thus it iterates to the next line

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

Formatting Syntax?

A

format(value, format spec)

{[index], width.precision[type]

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

print(“I {} like games”).format(“darren”))

A

Result : I darren like games

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

print(“—”, format(1.23456, 10.2%))

A

converts 1.23456 to percentage -> 123.456%

rounds to .2 float -> 123.45

pads 4 additional space as it requires a minimum of 10 digits. (decimal included)

result : —, 123.45%

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

print(‘{2} {1} {0}’.format(‘directions’, ‘the’, ‘Read’))

A

Result : Read the directions

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

print(‘The first {p} was alright, but the {q} {p} was tough.’.format(p = ‘second’, q= ‘last’))

A

Result:

The first second was alright, but the last second was tough.

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

print(“{0} ate {1:.1%} of the cake ! “.format(“tom”, (2/3)))

A

tom ate 66.7% of the cake

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

print(“{0} ate {1:.1%} of the cake ! “.format(“tom”, (2/3)*100))

A

tom ate 6666.7% of the cake !

Because its already converted to percentage in the bracket, hence multiplying by 100 is redundant.

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

num = 3.14159 print(f”The valueof pi is: {num:{1}.{5}}”)

A

The value of pi is 3.1415

basically round off to 5 sig fig

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

name= “Tom”
money = 6.17
print(f”{name} has {money*money:9.2%}”)

A

Tom has 3806.89%

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