Programme structure and control flow Flashcards

1
Q

What does the ‘or’ expression return?

A

x or y = x if x = True else y

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

What does the ‘and’ operator return?

A

x and y = y if x = True else x

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

What does the ‘not’ operator return?

A

not x = True if x = False else False

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

In python, an object is considered true unless either of 3 conditions hold, what are these?

A

bool(object) = False
object is empty
object has value 0

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

What would [] and 100 return?

A

[]

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

What would [] or 8 return?

A

8

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

In python, how would you iterate over the key-value pairs of a dictionary?

A

for key, value in dictionary.items():

… do stuff …

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

What is meant by variable unpacking?

A
  • (a, b, c) gives a, b, c

* *{a : 1, b : 2, c : 3} gives a=1, b=2, c=3

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

How would you return an exception in python?

A

try:
… code …
except Exception as e:
return e

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

How would you return an exception in python?

A

try-except blocks

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

Give an example of an Arithmetic error, an OS error, a Lookup error and a syntax error.

A
1 / 0
reading non-existent file
'string'[100000]
    for i in range(10):
print(i)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly