Python Flashcards

1
Q

5*6 - 5 // 2 + 8 / 3

A

30.66666…

This is an example of bad code: it should be properly written as:

(5 * 6) - (5 // 2) + 8 / 3

= 30 - 2 + 2.66666…

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

What would print out if you ran this fragment of Java code:

System.out.println(3/2)

A

1

Java integer division differs from Python: the equivalent code (which is not equivalent!) in Python:

>>> print(3/2)

1.5

>>>

would print out 1.5

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

In Python, what is the value of:

2 << 2

A

8

The binary left shift operator will return a value which is double the value of the operand for each operation. So left shifting twice is the same thing as multiplying by 4. In general a << b == a*(2**b).

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

Why does the bit-wise 00shift operator << have the effect it does (ie, doubling the value of the operand each time?)

A

Because of binary representation; the << operator adds 0s to the right of the number and shifts everything to the left.

Eg:

1 << 1 == 2

1 is represented in Python as 00000001.

The operation shifts the 1 to the left and puts a zero into its place:

00000010

which is 2.

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

>>> x = 1
>>> x << 2
4
>>> x
?

A

x remains 1. The bit shift operators do not modify their operands.

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

What is the value of :

( (16 >> 3) == (17 >> 3))

A

True. Both 16 >> 3 and 17 >> 3 evaluate to 2.

>>> 16 >> 3
2
>>> 17 >> 1
8
>>> 17 >> 2
4
>>> 17 >> 3
2
>>> 16 >> 1
8
>>> 16 >> 2
4
>>> 16 >> 3
2

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

5 // (4 ** 1/2) ++ 56 % 7 % 2 % 2 % 2 % 2

A

2.0

>>> 56 % 7 % 2 % 2 % 2 % 2
0
>>> 56 % 7 % 2 % 2 % 2
0
>>> 56 % 7 % 2 % 2
0
>>> 56 % 7 % 2
0
>>> 56 % 7
0
>>>

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

>>> “Don’t Panic!” [7 :]

A

‘anic!’

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

“Foomping” [0 : 6] + “Breathe, Move” [9:]

A

>>> “Foomping” [0 : 6] + “Breathe, Move” [9:]
‘FoompiMove’

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

What do you get when you type:

range(6)

at the Python prompt?

A

>>> range(6)
range(0, 6)

The result of range(x) is a range object which operationally behaves like a list of numbers from 0 to x-1 but is not the same thing as a list because Python computes a value each time we ask range for a new value during for i in range(x) operations.

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

>>> list(range(9))[4:9]

A

>>> list(range(9))[4:9]
[4, 5, 6, 7, 8]

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

not (False and True and False and True or False)

A

True

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

>>> (False and True) or (True and False) and (“This is fun”.index(‘i’) == 5)

A

>>> (False and True) or (True and False) and (“This is fun”.index(‘i’) == 5)
False

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

0x00b & 0x555

A

>>> 0x00b & 0x555
1

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

0x00b & 0x555 | 0xfff

A

0x00b & 0x555 | 0xfff
4095
>>> Correctly parenthesized, it is (0x00b & 0x555 ) | 0xfff

answer is 0xfff, because AND takes precedence

17
Q

>>> 0x10 | 0b1000 & (0x100011111010101 >> 11)
16

A

>>> 0x10 | 0b1000 & (0x100011111010101 >> 11)
16

18
Q
def another\_mystery(n):
    x, y, z, w = n, 3, 2, 1
while x \> 0:
    x -= 1
    z += z - y + 2\*x
    w \*= 100

y += n

return y

another_mystery(10)

A

13