Hey guys learnt about many concepts ,, enjoying my journey ,, have fun while reading the article . By the way, this week I will not learn much but I will be brushing up my previous concepts and solve practice code. Eg: Birthday paradox, sort, matrix operation, Search
TIP : PRACTISE MULTIPLE TIME
Lists and their some functions
Lists are versatile data structures used to store multiple items in a single variable. Here's a brief overview along with some common functions:
Creating a List:
my_list = [1, 2, 3, 4, 5]
Accessing Elements: You can access elements of a list using index:
print(my_list[0]) # Output: 1
Slicing: Extracting a subset of a list:
print(my_list[1:3]) # Output: [2, 3]
Appending and Extending Lists:
my_list.append(6) # Appends 6 to the end of the list my_list.extend([7, 8]) # Extends the list by adding elements from another list
Inserting Elements:
my_list.insert(2, 'hello') # Inserts 'hello' at index 2
Removing Elements:
list. Remove() removes the same elements in order as in the given example below :
my_list.remove(3) # Removes the first occurrence of 3 del my_list[0] # Removes the element at index 0
Popping Elements:
popped_element = my_list.pop() # Removes and returns the last element
Finding Elements:
index = my_list.index(4) # Returns the index of the first occurrence of 4
Sorting and Reversing:
my_list.sort() # Sorts the list my_list.reverse() # Reverses the list
Length of a List:
length = len(my_list) # Returns the number of elements in the li
Creating a matrix
You can create a matrix (a list of lists) using the append()
function in Python. Here's an example of how you can create a 3x3 matrix:
matrix = []
# Appending rows to the matrix
matrix.append([1, 2, 3])
matrix.append([4, 5, 6])
matrix.append([7, 8, 9])
# Printing the matrix
for row in matrix:
print(row)
This code will create a 3x3 matrix and then print it out:
O/P: [[1, 2, 3] , [4, 5, 6] , [7, 8, 9]]
WHICH REPRESENTS :
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Each inner list represents a row in the matrix, and by appending these lists to the matrix
list, you effectively create a matrix. You can adjust the values and dimensions of the matrix as needed.
{{ How can we call any element of a list of list of list ..... list ?
Then we can call the specific element(a list or list of list or a specific element) by L[x][y][z]...[q] }}
A new convention :
for _i in range(100)
, it is a convention to use _
as the variable name when you want to indicate that the value of the loop index is not going to be used within the loop body. This is a way to make it clear to other developers reading the code that the loop index is not relevant in this specific case.
l=[]
from random import randint as r
for _i in range(100):
a=r(1,100)
l=l+[a]
print(l)
Birthday Paradox
The birthday paradox, also known as the birthday problem, states that in a random group of 23 people, there is about a 50 percent chance that two people have the same birthday
l=[] # list for storing all birthdays
from random import randint as r #selecting random people' birtrhday
for _i in range(23): #loop for 23 people
a=r(1,365) #random day between 1 and 365
l=l+[a] #adding the random day to the list
l.sort() #sorting the list
print(l)
i=0 #counter for the loop
flag=0 #flag for checking if there is a duplicate
while (i<len(l)-1): #loop for checking if there is a duplicate
if (l[i]==l[i+1]): #checking if there is a duplicate
print("Repetido",l[i],l[i+1]) #printing the duplicate
flag=1
i=i+1
if (flag==0): #if there is no duplicate
print("No hay repetidos") #printing that there is no duplicate
As we can se there is 50 percent chance of getting an 2 same birthday.
This can be also called simulation of real life experiment.
Sorting of List
syntax : l.sort() OR l.sort(reverse=True)
import random # importing random module
l=[] # empty list
for y in range (20):
l.append(random.randint(1,50)) # appending random numbers between 1 to 50
print('randomlist',l)
#sorting algorithm
x=[] #sorted list
while (len(l)>0):
min=l[0]
for i in range(len(l)) :
if l[i]<=min:
min=l[i]
x.append(min)
l.remove(min)
print("sorted list",x)
Dot Product
# code for dot-product of two vectors
from random import randint as r
x=[]
y=[]
for e in range (10) :
x.append(r(0,10))
y.append(r(0,10))
print(x)
print(y)
# now dot product of these 2 vectors
#dot product = x1*y1 + x2*y2 + x3*y3 + x4*y4 ......
if len(x)==len(y):
dot_product=0
for i in range(len(x)):
dot_product=dot_product+x[i]*y[i]
print(f' dotproduct of the vectors is : {dot_product}')
else:
print('the vectors are not of same dimension')
Matrix Addition
#matrix addition code
dim=3
r1=[1,2,3]
r2=[4,5,6]
r3=[7,8,9]
s1=[1,2,1]
s2=[6,2,3]
s3=[4,2,1]
a=[]
b=[]
a.append(r1)
a.append(r2)
a.append(r3)
b.append(s1)
b.append(s2)
b.append(s3)
print(a)
print(b)
c=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(dim):
for j in range(dim):
c[i][j]=a[i][j]+b[i][j]
print(c)
print(f' Matrix addition is {c}')
Matrix Multiplication
#matrix multiplication code
dim=3
r1=[1,2,3]
r2=[4,5,6]
r3=[7,8,9]
s1=[1,2,1]
s2=[6,2,3]
s3=[4,2,1]
a=[]
b=[]
a.append(r1)
a.append(r2)
a.append(r3)
b.append(s1)
b.append(s2)
b.append(s3)
print(a)
print(b)
c=[[0,0,0],[0,0,0],[0,0,0]]
# c[i][j] is dotproduct of ith row of a and jth column of b
for i in range(dim):
for j in range(dim):
for k in range(dim):
c[i][j]=c[i][j]+a[i][k]*b[k][j]
print(f" Matrix Multiplication of Matrix a and b = {c} ")
Using NumPy for calculations
In the vast realm of data science, machine learning, and scientific computing, there exists a cornerstone tool that has revolutionized the way we handle numerical data: NumPy. NumPy, short for Numerical Python, is an open-source library that provides support for large, multi-dimensional arrays and matrices, along with a plethora of high-level mathematical functions to operate on these arrays efficiently. Its simplicity, speed, and versatility have made it an indispensable asset for researchers, engineers, and data enthusiasts alike.
using same code as above code up to Matrix formation of a and b :
then-
As we conclude Week 4 of our Python journey, let's celebrate the strides we've made and the knowledge we've gained. From mastering the basics to delving into more advanced concepts, each step has brought us closer to fluency in this powerful language. As we look ahead, let's remain curious, persistent, and eager to explore all that Python has to offer. Here's to continued growth, discovery, and the exciting adventures that lie ahead in our Python experience.