• Methods Of Dispersion

    Methods Of Dispersion

    Let’s learn to calculate the spread of the data and measure it. with Absolute measures and Relative measures


Efficient Python 1: Play with Numpy, loops, Lists, Arrays

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_Array1
  

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_Array2
  

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_Array1
  

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_Array2
  

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

Creating_Identity_Matrix_1
  

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

Creating_Identity_Matrix_2
  

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_1
  

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_2
  

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_1
  

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_2
  

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]

Efficient python 2

This is the second segment of Efficient python : From Long and Basic to Short

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

, ,
  • Efficient Python 1: Play with Numpy, loops, Lists, Arrays

    solve these Efficient python code quizzes

Instagram
WhatsApp
error: Content is protected !!