Matplotlib is the open source data science library of python which is used mainly for the visualization of data. The Pyplot package in the matplot library is helpful in plotting the 2-D figure. There are multiple type pf plot or graphs are present in matplotlib which is helpful in analysis of huge data by visualization like Line plot, bar plot, scatter plot, pie plot, histogram, Box plot.
Learn Basic Concepts of Machine Learning and Azure
Monday, 31 January 2022
MatplotLib: Data Science Library for Data Visualization
Pandas Practice: Data Science Library in Python
Pandas, an open source data science library of python which is used mainly for the analyses, cleaning and manipulation of data. Pandas allow to load the data, clean and manipulate the complex big data, and based on this, analysis is done on clean data and make conclusions. Pandas library is very fast and give very high performance on large datasets also.
Python provide Series (1-D array) and Dataframe (2-D array) in manipulating the data. The data can be loaded in both Series and Dataframe from array, list, dictionary, tuple, CSV files, excel files, SQL database..
Below are the set of multiple questions which will help to give the basic knowledge of all type of programming of Pandas Data science library in Python. You can visit website https://https://www.w3resource.com/python-exercises/pandas/ or https://www.geeksforgeeks.org/python-pandas-practice-exercises-questions-and-solutions/
Steps to run below programs:
- Install Anaconda in your PC/ Laptop.
- Open Spyder.
- Copy and paste below programs.
- Press Run button. You can see the result in Console section at right side.
Wednesday, 19 January 2022
Python Exercise on loops, Function and some common python programs
Below are the set of multiple questions which will help to give the basic knowledge of the loops in python and how to display the different patterns using python. This section also contain some basic common python programs along with some question on Functions.
Steps to run below programs:
- Install Anaconda in your PC/ Laptop.
- Open Spyder.
- Copy and paste below programs.
- Press Run button. You can see the result in Console section at right side.
- Result of all the below program is given in the last 'Result' Section.
#1. Program to calculate the sum of all numbers from 1 to given number by user.
num = int(input("Enter Number to calculate sum :"))
sum=0
for i in range (1, num+1):
sum = sum +i
print("Sum of the all numbers :", sum)
#2. Program to count total digits in a given number
digit_num = 856938
count=0
while (digit_num != 0):
digit_num = digit_num // 10
count = count +1
print ("count of all digits in given number :", count)
#3. Program to show numbers from -5 to -1
print("Numbers from -5 to -1 are :")
for i in range(-5, 0, 1):
print(i, end=' ')
print (" ")
#4. Program of Fibonacci series upto 10 when first two numbers are 0 and 1
n1=0
n2=1
print("Fibonacci series upto 10 numbers are :")
for i in range(10):
print (n1, end =' ')
n3 = n1 + n2
n1 = n2
n2 = n3
print(" ")
#5. Program to display all prime numbers upto 2 to 20 numbers
#Note: all prime numubers are greater than 1
print ("All prime numbers upto 2 to 20 numbers are :")
for num in range(2, 21):
if (num>1):
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num, end =' ')
print(" ")
#6. Program to reverse the given number and to check if number is palindrome
#Note: A palindrome number is a number that is same after reverse like 1221
Given_Num = 589371
Reverse_num = 0
while (Given_Num> 0):
rem = Given_Num % 10
Reverse_num = (Reverse_num * 10) + rem
Given_Num = Given_Num // 10
print("Reversed Number of given num are :", Reverse_num)
if Given_Num == Reverse_num:
print ("Number is palindrome")
else:
print ("Number is not palindrome")
#7. Program to find any exponent value
def Exponent(base, exp):
pow = exp
res =1
while (pow > 0):
res = res*base
pow =pow-1
print("Exponent, base raise to the power is :", res)
Exponent(20,3)
#8. Program to find factorial of given number
number = 10
fact =1
if number<0:
print("No Factorial for negative number")
elif number == 0:
print("Factorial of zero is 1")
else:
for i in range(number+1):
fact = fact * i
print ("The factorial of given number is :", fact)
#9. Program to display Multiplication table from 1 to 10
for i in range(1, 11):
for j in range(1,11):
print(i*j, end=' ')
print(" ")
print(' ')
#10. Program to check if given year is leap year
def CheckLeapYear(Year):
if((Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0)):
print("Given year :", Year, " is leap Year");
else:
print ("Year :", Year, " is not a leap Year")
CheckLeapYear(2012)
CheckLeapYear(2002)
CheckLeapYear(2020)
#11. Loop1 : Number pattern
print("Solution of Loop1 : Number pattern : ")
for i in range(1,6):
for j in range(i):
print(i, end=' ')
print(" ")
#12. Loop2 : Right angled triangle star pattern with asterisk(*)
print("Solution of Loop2 : Right angled triangle star pattern with asterisk(*) : ")
for i in range(5,0,-1):
for j in range(i):
print("*", end=' ')
print(" ")
#13. Loop3 : different Number pattern
print("Solution of Loop3 : different Number pattern : ")
for i in range(1,6):
for j in range(1,i+1):
print(j, end=' ')
print(" ")
#14. Loop4 : Half Pyramid pattern with number
print("Solution of Loop4 : Half Pyramid pattern with number : ")
for i in range(5,0,-1):
for j in range(i,0,-1):
print(j, end=' ')
print(" ")
#15. Loop5: Pyramid star Pattern with asterisk(*)
print("Solution of Loop5: Pyramid star Pattern with asterisk(*) : ")
rows1 =6
rows2 =4
for i in range(1, rows1):
for j in range(rows1-i-1):
print(' ', end='')
for k in range(0, 2 * i - 1):
print('*', end = '')
print(" ")
#16. Loop6: Inverted Pyramid star pattern with asterisk(*)
print("Solution of Loop6: Inverted Pyramid star Pattern with asterisk(*) : ")
rwo = 5
for i in range(rwo,0,-1):
for j in range(rwo-i+1):
print(' ', end='')
for k in range(0,2*i -1):
print('*', end = '')
print(" ")
#17. Loop7 : Half diamond pattern with asterisk(*)
print("Solution of Loop7: Half diamond pattern with asterisk(*) : ")
for i in range(1,5):
for j in range(i):
print("*", end='')
print(" ")
for i in range(5,0,-1):
for j in range(i):
print("*", end='')
print(" ")
#18. Loop8 : Mirror Half diamond pattern with asterisk(*)
print("Solution of Loop8: Mirror Half diamond pattern with asterisk(*) : ")
rows =6
for i in range(1,rows):
for j in range(rows-1-i):
print(' ', end='')
for k in range(i):
print("*", end='')
print(" ")
for i in range(rows,0,-1):
for j in range(rows-i+1):
print(' ', end='')
for l in range(i-2):
print("*",end='')
print(" ")
#19. Loop9 : Diamond Star pattern with asterisk(*)
print("Solution of Loop9: Diamond Star pattern with asterisk(*) : ")
rows1 =6
rows2 =4
for i in range(1, rows1):
for j in range(rows1-i-1):
print(' ', end='')
for k in range(0, 2 * i - 1):
print('*', end = '')
print(" ")
for i in range(rows2,0,-1):
for j in range(rows2-i+1):
print(' ', end='')
for k in range(0,2*i -1):
print('*', end = '')
print(" ")
#20. Loop10 : Rectangle Star pattern
print("Solution of Loop10: Rectangle Star pattern : ")
for i in range(5):
for j in range(12):
print("*", end=' ')
print(" ")
#21. Loop11 : Square Star pattern
print("Solution of Loop11: Square Star pattern : ")
for i in range(5):
for j in range(5):
print("*", end=' ')
print(" ")
#22. Loop12 : Rhombus Star pattern
print("Solution of Loop12: Rhombus Star pattern : ")
for i in range(rows, 0, -1):
for j in range(1, i):
print(' ', end = '')
for k in range(0, rows):
print('*', end = '')
print(" ")
#23. Loop13: Triangle Number pattern
print("Solution of Loop13: Triangle Number pattern : ")
for i in range(1, 6):
for j in range(6-i-1):
print(' ', end='')
for k in range(0, 2 * i - 1):
print(i, end = '')
print(" ")
#24. Create function 'Employee' with two argument, Emp_name and salary
#Declaration of function
def Employee(Emp_name, salary):
print("Function Declaration :", Emp_name, salary)
#Calling the function
Employee("Akansha",30000)
#25. Create function Employee calling multiple arguments at a time using one function
def Employee(*args):
for i in args:
print("Calling multiple arguments :", i)
Employee("Akansha","Isha")
Employee("Isha", "Akansha", "Sakshi")
#26. Create a function 'Employee' with salary as default argument and it can be changed also after calling the function
def Employee(Emp_name, salary=25000):
print(Emp_name, salary)
Employee("Akansha")
Employee("Isha", 20000)
#27. Change the name of function 'Employee' to 'Employee_Details
def Employee(Emp_name, salary):
print(Emp_name, salary)
#Assigning different function name
Employee_Details = Employee
Employee("Akansha",30000)
#28. Find the Factorial of given number using recursion function
def Factorial_Recursion(num):
if num == 1:
return num
else:
return num*Factorial_Recursion(num-1)
Given_num = int(input("Enter factorial number :"))
if Given_num < 0:
print("No factorial for negative integers")
elif Given_num == 0:
print("Factorial of zero is 1")
else:
print(("Factorial of given number is :", Factorial_Recursion(Given_num)))
Result of above Exercise
Enter Number to calculate sum :5
Sum of the all numbers : 15
count of all digits in given number : 6
Numbers from -5 to -1 are :
-5 -4 -3 -2 -1
Fibonacci series upto 10 numbers are :
0 1 1 2 3 5 8 13 21 34
All prime numbers upto 2 to 20 numbers are :
2 3 5 7 11 13 17 19
Reversed Number of given num are : 173985
Number is not palindrome
Exponent, base raise to the power is : 8000
The factorial of given number is : 0
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Given year : 2012 is leap Year
Year : 2002 is not a leap Year
Given year : 2020 is leap Year
Solution of Loop1 : Number pattern :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Solution of Loop2 : Right angled triangle star pattern with asterisk(*) :
* * * * *
* * * *
* * *
* *
*
Solution of Loop3 : different Number pattern :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Solution of Loop4 : Half Pyramid pattern with number :
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Solution of Loop5: Pyramid star Pattern with asterisk(*) :
*
***
*****
*******
*********
Solution of Loop6: Inverted Pyramid star Pattern with asterisk(*) :
*********
*******
*****
***
*
Solution of Loop7: Half diamond pattern with asterisk(*) :
*
**
***
****
*****
****
***
**
*
Solution of Loop8: Mirror Half diamond pattern with asterisk(*) :
*
**
***
****
*****
****
***
**
*
Solution of Loop9: Diamond Star pattern with asterisk(*) :
*
***
*****
*******
*********
*******
*****
***
*
Solution of Loop10: Rectangle Star pattern :
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
Solution of Loop11: Square Star pattern :
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Solution of Loop12: Rhombus Star pattern :
******
******
******
******
******
******
Solution of Loop13: Triangle Number pattern :
1
222
33333
4444444
555555555
Function Declaration : Akansha 30000
Calling multiple arguments : Akansha
Calling multiple arguments : Isha
Calling multiple arguments : Isha
Calling multiple arguments : Akansha
Calling multiple arguments : Sakshi
Akansha 25000
Isha 20000
Akansha 30000
Enter factorial number :5
('Factorial of given number is :', 120)
Friday, 14 January 2022
Numpy Exercise by Python
Numpy is Numerical Python, an open source data science library of python which is used mainly for the arrays, matrices calculation. This library provides lot of supporting functions which make the complex mathematical calculation of arrays very easy. Numpy are often used with other python libraries like pandas, scipy and matplotlib to solve complex problem in short span of time and show the result accurately.
Below are the set of multiple questions which will help to give the basic knowledge of all type of programming of Numpy Data science library in Python. You can visit website https://www.w3resource.com/python-exercises/numpy/ orhttps://www.geeksforgeeks.org/python-numpy-practice-exercises-questions-and-solutions/
Steps to run below programs:
- Install Anaconda in your PC/ Laptop.
- Open Spyder.
- Copy and paste below programs.
- Press Run button. You can see the result in Console section at right side.
#1. Declaration of Numpy , create arrays of zeros, ones, fifties
import numpy as np
arr = np.zeros(5)
print("Zeros array in numpy are:", arr)
arr_z= np.zeros(shape=(2,3), dtype='int')
print("Zeros array with diff shape in numpy are:",arr_z)
arr1 = np.ones(5)
print("Ones array in numpy are:",arr1)
arr2 = np.ones(5)*50
print("50th value array in numpy are:", arr2)
#2. creation of random numbers b/w 0 and 1
rand= np.random.normal(0,1,1)
print("Random values from 0 to 1 are: ", rand)
rand1 = np.random.normal(0,1,25)
print("25 Random values from 0 to 1 are: ",rand1)
#3. create a vector with values ranging from 10 to 19, with reshaping
vec= np.arange(10,19)
print("Vector with values from 10 to 19 are: ", vec)
vec_rev= vec[::-1]
print("Reversing of vector: ", vec_rev)
vec1 = vec.reshape(3,3)
print("Reshaping of Vector \n: ", vec1)
print ('\n')
#4. creation of identity matrix
Iden= np.eye(3)
print("Identity Matrix is \n:", Iden)
print ('\n')
#5. create 3*3 matrix with values ranging from 2 to 10
mat = np.arange(2, 11).reshape(3,3)
print("3*3 matrix with values ranging from 2 to 10 \n:", (mat))
print ('\n')
#6. Write a NumPy program to convert a list and tuple into arrays.
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("List to array: ")
print(np.asarray(my_list))
my_tuple = ([8, 4, 6], [1, 2, 3])
print("Tuple to array: ")
print(np.asarray(my_tuple))
#7. Write a NumPy program to append values to the end of an array.
import numpy as np
x = [10, 20, 30]
print("Original array:")
print(x)
x = np.append(x, [[40, 50, 60], [70, 80, 90]])
print("After append values to the end of the array:")
print(x)
#8. Write a NumPy program to construct an array by repeating.
#Sample array: [1, 2, 3, 4]
a = [1, 2, 3, 4]
print("Original array:")
print(a)
print("2 times repeating the same array:")
x = np.tile(a, 2)
print(x)
print("3 times repeating the same array:")
x = np.tile(a, 3)
print(x)
#9. Write a NumPy program to change the dimension of an array.
x = np.array([1, 2, 3, 4, 5, 6])
print("Shape of array is: ", x.shape)
print("Size of array is: ", x.size)
y = np.array([[1, 5, 2],[6, 7, 1],[3,8,4]])
print("Make array of 3 rows and 3 colums \n :",y)
print ('\n')
x = np.array([5,3,11,4,5,9,7,25,19])
x.shape = (3, 3)
print("Change shape of given array into 3 rows and 3 columns \n: ", x)
print ('\n')
#10. Mathematical practice for Numpy
print("Addition: ", np.add(55, 30))
print("Subtraction: ", np.subtract(55, 30))
print("Multiplication :", np.multiply(55, 30))
print("Division :", np.divide(55,30))
expo = np.arange(5)
print("Originial term are: ", expo)
expo_result = np.power(expo,3)
print("Originial term are: ", expo_result)
abs =np.array([-2.3, 6.8, -9.1])
abs_result = np.absolute(abs)
print("Absolute Value are: ", abs_result)
#11. Round array elements of given decimals to nearest value
round_element = np.round([1.6, 2.4, 0.34, 7.8, 8.1])
print("Round Values of given array are: ",round_element)
#12. find max and min value of array
arr_max_min = np.arange(6).reshape((2,3))
print(arr_max_min)
print("Max value of array \n: ",(np.amax(arr_max_min)))
print ('\n')
print("Min value of array \n: ", (np.amin(arr_max_min)))
print ('\n')
print("Sum of all the elements \n: ", (arr_max_min.sum()))
print ('\n')
#13. Matrix addition and multiplication of two arrays
mat1 = [[4, 5], [2,5], [6,2]]
mat2 = [[5,7,1], [6,9,3]]
print("Matrix multiplication \n: ", (np.matmul(mat1,mat2)))
print ('\n')
#14. Another method of addition, multiplication, Division, Concatenation (Vertical stacking and horizontal stacking)
mat1_np = np.array([[4, 5], [2,5]])
mat2_np = np.array([[5,7], [6,9]])
print("Another method of Matrix addition \n: ", mat1_np + mat2_np)
print ('\n')
print("Another method of Matrix multiplication \n: ", mat1_np * mat2_np)
print ('\n')
print("Another method of Matrix Division \n: ", mat1_np / mat2_np)
print ('\n')
print("Concatenation, Vertical stacking: \n :", np.vstack((mat1_np,mat2_np)));
print ('\n')
print("Concatenation, horizontally stacking \n :", np.hstack((mat1_np,mat2_np)));
print ('\n')
#15. calculate inverse sine value (trignometric function)
sine_value = np.array([-1., 0, 1.])
print("Inverse Sine Value: ", np.arcsin(sine_value))
print ('\n')
#16. Array Iteration
mat_iter = np.array([[5,7], [6,9]])
print("Array before Iterating: /n", mat_iter)
print("Array after Iterating: /n")
for x in np.nditer(mat_iter):
print(x, end=' ')
print ('\n')
#17. Transpose of Array
mat_Trans = np.array([[5,7], [6,9]])
print("Transpose of array: \n", mat_Trans.T)
print ('\n')
#18. Find mean, median, average of array
m = np.array([[4,7,5],[6,2,8],[3,1,7]])
print("Original array: \n", m)
print('\n')
print("Mean of array along axis 0 :", np.mean(m,0))
print("Median of array along axis 1 :", np.median(m,0))
print("Average of array along axis 1 :", np.average(m,1))
print("\n")
Result of above Exercise
Zeros array in numpy are: [0. 0. 0. 0. 0.]
Zeros array with diff shape in numpy are: [[0 0 0]
[0 0 0]]
Ones array in numpy are: [1. 1. 1. 1. 1.]
50th value array in numpy are: [50. 50. 50. 50. 50.]
Random values from 0 to 1 are: [0.1241432]
25 Random values from 0 to 1 are: [ 0.62204881 -0.82931417 0.23603654 -1.29256341 -1.68773158 0.09786349
-0.63253742 0.64259035 1.60333346 0.27708779 -0.36226831 0.094398
-1.31297426 0.29043894 0.37204664 0.06414133 -0.96859801 -0.59008381
0.19558017 -0.13458364 0.03206455 -0.89067194 -0.56143579 0.19745269
0.04995617]
Vector with values from 10 to 19 are: [10 11 12 13 14 15 16 17 18]
Reversing of vector: [18 17 16 15 14 13 12 11 10]
Reshaping of Vector
: [[10 11 12]
[13 14 15]
[16 17 18]]
Identity Matrix is
: [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
3*3 matrix with values ranging from 2 to 10
: [[ 2 3 4]
[ 5 6 7]
[ 8 9 10]]
List to array:
[1 2 3 4 5 6 7 8]
Tuple to array:
[[8 4 6]
[1 2 3]]
Original array:
[10, 20, 30]
After append values to the end of the array:
[10 20 30 40 50 60 70 80 90]
Original array:
[1, 2, 3, 4]
2 times repeating the same array:
[1 2 3 4 1 2 3 4]
3 times repeating the same array:
[1 2 3 4 1 2 3 4 1 2 3 4]
Shape of array is: (6,)
Size of array is: 6
Make array of 3 rows and 3 colums
: [[1 5 2]
[6 7 1]
[3 8 4]]
Change shape of given array into 3 rows and 3 columns
: [[ 5 3 11]
[ 4 5 9]
[ 7 25 19]]
Addition: 85
Subtraction: 25
Multiplication : 1650
Division : 1.8333333333333333
Originial term are: [0 1 2 3 4]
Originial term are: [ 0 1 8 27 64]
Absolute Value are: [2.3 6.8 9.1]
Round Values of given array are: [2. 2. 0. 8. 8.]
[[0 1 2]
[3 4 5]]
Max value of array
: 5
Min value of array
: 0
Sum of all the elements
: 15
Matrix multiplication
: [[50 73 19]
[40 59 17]
[42 60 12]]
Another method of Matrix addition
: [[ 9 12]
[ 8 14]]
Another method of Matrix multiplication
: [[20 35]
[12 45]]
Another method of Matrix Division
: [[0.8 0.71428571]
[0.33333333 0.55555556]]
Concatenation, Vertical stacking:
: [[4 5]
[2 5]
[5 7]
[6 9]]
Concatenation, horizontally stacking
: [[4 5 5 7]
[2 5 6 9]]
Inverse Sine Value: [-1.57079633 0. 1.57079633]
Array before Iterating: /n [[5 7]
[6 9]]
Array after Iterating: /n
5 7 6 9
Transpose of array:
[[5 6]
[7 9]]
Original array:
[[4 7 5]
[6 2 8]
[3 1 7]]
Mean of array along axis 0 : [4.33333333 3.33333333 6.66666667]
Median of array along axis 1 : [4. 2. 7.]
Average of array along axis 1 : [5.33333333 5.33333333 3.66666667]
Sunday, 9 January 2022
Object Oriented Programming (Classes and Inheritance) Exercise by python
Below are the set of 20 questions which will help to give the basic knowledge of all type of programming on classes and Inheritance in Python. You can visit website pynative.com or https://www.geeksforgeeks.org/.
Steps to run below programs:
- Install Anaconda in your PC/ Laptop.
- Open Spyder.
- Copy and paste below programs.
- Press Run button. You can see the result in Console section at right side.
#1. Program to create a class Employee without any variables and methods
#Answer:
#Solution:
class Employee:
pass
#2. Program to create child class Animal that will inherit all variables and methods of Bird class
#Answer: Parrot 10
#Solution:
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
class Bird(Animal):
pass
bird_detail = Bird("Parrot", 10)
print(bird_detail.name, bird_detail.age)
#3. Program to determine the type of class, and also if child class is an instance of Parent class
#Answer: Type of class: <class '__main__.Birds'> , Instance of class: True
#Solution:
class Animals:
def __init__(self, name, age):
self.name = name
self.age = age
class Birds(Animals):
pass
bird_details = Birds("Parrot", 10)
print("Type of class: ", type(bird_details))
print("Instance of class: ", isinstance(bird_details, Animals))
#4. Program of Object Oriented Programming, inheritance with default value of variables
#Answer: The color of animal Parrot is white passengers
#Solution:
class Animals:
def __init__ (self, name, age):
self.name = name
self.age = age
def animal_color (self, color):
return f"The color of animal {self.name} is {color} passengers"
class Birds(Animals):
def animal_color(self, color="white"):
return super().animal_color(color="white")
animal_check = Birds("Parrot", 10)
print (animal_check.animal_color())
#5. Write a program with the property that has same value for every object
#Answer: Parrot 10 Black , Snake 8 Black
#Solution:
class Animal1:
color = "Black"
def __init__ (self, name, age):
self.name = name
self.age = age
class Bird1(Animal1):
pass
class Reptiles(Animal1):
pass
animal_check = Bird1("Parrot", 10)
print (animal_check.name, animal_check.age, animal_check.color)
animal_check = Reptiles("Snake", 8)
print (animal_check.name, animal_check.age, animal_check.color)