Adobe Scan Feb 03, 2023
Adobe Scan Feb 03, 2023
Introduction to PYTHON
https://drive.google.com/file/d/1gVG2I
share_link
J8BIjh YDwDx6j WJns59h9dGOGVi/view?usp=
II. Programming Structures
Conditional structure
What is
conditioning in Python?
Based on certain conditions, the flow of execution of the
using proper syntax. program is determined
.Often called
decision-making statements in Python.
How to if
use
conditions?
if
statement-for implementing one-way branching
-
if. .else
statements-for implementing two-way
-
branching
nested if
statementsfor implementing multiple branching
if-elif ladder - for
implementing multiple branching
#Syntax:
Condition:
statements
Enter an integer: 5
Entered value is positive
# Synatx:
# if condition :
#
statements 1
# else:
Statements 2
# If condition is True -
statements 1 will be executed
#othervise statements 2 will be executed
ontered e ")
ve"
n e g a t i
ber
2nt("Num 18
entered
p
el8e N u m b e r
PTInt
("
-5
Enter an integer.
negat1
ve
is
e n t e r e d
N u m b e r
Synt ax
# 1 f condition
statements I
#elif condition
s t a t e m e n t s
#elif condition 3
statements 3
e x e c u t e d .
# else
statements 4 Wlll
be
1 be
b e executed
executs
and s
2w i l l
S t a t e m e n t s
Wlll
True S t a t e m e n t s
1 is
#If condition 2 is
True
staTementS in else
condition
* else if then
True
not
c o n d i t i o n s
is e x e c u t e d .
of the
any
I
# Example : D t a l n e d
by a studen.
percentage
or mar KS
the
perc=fl oat (input (" Enter
Enter a number:45
The given number 1s not
divisible by 7
Department of Mathematics,
SJBIT
# Conversion Celsius to Fahrenheit and vice -versa :
def print_menu () :
1. Celsius to
Fahrenheit
2. Fahrenheit to Celsius
Which conversion would
you 1like: 1
Enter Temperature in Celsius:
34
Temperature in Fahrenheit: 93.20
Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.
nested loops
You can use one or more loop inside any another while, for or do..while loop.
block
of
of
nhe
\oop i t e r m i n a t e s
the
current
loop
exccutea
1 . W h i l e l o o p
tne It
use
for br-
false, c o m m o n
most from a
lo
to becomes The exit
I s
u s e e l
a
hasty
quiring
h e d .
condition can
be
s t a t e m e n t
break
gram is
When
thexeeuted
e .The
nested
loops
while expressi
on are
using
I f you start
executi
S t a t e m e n t( s )
and
most loop
S y n t a x
ststement
next
second
s 7
a=1;b=1+0
ond step:a=
of
break
t h e
#Use
defines
i1
b = 1
:a=0;
: while i<6:
ts
series
elemen
Fibonacci
s t e p
Print (i)
t w o
#First
of if i-=3:
#
s u n
break
a, e b 0,
while a 10
a, b=b, a+b
i+1
print (a)
2
3
Continue statement
T h e continue stateme
and move
of the loop
13 .The continue staten
Print multiplication table
"))
number:
the
("Enter
n i n t
(1nput i = 0
i-1 hile i 6:
while ( i c 1 1 ) :
p r i n t( n , 'x',i,':',n*i) i+=1
f i==3:
i-it1 c o n t i n u e
print (i)
number: 45
Enter the
45 x 1 45
45 x 2 = 90
45 x 3 = 135
45 x 4 = 180
45x 5 = 225
45 x 6 = 270
6
45 x 7 = 315
45 x 8 360
45x 9 405
45 x 10 = 450
i1
while i<6:
print (i)
if i==3:
break
i+1
2
3
Continue statement
the curent itration
T h e continue statement rejects all rennaining statements in
the
and the control back to the top of the loop.
of the loop moves
i-0
hle i 6
it=1
f i-3
Continue
print (i)
Lab Manual
artment of Mathematics, SJBIT I sem Engineering Mathematics
2. for loop
a r e used for sequential traversal
iteration
l a l l s under the category of definite
elements from a container (tor example list, strina
ng, tuple) si
s o used to access
.SyntaX
in sequence
for variable_n ame
statement_1
statenent_2
101
103
105
107
109
111
113
115
117
119
121
123
125
127
129
#Multiplication table
nint (input (" Enter the number "))
for i in
range (1,11):
print (n, 'x' ,i,'=' ,n*i)
apple
banana
cherry
orange
Exercise:
1. Finding the factors of a number using for loop.
2. Check the given number is prime or not.
4. Arite a
program to print even numbers between 25 and 45.
5. Write a
program to
print all numbers divisible by 3 between 55 and 75.
1.1 Objectives:
Use pytho0n
Cartesian curves.
1. to plot
2.to plot polar curves.
https:/ matplotlib.org/stable/apoints(Scattered
1.2 pi/Pyplot_sunmary.htnl#module-matplotlib.pyplot
Example: Plotting
plot)
#importing the required
1 mport matplotlib.pyplot module
as
plt
x
=[1 ,2,3,4,6,7,8]#x axis values
y
2,7,9,1,5,10,3J #
corresponding y axis values
Plt.scatter (x, plotting the y) #
plt.xlabel ('x axis ')# naming points
-
the axis x
plt.ylabel ("y axis ')# naming the y axis
-
10
4 5
X axis
#
naning the axis
plt.ylabel('y axis')# naming the y axiS
plt.title("My first graph! ') #
giving title a to
plt.show () #function to
my graph
sho the plot
My first graph!
10
2 3 6 8
X- aXis
11
Exponential curve
20000
15000
10000
5000
-100 -75
-50 -25 00 25 50 75
2. Sine 100
and Cosine curves
nunpy as np
1 port
matplotlib.pyplot as
plt
np. arange (-10, 10, 0.001)
y1 =
np.sin (x))
y2=np.cos (x)
plt.plot (x, y1,x , y2) #
plotting sine and
cosine
plt.title("sine curve and Cosine
same
valuos
function
together
plt.zlabel ("Values of x") curve") of x ith
plt.ylabel("Values
plt grid ()
of sin(x) and
cos (x) ")
plt.show ()
12
1.00
0.75
0.50
0.25
0.00
-0.25
WW
-0.50
-0.75
.00
#A simple graph
import matplotlib.pyplot
import nunpy as np
as plt
X
np.linspace (0, 2, 100)
of y=x
plt.plot (x, x**3, label= 'cubic ' ) # Plot of *2 quadric a curve
cubic y=x*3 a curve
Simple Plot
8 linear
quadratic
aubic
13
3a-
y
=
(y, +
-2,5)
,
D e - C a r t e s :
r'
(X,
p 4 p l o t _i m p l i c 1 t (
(3-x).,x**3), Folium
of
5.
((y2)
Eq it(
(x, -5,5
x*y) ,x,
3.2*x*y ,
p6-plot- 1 mp 3,3 2
Eq(x*3
y
p5 plot_ implicit (
(4-x**2)),(x, -5,5),(y, 5,5)) a=2
Eq (4 (y -- 2) , (x**2)
The matplotlib.pyplot.pol
Syntax:
matplotlib.pyplot .polar
r : It is the distance.
4
1. Circle: r = p,Where
-2
import numpy as np
1nport matplotlib.PYP
plt.axes (projection
T 3
Tads np. arange (0,
16
4 2 4
r : It is the distance.
circle
1. Circle: r =
p,Where p is the radius of the
=
'polar ')
Plt.axes (projection
r 3
(2 *np.pi), 0.01)
rads np.arange (0,
17
90
135 45
0
0glos20
180
25 315
270
3.
Cardioid: r
=
5(1 +cos0)
dioid r=
os theta)
PTlab
-spece (0,2 np.pi, 1000)
ce (theta)
0
135
4
180
225
315
270
18
90
135
180
225 315
270
5. Cardioids: r =
a+ acos(6) and r =a -
acos(6)
pOLt numPy as np
1port matplotlib.pyplot as plt
pOrt mathh
19
i
port numpy 3 np
t0port matplotiib.pyplot as
def circle (r): pl
0 creato the 1int
Y U
ofz c0ordinates
creato the 1int of y c0ordinaten
or theta in np.
1inzpace ( 2*np.pl, 2 np p, 109
Wloop ovor a list of hta, uhich ranges f ron -2 :
. append (r*np. cus (theta))
Ddd Lho
(oITo#ponding pren i on 7 10
th 1
. append (r *np. tin(thet a))
l samo for y
4 2
20
Isem
Department of Mathematics, SJBIT Engineering Mathematics Lab Manual part
2. Cycloid: r
=a( sin0): y = a(1 - sin0)
def cycloid (r)
X U #create the list of x coordinates
y U #create the list of y coordinates
for theta in
np.linspace ( -2*np.pi, 2*np. pi, 100):
"loop over a list of theta, which ranges from -2 pi to 2 pi
X.append (r*(theta - np.sin (theta) ))
#add the
corresponding expression of x to the X list
y. append (r* (1 np. cos ( theta))) #same for y
4.0
3.5
3.0
25
20
15
10
05
00
-10 10
1.8 Exercise:
Plot the following:
1. Parabola y = 4ax
2. Hyperbola - =1
3. Lower half of the circle:
+2 =
4 + 4y y -
4. cos()
5. 1+sin(r +7)
6. Spiral of Archimedes: r =a + b6
7. Limacon: r =
a+b cos6
21
diff
An
(function variable)
,
2. Derivative()
1. I
Derivative (expression, reference variable)
fro
expression -
doit (x)
simplify (expression)
6. expression It is the
mathematical expression which neecds to
be
7. Returns: Returns a simpliied mathematical expression simplified.
.
expression.
8. display )
display (expression)
9. expression
-
22
SJBIT m Engineering
Department of
Mathematics,
wwatics Lab Manual
epartment
math e xpression. suba (vari able, substitute)
cos t).
rom
sympy import *
2. Find the
angle between the curves r =
4 cos t and r =
5 sint.
Iron sympy 1mport *
T,t s ymbols ('r,t')
r1 4* (cos (t)) ;
r2-5 (sin (t)) ;
qsolve(r1-r2,t)
yl at an (w1)
y2 at an (#2)
abs (y1-y2)
print('Angle betveen curves in radians is 0.41 %iloa*
,Eloat (w))
t Symbol('t') # define t as
symbol
rSymbol('r')
r=4*(1+cos (t))
r1=Derivative (r ,t) .doit () #find the first derivative
r2=Derivative (ri ,t). doit () #1ind the second of r
rho (r*2+T1**2) derivative of .t
(1.5)/(r**2+2*rl ** 2-r*r2);#
**
r .
Substitute r1! -
rho1=rho.subs (t,pi/2) # substitute formula
in rho t
print ('The radius of curvature 1s 13.4í units'
hrho1)
asin(t).
24
subs (a,r1);
PTint ('\n\nRadius of curvature at r=5 and t=
Curvature=1/rho2; pi/2 is', simplify (rho2))
print ( '\n\n Curvature at
(5,pi/2) is',float (curvature))
dydx =simplify (
Derivative (y,t) . doit ())/simplify (Derivative
rho- simplify((1+dydx*2) (x,t).doit ())
1.5/(Derivative (dydx ,t) .doit()/(Derivative (x
Radius of curvature
t).doit ())))
is ')
display (ratsimp (rho))
t1 pi/4
r1-1;
rho1=rho. subs (t ,
t1);
rho2=rho1 .subs (a,r1);
display ('Radius of curvature at r=1 and t=pi/4 is'
curvature=1/rho2; , simplify (rho2));
print (° \n\n Curvature at
(1,pi/4) is' ,float (curvature))
2.5 Exercise:
Plot the following:
25
id
radiun of eurvate of =acow"(0),u= anin"(0) at t =0.
a
An:
p2.82842712 and K=0.353553