Python Material Chapter-3-2024
Python Material Chapter-3-2024
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
CHAPTER-3
Plotting Using PyLab
79
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Detail :-
PyLab is a module inside MATPLOTLIB library.
MATPLOTLIB was developed by John . D. Hunter in 2003.
MATPLOTLIB has it’s roots in MATLAB which need to decide PyLab.
MATLAB support many built – in function for users to develop the code.
It become easy for MATLAB user who don’t want to use import statement.
PyLab having number of functions and classes for generating the drawings.
MATPLOTLIB in python having 1000 lines of code to create quality graphics.
MATPLOTLIB :-
Example :-
80
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
81
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Detail :-
Mortgage is a simple calculation to find out or understand true cost of any
loan or interest.
To install mortgage you have to write following command at CMD prompt.
The above package can provide easy way to compare different mortgages.
It you are consider in mortgage loan , you should understand all the dtails
about how principal and interest will be calculated.
It will be also easy to find out monthly payment system for mortgage loan
which include following :
o HOA :- [ Home Owner Association fee ]
o PMI :- [Private Mortgage Insurance fee ]
o Home Owner Insurance
o Taxes
Here are some common tricks to find out mortgage loan calculation :-
1. Provide sales price of house.
2. Enter down payment.
82
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
sr.
no
. is a simple calculation
1 to find Mortgage
out or understand true cost of any loan or
interest.
To install mortgage you have to write
Pip install mortgage
command.
83
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Detail :-
One of the most common type of math based technical challenges are ones
that deal with Fibonacci sequence.
Each new term in the Fibonacci sequence is generated by adding the
previous two terms.
For example , starting with 1 and 2 ,the first 10 numbers in the sequence
would be :
o 1,2,3,5,8,13,21,34,55,89
One of the favourite challenge that deals with Fibonacci sequence is one that
asks for index value of some high number in the sequence.
It might be good idea to record the value returned by the first call , and then
look it up rather than compute it each time it is needed.This is called
“memorization”.
“Memorization” is key idea behind any dynamic programming.
Normally , easy way to go about doing something like would be to put all
the numbers in array and then cycle them with for loop.
First it requires two different functions , one function to generate Fibonacci
sequence and second function to cycle through all the numbers we have
generated.
Let’s see ,implementation of Fibonacci sequence by following figure.
84
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot – 360001
As above figure , look at tree of calls associated with the invocation fib(6).
Notice that we are computing same values over & over again.
oExample :-
Def
fib_seq(n)
A=0
B=1
If n==1:
Print(a)
Elif n==2:
Print(a,b)
Else:
Print(a,b,end=” “)
For I in range(n-2):
c=a+b
a=b
b=c
print(c , end=” “)
#calling
fib_seq(10)
85
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
Detail :-
In 0-1 knapsack , items can not be broken.
It means if 1 than cover all the elements and if 0 than leave all the
elements.
This is the main reason behind calling it as 0-1 knapsack.
In case of 0-1 knapsack , the value of xi can be either 0 or 1 where other
elements remains same.
Let’s consider the capacity of the knapsack is w=25 and the items as shown
in the following table.
86
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
ITEM A B C D
PROFIT 24 18 18 10
WEIGHT 24 10 10 7
Here , profit per unit weight = (pi/wi).
First item A will be selected as it will contribute maximum profit among
all the elements.
After selecting item A , no more item will be selected.
Here , for this given set of items total profit is 24.
The optimal solution can be achieved by selecting items , B and C , where
total profit is 18+18=36.
In this example , the items are selected based on ratio (pi/wi).
Let’s us consider capacity of knapsack is w=60 and the items are as shown
in following table.
ITEM A B C
PRICE 100 280 120
WEIG 10 40 20
HT
RATIO 10 7 8
87
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
o CONQUER(Solve) :-
It include process of recursively solve the sub – problems.
This step receives a lot of smaller sub – problem to be solved.
Generally , at this level , the problems are considered as
“solved”.
o COMBINE (Merge):-
It combines the appropriate answers as well as results.
When smaller sub – problems are solved , this stage recursively
combines them until they found solution of original problem.
89
SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.
(AFFILIATED TO SAURASHTRA UNIVERSITY)
2 – Vaishalinagar 3 – Vaishalinagar
Nr. Amrapali Under Bridge Nr. Amrapali Under Bridge
Raiya Road Raiya Road
Rajkot – 360001 Rajkot - 360001
o Example :-
def bsearch(list ,val):
list_size = len(list) -1
idx0 = 0
idxn = list_size #find middle most value
While idx0 < = idxn :
Midval = (idx0 _ idxn)/2
If list[midval] == val:
Return midval
#compare value – middle most value
If val > list[midval]:
Idx0 = midval +1
Else:
Idxn = midval – 1
If idx0 > idxn:
Return none
#calling
l1 = [55,44,2,4,9,8]
Print (bsearch(l1,4)
A classic example of divide & conquer is merge sort which demonstrated
below.
In merge sort , we divide array into two halves & sort the two halves
recursively & then finally merge the sored halves.
90