Predict the output of the following code blocks? Run them in Colab to check if your predictions were correct?
Note: You might have to look on the internet for the meaning of some symbols (such as /
and %
)
7 / 3 == 7 / 3.0
True
'nesia' in 'Indonesia'
True
(2 + 3) == 5
True
2 + 3 == 5
True
"2" + "3" == "5"
False
11 % 3
2
1 < 2 and 11 >= 9
True
2 > 5 or (1 < 2 and 11 >= 9)
True
Modify the above code to print ‘Hello Hello Hello Hello Batman’ when the name is ‘Batman.’
name = 'Batman'
if name == 'Batman':
print('Hello '*5 + 'Batman!')
else:
print('Hello World!')
Hello Hello Hello Hello Hello Batman!
Modify the code to respond to Robin instead of Batman.
name = 'Robin'
if name == 'Robin':
print('Hello '*5 + 'Robin!')
else:
print('Hello World!')
Hello Hello Hello Hello Hello Robin!
The following code has a few bugs; fix them!
import math
x,y = 3,4
z = math.sqrt(x**2 + y**2)
print(x,y,z)
3 4 5.0
import numpy as np
my_np_list = np.array([182, 269, 341, 410, 523,
697, 728, 856, 948, 101,
1120, 1272, 1312, 1460, 1564,
1642, 1717, 1816, 1923, 2087])
my_np_list[3]
410
my_np_list[-1]
2087
my_np_list[:3]
array([182, 269, 341])
my_np_list[3:10]
array([410, 523, 697, 728, 856, 948, 101])
my_np_list[my_np_list > 500]
array([ 523, 697, 728, 856, 948, 1120, 1272, 1312, 1460, 1564, 1642, 1717, 1816, 1923, 2087])
max()
, min()
, mean()
and std()
to find the maximum, minimum, mean and standard deviation of the values of the numpy array.my_np_list.max()
2087
my_np_list.min()
101
my_np_list.mean()
1048.4
my_np_list.std()
612.5768849703685
my_np_list[my_np_list < 500] = 0
my_np_list # Did it work?
array([ 0, 0, 0, 0, 523, 697, 728, 856, 948, 0, 1120, 1272, 1312, 1460, 1564, 1642, 1717, 1816, 1923, 2087])
Note
and
' how arrays are setup internally! You need to use '&
'.or
' the symbol will be '|
' so that:
(my_np_list > 500) | (my_np_list < 1000)
my_np_list[(my_np_list > 500) & (my_np_list < 1000)]
array([523, 697, 728, 856, 948])