solve these Efficient python code quizzes
Efficient Python 1: Play with Numpy, loops, Lists, Arrays

Solve these quizzes and learn how to make your python codes concise and efficient
1. Sum of arrays
Simple Code
Computing sum of an array with for loop
num
numbers
total
= [1, 2, 3, 4, 5]
total = 0
for num in numbers:
total +=
print( )
Output:
15
Advanced Code
Computing sum of an array with sum()
[1, 2, 3, 4, 5]
sum
total
numbers =
= (numbers)
print(total)
Output:
15
2. Mean of arrays
Simple Code
Computing mean of an array with for loop
len(numbers)
mean
+=
numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
total num
mean = total /
print( )
Output:
3
Advanced Code
Computing mean of an array with mean function
numpy
numbers
np.mean
print
import as np
= [1, 2, 3, 4, 5]
mean = (numbers)
(mean)
Output:
3
3. Create Identity Matrix
Simple Code
Create identity matrix with for loops
row
j
I
range
i == j
append(1)
append(0)
n = 4
identity_matrix = []
for in (n):
= []
for in range(n):
if :
row.
else:
row.
identity_matrix.append(row)
print(row)
Output:
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]
Advanced Code
Create identity matrix with numpy library
np.eye
identity_matrix
import numpy as np
n = 4
= (n)
print(identity_matrix)
Output:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
4. Remove Duplicates
Simple Code
Remove duplicates from list using for loop
numbers
[1, 2, 2, 3, 4, 4, 5]
num
numbers =
unique_numbers = []
for num in :
if not in unique_numbers:
unique_numbers.append(num)
Output:
[1, 2, 3, 4, 5]
Advanced Code
Remove duplicates from list using set
set
numbers
list
[1, 2, 2, 3, 4, 4, 5]
numbers =
unique_numbers = ( ( ))
Output:
[1, 2, 3, 4, 5]
5. Filter Even Numbers
Simple Code
Filtering_Even_Numbers from a List
numbers
% 2
append
= [1, 2, 3, 4, 5]
even_numbers = []
for num in numbers:
if num == 0:
even_numbers. (num)
Output:
[2, 4]
Advanced Code
Filtering_Even_Numbers from a List
num
[1, 2, 3, 4, 5]
% 2
numbers =
even_numbers = [num for in numbers if num == 0]
Output:
[2, 4]
Manova Quiz
Solve this quiz for testing Manova Basics
Efficient Python 1: Play with Numpy, loops, Lists, Arrays
solve these Efficient python code quizzes