Python 3 Programming Questions
- A Python dev must verify if key
xis stored in a dictionarymy_dict. Ifxis indict, set valuefoundtoTrue; otherwise, setfoundtoFalse. Which of the following code snippets will achieve this?
- Options:
found = my_dict.has_key(x)- :heavy_check_mark:
found = x in my_dict found = my_dict.contains(x)found = x.is_elem(my_dict)- :heavy_check_mark:
found = True try: y = my_dict[x] except KeyError as e: found = False
- Which of the following lines python of code generate the same “result” value as the following Python code snippnet
Code Snippet:
data = [1,2,3,4,5,6]
def f1(x):
return 3*x
def f2(x):
try:
return x > 3
except:
return 0
result = list(map(f1, filter(f2, data)))
Note:
filter()takes 2 arguments, a function and an iterable (e.g. list)
The function passed tofilter()should return a boolean value (True/False)
filter(func, iter) --> keep items if bool is True
- Options:
- :heavy_check_mark:
result = [3*i for i in data if i > 3] result = [3*i for i in data(i) if i > 3]- :heavy_check_mark:
result = list(map(lambda r: 3*r, filter(f2,data))) result = list(map(lambda r: 3*r, filter(lambda f: f>3 or 0, data)))result = [3*i for i in data(i) if i > 3 else 0]
- :heavy_check_mark: