1st Sem Lab Manual Python
1st Sem Lab Manual Python
B.Sc. Mathematics
Practical using
FOSS(Python) for
I semester B.Sc.
Effective from the academic Year 2020-21
(Draft)
Contents
Lab Description
No.
1 Introduction to Python :Basic syntax,variable types,basic
operators,numbers,strings,lists,tuples,functions and input/output
statements.
2 Some simple programs to understand the relational,conditional and logical
operators.
i)Compare two numbers (less than,greater than) using if statement
ii) Sum of natural numbers using while loop
iii) Finding the factors of a number using for loop
iv) To check the given number is prime or not (use if …else statement)
v)Find the factorial of a number (use if…if…else).
vi) Simple programs to illustrate logical operators (and, or , not)
3 Python commands to reduce given matrix to echelon form and normal form
with examples.
4 Python program/command to establish the constituency or otherwise and
solving system of linear equations.
5 Python command to find the nth derivatives.
6 Python program to find nth derivative with and without Leibnitz’s theorem.
Python is a widely used general purpose, high level programming language.it was initially
designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was
mainly developed emphasis on code readability, and its syntax allows programmers to express
concepts in fewer lines of code.
Python is a programming language that lets you work quickly and integrate systems more
efficiently
There are two major Python versions – Python 2 and Python 3. Both are quite different.
Beginning with Python programming:
Before we start Python programming, we need to have an interpreter and run our programs. There
are certain online interpreters like http://ide.geekforgeeks.org/, http://ideone.com/ or
http://codepad.org/ that can be used to start Python without installing an interpreter.
Windows: There are many interpreters available freely to run Python scripts like IDLE (Integrated
Development Environment) which is installed when you install the python software from
http://python.org/
1.1. INSTALLING PYTHON
1. Go to => https://www.python.org/downloads
2. Download the file
3. Double click on downloaded file (check THE BOX add python to path )
4. Install Now
5. Open python IDLE in search or in all program
6. Start working on IDLE
Input statement : input() is used to input string,int(input()) is used to input integer and
float(input()) is used to input floating value.
Output statement:
print statement :
a=2
b=3
The print statement is a function its general form is
Print(‘this is’,a,’that is’,b)
Where a and b are pre assigned variable names. If the above statement is executed the output will
be as , this is 2 that is 3, anything written between two single quotes or between two double
quotes are considered as string.
Lists: Lists are defined as a= [ 1,2,3,4], indexing start with 0 i.e. a[0]=1,a[1]=2,a[2[=3, a[3]=4.
The elements of the list may be any data type and are mutable.
Tuple : Tuples are the group of any data types separated by commas and are enclosed in
parenthesis.The elements of tuple are immutable. Examples are a=(1,2,3), b=( ‘a’,2,”C”).
Functions : Functions are defined as
def name(arguments):
return expression
for example def f(x,y):
return x+y+2
1.4.COMMANDS IN PYTHON
Execute the following commands,record the output and explain their meaning :
For Example:
>>> 2+3
Output: 5
Explanation :2+3=5
>>> 2-3
Output: -1
Explanation : 2-3=-1
>>> 2*3
Output: 6
Explanation : 2*3=6
>>> 2/3
Output :0.6666666666666666
Explanation : 2/3=.66666
>>> 2**3
Output: 8
Explanation : 23 =8
>>> 2//3
Output: 0
Explanation :quotient obtained in dividing 2 by 3
2.2.Execute the following commands in console , record the output and explain the nature of
the command.
>>> int(3/2)
>>> round(2.51)
>>> round(2.49)
>>> round(2.5)
>>> round(3.51)
>>> round(3.49)
>>> round(3.5)
>>> x=23145621.456872194
>>> x
>>> round(x)
>>> round(x,1)
>>> round(x,2)
>>> round(x,4)
>>> round(x,6)
>>> round(x,-1)
>>> round(x,-2)
>>> round(x,-4)
>>> round(x,-7)
>>> round(x,-8)
>>> x,y,z=1,2,3
>>> x
>>> x+y
>>> x+y+z
>>> print(x)
>>> print(x+y)
>>> print(x+y+z)
>>> x,x+y,x+y+z
>>> print(x,x+y,x+y+z)
>>> x=5
>>> x+=2
>>> x
>>> x-=2
>>> x
>>> a=[10,20,30,40]
>>> a
>>> a[0],a[1],a[2]
>>> a[3]
>>> a[-1]
>>> a[-2]
>>> a[-3]
>>> a[-4]
>>> a.append(50)
>>> a
>>> b=[60,70,80]
>>> a.append(b[0])
>>> a
>>> a.append(b)
>>> a
>>> a.insert(6,90)
>>> a
>>> a.insert(0,100)
>>> a
>>> a.pop()
>>> a
>>> a.pop()
>>> a
>>> a.pop(1)
>>> a
>>> a.pop(0)
>>> a
>>> a.pop(-1)
>>> a
>>> a.remove(40)
>>> a
>>> a.remove(a[1])
>>> a
>>> a=[1,5,9,7,1,0,6,9]
>>> a
>>> a.sort()
>>> a
>>> b=[12,36,0,15,78,9,3,4,6,7]
>>> b
>>> b.sort()
>>> b
>>> import math
>>> math.sin(1)
>>> math.cos(1)
>>> math.e
>>> math.pi
>>> import math as m
>>> m.sin(1)
>>> m.cos(1)
>>> m.e
>>> m.pi
>>> from math import *
>>> sin(1)
>>> cos(1)
>>> e
>>> pi
>>> sin(pi/2)
>>> cos(pi/2)
>>> tan(pi/2)
>>> log(e)
>>> log(100)
>>> log10(100)
>>> log2(16)
>>> asin(.5)
>>> acos(.6)
>>> atan(35)
>>> asin(1)
>>> acos(1)
>>> asin(-1)
>>> acos(-1)
>>> atan(-1)
>>> atan(1.1)
>>> asinh(2)
>>> acosh(2)
>>> ceil(2.12354)
>>> ceil(6.99999)
>>> ceil(6.000001)
>>> floor(2.2315)
>>> floor(2.99999)
>>> sinh(2)
>>> cosh(2)
>>> tanh(2)
>>> degrees(pi)
>>> degrees(pi/2)
>>> radians(90)
>>> radians(pi/4)
>>> radians(pi/4)
>>> radians(180)
>>> exp(2)
>>> e**2
>>> abs(x)
>>> x
>>> x=-23.125
>>> abs(x)
>>> fabs(x)
>>> x=-123
>>> abs(x)
>>> x
>>> fabs(x)
>> factorial(5)
>>> factorial(10)
>>> factorial(0)
>>> 3//2
>>> 5//2
>>> 5%2
>>> 13%5
>>> fmod(5,2)
>>> fmod(13,5)
>>> gcd(15,25)
>>> gcd(7,13)
>>> gcd(-20,4)
>>> hypot(0,1)
>>> hypot(1,1)
>>> hypot(3,5)
>>> modf(2.5)
>>> modf(35.26)
>>> pow(2,3)
>>> pow(3,4)
>>> x=range(10)
>>> x
>>> x[0]
>>> x[5]
>>> x[9]
>>> len(x)
>>> min(x)
>>> max(x)
>>> sum(x)
>>> x=range(5,10)
>>> len(x)
>>> x[0]
>>> x[3]
>>> x[4]
>>> x=range(2,6)
>>> list(x)
>>> x
>>> list(_)
>>> a={1,2,3,4,5}
>>> a
>>> a={1,2,'b','i',6}
>>> a
{'b', 1, 2, 6, 'i'}
>>> a={1,1,3,4,6,5,3}
>>> a
>>> x=3+4j
>>> x
>>> y=4+3j
>>> x+y
>>> x-y
>>> x*y
>>> x/y
>>> abs(x)
>>> bin(20)
>>> oct(20)
>>> hex(20)
>>> int(0b101101)
>>> int(0o120367)
>>> int(0x12abc534)
>>> int(0o234)
2.1. if statement
The syntax of if statement is
if condition:
Statement1
Statement2
Statement3
Statement4
Statement5
Statement6
Statement7
Here the “condition” is a logical expression which is either true or false if the “condition”
is true then all the statements in the block ( Statement1 to statement6) are executed and then the
control moves to execute statement7. If the ‘condition ” is false then Statement1 to statement6 are
skipped and the control moves to execute statement7.It may be observed that statement1 to
statement7 are placed with same indent which is called the block of if statement.
2.2. if else statement
The syntax of if else statement is
if condition:
Statement1
Statement2
Statement3
Statement4
Statement5
Statement6
else:
Statement7
Statement8
Stayement9
Statement10
Here the “condition” is a logical expression which is either true or false if the “condition”
is true then all the statements in the block ( Statement1 to statement6) are executed and then the
control moves to execute statement10. If the ‘condition ” is false then Statement1 to statement6
are skipped ,Statement7 to Statement9 are executed and the control moves to execute
statement10.It may be observed that statement1 to statement7 are placed with same indent which
is called the true block and Statement7 to Statement9 is called false block.
2.3. nested if statement
The syntax of if else statement is
if condition1:
BLOCK1
elif condition2:
BLOCK2
else:
BLOCK3
If condition1 is true BLOCK1 is executed else if condition2 is true BLOCK2 is executed
else BLOCK3 is executed.
2.4. for loop
The syntax of for loop is
for i in range(n):
S1
S2
S3
print()
First time i is initialized to zero and all the statements in the body of for loop(i.e. S1,S2,S3)
are executed once, then i increased by 1, again all the statements in the body are executed, again
i increased by 1 and the process continues ,when i reaches n then the control comes out of for loop
and the next statement (in this case it is the print statement) is executed ( I takes the values from 0
to n-1).
for i in range(1,n):
S1
S2
S3
print()
In this case i start with 1and takes the values up to n-1.
for i in range(1,n,2):
S1
S2
S3
print()
In this case i start with 1 and takes the values less than n with increment 2.
OUT PUT
>>> compare(2,3)
3>2
>>> compare(20,5)
20 > 5
#2.2.Sum of natural numbers using while loop
def sonn(n):
i,s=1,0
while i<=n:
s=s+i
i=i+1
print('sum of first',n,'natural numbers =',s)
OUT PUT
>>> sonn(5)
sum of first 5 natural numbers = 15
>>> sonn(10)
sum of first 10 natural numbers = 55
OUT PUT
>>> factors(10)
The factors of 10 are
1 2 5 10
>>> factors(100)
The factors of 100 are
1 2 4 5 10 20 25 50 100
OUT PUT
>>> pri(7)
7 is a prime
>>> pri(10)
10 is not a prime
>>> pri(23)
23 is a prime
>>> pri(37)
37 is a prime
a,b=False,False
print('a and b is',a and b)
print('a or b is',a or b)
print('not a is',not a)
print('not b is',not b)
OUT PUT
a and b is False
a or b is False
not a is True
not b is True
a,b=2,3
p=a>1
q=b>1
print(a>1 and b>1)
print(a>1 or b>1)
print(not p,not q)
OUT PUT
True
True
False False
def big(a,b,c):
if(a>=b and a>=c):
print(a,'is bigger')
elif (b>=a and b>=c):
print(b,'is bigger')
else:
print(c,'is bigger')
OUT PUT
big(10,20,30)
30 is bigger
>>> big(12,67,24)
67 is bigger
>>> big(35,63,85)
85 is bigger
___________________________________________________________________
Exercise :
1.Write a program to find the factorial of a natural number n
2. Write a program to find the sum of first n natural numbers
3. Write a program to find the roots of a quadratic equayion
4. Write a program to reverse a number
5. Write a program to check the given number is palindrome or not
6. Write a program to check the given number is prime or not
7. write a program to print all odd numbers between 0 and 20
8. write a program to print even numbers between 25 and 45
9. Write a program to print all numbers divisible by 3 between 55 and 75
10. Write a program to print the first n Fibonacci numbers
OUT PUT
echelonform of
⎡1 2 3⎤
⎢5 3 6⎥
⎣6 4 2⎦
is
⎡1 2 3 ⎤
⎢0 -7 -9⎥
⎣0 0 40⎦
OUT PUT
Normal form of
⎡1 2 4 ⎤
⎢-1 -2 -4⎥
⎢2 4 8 ⎥
⎣3 6 9 ⎦
is
(Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 0],
[0, 0, 0]]), (0, 1))
𝑬𝒙𝒆𝒓𝒄𝒊𝒔𝒆: 𝐹𝑖𝑛𝑑 𝑡ℎ𝑒 𝑒𝑐ℎ𝑒𝑙𝑜𝑛 𝑎𝑛𝑑 𝑛𝑜𝑟𝑚𝑎 𝑓𝑜𝑟𝑚𝑠 𝑜𝑓 𝑡ℎ𝑒 𝑓𝑜𝑙𝑙𝑜𝑤𝑖𝑛𝑔 𝑚𝑎𝑡𝑟𝑖𝑐𝑒𝑠
4 6 5
1 3 5 3 5 6 3 3 5
1 2 3 2 5 4 6 7
𝑖) [ ] 𝑖𝑖) [1 5 7] 𝑖𝑖𝑖) [2 5 7 6] 𝑖𝑣) [ ] 𝑣) [ ] 𝑣𝑖) [4 5]
5 2 6 7 8 2 4 6
3 2 6 8 5 7 8 7 8
8 9 7
OUT PUT
Enter the number of unknowns3
Rank(A)= 3
Rank(A|B)= 3
The system has unique solution and the solution is
{x: 7/4, y: 17/12, z: -11/12}
𝐸𝑥𝑒𝑟𝑐𝑖𝑠𝑒 𝑺𝒐𝒍𝒗𝒆 ∶
1. 𝑆𝑜𝑙𝑣𝑒 𝑥 + 𝑦 + 𝑧 = 4, 2𝑥 + 𝑦 − 𝑧 = 1, 𝑥 − 𝑦 + 2𝑧 = 2
OUT PUT
Enter the number of unknowns3
Rank(A)= 3
Rank(A|B)= 3
The system has unique solution and the solution is
{x: 3/7, y: 13/7, z: 12/7}
2. 𝑥 + 2𝑦 + 2𝑧 = 1, 2𝑥 + 𝑦 + 𝑧 = 2, 3𝑥 + 2𝑦 + 2𝑧 = 3
OUT PUT
Enter the number of unknowns3
Rank(A)= 2
Rank(A|B)= 2
The system has infinitely many solutions and the solution is
FiniteSet((1, -z, z))
3. 𝑥 + 𝑦 − 2𝑧 = 5, 𝑥 − 2𝑦 + 𝑧 = −2, −2𝑥 + 𝑦 + 𝑧 = 4
OUT PUT
Enter the number of unknowns3
Rank(A)= 2
Rank(A|B)= 3
The system is inconsistent and has no solution
4. 𝑥 + 2𝑦 − 𝑧 = 3, 3𝑥 − 𝑦 + 2𝑧 = 1, 2𝑥 − 2𝑦 + 3𝑧 = 2,
OUT PUT
Enter the number of unknowns3
Rank(A)= 3
Rank(A|B)= 3
The system has unique solution and the solution is
{x: -1, y: 4, z: 4}
OUT PUT
>>> der(1)
cos(x)/sin(x)
>>> der(2)
-(1 + cos(x)**2/sin(x)**2)
>>> der(3)
2*(1 + cos(x)**2/sin(x)**2)*cos(x)/sin(x)
>>> der(4)
-2*(1 + 4*cos(x)**2/sin(x)**2 + 3*cos(x)**4/sin(x)**4)
𝑖𝑖𝑖) 𝑒 2𝑥
OUT PUT
>>> der(1)
2*exp(2*x)
>>> der(5)
32*exp(2*x)
>>> der(10)
1024*exp(2*x)
>>> der(20)
1048576*exp(2*x)
>>>
𝑖𝑣) log(𝑐𝑜𝑠𝑥)
OUT PUT
>>> der(1)
-sin(x)/cos(x)
>>> der(3)
-2*(sin(x)**2/cos(x)**2 + 1)*sin(x)/cos(x)
𝑣) (2𝑥 + 3)4
OUT PUT
der(1)
8*(2*x + 3)**3
>>> der(4)
384
𝑣) sin3 𝑥
OUT PUT
der(1)
3*sin(x)**2*cos(x)
>>> der(3)
3*(-7*sin(x)**2 + 2*cos(x)**2)*cos(x)
𝒗𝒊) 𝐜𝐨𝐬 𝟑 𝒙
OUT PUT
der(3)
3*(-2*sin(x)**2 + 7*cos(x)**2)*sin(x)
𝑣𝑖𝑖) tan2 𝑥
OUT PUT
der(1)
(2*tan(x)**2 + 2)*tan(x)
>>> der(3)
8*(tan(x)**2 + 1)*(3*tan(x)**2 + 2)*tan(x)
OUT PUT
der(2)
2*exp(x)*cos(x)
OUT PUT
zx= -y/(x**2 + y**2)
zy= x/(x**2 + y**2)
zxy= (-x**2 + y**2)/(x**2 + y**2)**2
zyx= (-x**2 + y**2)/(x**2 + y**2)**2
zxx= 2*x*y/(x**2 + y**2)**2
zyy= -2*x*y/(x**2 + y**2)**2
𝑬𝒙𝒆𝒓𝒄𝒊𝒔𝒆 ∶ 𝐹𝑖𝑛𝑑 𝑎𝑙𝑙 𝑓𝑖𝑟𝑠𝑡 𝑎𝑛𝑑 𝑠𝑒𝑐𝑜𝑛𝑑 𝑜𝑟𝑑𝑒𝑟 𝑝𝑎𝑟𝑡𝑖𝑎𝑙 𝑑𝑒𝑟𝑖𝑣𝑎𝑡𝑖𝑣𝑒𝑠 𝑜𝑓 𝑡ℎ𝑒 𝑓𝑜𝑙𝑜𝑤𝑖𝑛𝑔
𝑦 𝑥 𝜕𝑢 𝜕𝑢 𝜕𝑢
𝑖)𝑥 𝑦 + 𝑦 𝑥 𝑖𝑖 ) 𝑥 2 tan−1 𝑥 − 𝑦 2 tan−1 𝑦 𝑖𝑖𝑖) 𝑖𝑓 𝑢 = 𝑥 2 𝑦 + 𝑦 2 𝑧 + 𝑧 2 𝑥 𝑓𝑖𝑛𝑑 𝜕𝑥 + 𝜕𝑦 + 𝜕𝑧
𝜕𝑢 𝜕𝑢 𝜕𝑢
𝑖𝑣) 𝑖𝑓 𝑢 = 𝑥 3 + 𝑦 3 + 𝑧 3 − 3𝑥𝑦𝑧 𝑓𝑖𝑛𝑑 + +
𝜕𝑥 𝜕𝑦 𝜕𝑧
𝜕𝑢 𝜕𝑢 𝜕𝑢
𝑣) ) 𝑖𝑓 𝑢 = 𝑥 3 + 𝑦 3 + 𝑧 3 + 3𝑥𝑦𝑧 𝑓𝑖𝑛𝑑𝑥 +𝑦 +𝑧
𝜕𝑥 𝜕𝑦 𝜕𝑧
Lab 8 :Verification of Euler’s theorem
#Program to verify Euler's theorem for u=(x**2+y**2)/(x+y)
from sympy import *
x,y=symbols('x,y')
n=int(input('Enter the degree'))
u=(x**2+y**2)/(x+y)
ux=diff(u,x)
uy=diff(u,y)
lhs=simplify(x*ux+y*uy)
rhs=n*u
if lhs==rhs:
print('Eulers theorem is satisfied')
else:
print('Eulers theorem not satisfied')
OUT PUT
Enter the degree1
Eulers theorem is satisfied
OUT PUT
Enter the degree2
Eulers theorem is satisfied
OUT PUT
jacobian(A)= x
>>>
#Finding jacobian x=r*cos(t), y=r*sin(t)
from sympy import *
x,y,r,t=symbols('x,y,r,t')
x=r*cos(t)
y=r*sin(t)
A=Matrix([x,y])
jacA=A.jacobian([r,t])
jacobian=simplify(det(jacA))
print('jacobian(A)=',jacobian)
OUT PUT
jacobian(A)= r
OUT PUT
red(2)
x/2 - sin(x)*cos(x)/2
>>> red(4)
3*x/8 - sin(x)**3*cos(x)/4 - 3*sin(x)*cos(x)/8
#Finding the integration of sinx^m*cosx^n
from sympy import *
def red(m,n):
x=symbols('x')
print(integrate(sin(x)**m*cos(x)**n,x))
OUT PUT
>>> red(1,1)
sin(x)**2/2
>>> red(2,1)
sin(x)**3/3
>>> red(1,2)
-cos(x)**3/3
>>> red(2,2)
x/8 - sin(2*x)*cos(2*x)/16
'
#Finding the integration of sinx^n x: 0 t0 pi/2
from sympy import *
def red(n):
x=symbols('x')
print(integrate(sin(x)**n,[x,0,pi/2]))
OUT PUT
>>> red(1)
1
>>> red(3)
2/3
>>> red(10)
63*pi/512
>>> red(20)
46189*pi/524288
OUT PUT
>>> sphere(0,0,0,2)
The equation of the sphere with centre ( 0 0 0 ) and radius r= 2 is)
x**2 + y**2 + z**2 - 4 = 0
>>> sphere(1,1,2,3)
The equation of the sphere with centre ( 1 1 2 ) and radius r= 3 is)
(x - 1)**2 + (y - 1)**2 + (z - 2)**2 - 9 = 0
#Equation of the sphere with two ends of diameter A(x1,y1,z1) and B(x2,y2,z2)
from sympy import *
from sympy.abc import x,y,z
def sphere(x1,y1,z1,x2,y2,z2):
eq=(x-x1)*(x-x2)+(y-y1)*(y-y2)+(z-z1)*(z-z2)
eq=simplify(eq)
print('The equation of the sphere with two ends of diameter A(',x1,y1,z1,
') and B(',x2,y2,z2,')is \n', expand(eq),'=',0)
OUT PUT
>>> sphere(1,2,3,5,6,7)
The equation of the sphere with two ends of diameter A( 1 2 3 ) and B( 5 6 7 )is
x**2 - 6*x + y**2 - 8*y + z**2 - 10*z + 38 = 0
>>>
OUT PUT
Equation of tangent plane is
-2*x + 2*y - z – 12
10.2. Cone
'''Equation of the right circular cone with vertex (a,b,c)and its axis on
the line whose direction ratios are l,m,n and semivertical angle t '''
from sympy import *
from sympy.abc import x,y,z
from math import *
def cone(a,b,c,l,m,n,t):
eq=(l*(x-a)+m*(y-b)+n*(z-c))**2-(l**2+m**2+n**2)*((x-a)**2+(y-b)**2+(z-
c)**2)*(cos(radians(t)))**2
eq=expand(eq)
print('The equation of the cone is \n', eq,'=',0)
OUT PUT
>>> cone(0,0,0,0,0,1,30)
The equation of the cone is
-0.75*x**2 - 0.75*y**2 + 0.25*z**2 = 0
>>> cone(0,0,0,0,1,0,30)
The equation of the cone is
-0.75*x**2 + 0.25*y**2 - 0.75*z**2 = 0
10.3. Cylinder
OUT PUT
>>> cylinder(1,2,3,2,-3,6,2)
The equation of the cylinder is
45*x**2/49 + 12*x*y/49 - 24*x*z/49 - 6*x/7 + 40*y**2/49 + 36*y*z/49 - 40*y/7 + 13*z**2/49
- 18*z/7 + 6 = 0
>>> cylinder(1,3,5,2,2,-1,3)
The equation of the cylinder is
5*x**2/9 - 8*x*y/9 + 4*x*z/9 - 2*x/3 + 5*y**2/9 + 4*y*z/9 - 14*y/3 + 8*z**2/9 - 32*z/3 + 25
=0
>>> cylinder(1,0,3,2,3,1,2)
The equation of the cylinder is
5*x**2/7 - 6*x*y/7 - 2*x*z/7 - 4*x/7 + 5*y**2/14 - 3*y*z/7 + 15*y/7 + 13*z**2/14 - 37*z/7 +
59/14 = 0