Programme structure and control flow Flashcards
What does the ‘or’ expression return?
x or y = x if x = True else y
What does the ‘and’ operator return?
x and y = y if x = True else x
What does the ‘not’ operator return?
not x = True if x = False else False
In python, an object is considered true unless either of 3 conditions hold, what are these?
bool(object) = False
object is empty
object has value 0
What would [] and 100 return?
[]
What would [] or 8 return?
8
In python, how would you iterate over the key-value pairs of a dictionary?
for key, value in dictionary.items():
… do stuff …
What is meant by variable unpacking?
- (a, b, c) gives a, b, c
* *{a : 1, b : 2, c : 3} gives a=1, b=2, c=3
How would you return an exception in python?
try:
… code …
except Exception as e:
return e
How would you return an exception in python?
try-except blocks
Give an example of an Arithmetic error, an OS error, a Lookup error and a syntax error.
1 / 0 reading non-existent file 'string'[100000] for i in range(10): print(i)