Python Record
Python Record
2024 – 2025
DEPARTMENT OF MATHEMATICS
I M Sc. MATHEMATICS
VAP-II– MATHEMATICAL COMPUTATION USING PYTHON -PRACTICAL
Register No. : __________________
Examiner
INDEX
S.NO. DATE TOPIC PAGE SIGNATURE
1. 04.12.2024 AREA OF THE TRIANGLE 1-2
Output:
Enter the value of side a:4
Enter the value of side b:6
Enter the value of side c:8
The Area of the triangle is:11.6189500
1
1. Area Of Triangle
Aim:
Find the area of triangle.
Algorithm:
Step-1:Start
Step-2:Enter the values of sides a,b,c
Step-3:S=(a+b+c)/2
Step-4:Area=sqrt(s*(s-a)*(s-b)*(s-c))
Step-5: Write “ The Area of the triangle is”,Area
Step-6:stop
Result:
Thus the program has been executed successfully.
2
Program:
Import math
P= int( input ( “Enter the principle amount:”))
N=int( input ( “Enter the no of years n:”))
R=int (input (“ Enter the rate of interest r:”))
SI=( p* n* r )/100
Print (“The Simple interest SI is:”, SI)
CI=p*( pow((1+r/100),n)-1)
Print (“ The Compund Interest CI is:”,CI)
Output :
Enter the principle amount p:1000
Enter the no of years n:5
Enter the rate of interest r:10
The simple interest SI is:500
The Compound Interest CI is:1464.1000
3
2. Simple Interest And Compound Interest
Aim:
Raja has borrowed 1000Rs at the of 10% simple interest and compound interest per annum from his friend
Ramu.He proposes to pay the amount borrowed with interest after 5 years.Calculate the amount Raja needs.
Algorithm:
Step-1: Start
Step-2: Read the value of P
Step-3: Read the value of n
Step-4: Read the value of r
Step-5: SI=( p* n* r )/100
Step-6: Write SI
Step-7: CI=p*( pow((1+r/100),n)-1)
Step-8: Write CI
Step-9: Stop
Result:
Thus the program has been executed successfull
4
Program:
Import math
A= int( input (Enter the value for a:”))
B= int( input ( “ Enter the value for b:”))
C= int( input (“ Enter the value of c:”))
If a>b:
Big=a
else:
Big = b
If c>big:
Big=c
Print (“ The largest of three”, “ a=”,a,”b=”,b,”c=”,c,”is”, big)
Output:
Enter the value for a:20
Enter the value for b:30
Enter the value for c:10
The largest of three a=20,b=30,c=10 is 30
5
3. Largest Three Numbers
Aim:
The largest among three integers using the values a=20,b=30,c=10
Algorithm:
Step-1: Start
Step-2: Read the values of a, b, c
Step-3: if a>b big=a
Step-4:else big=b
Step-5: if c>b big= c
Step-6:” write the largest of three numbers a, b, c is”, big
Step-7: Stop
Result:
Thus the program has been executed successfully.
6
Program:
# Program to check if a number is even or odd
Num = int(input(“Enter a number: “))
If num % 2 == 0:
Print(“Even”)
Else:
Print(“Odd”)
Output:
Enter a number:47
Odd
Enter a number:40
Even
7
4. Odd or Even
Aim:
Find a number whether it is even or odd
Algorithm:
Step-1: Start
Step-2: Enter the number
Step-3: if num%2==0
Step-4: The number is even
Step-5: else
Step-6: The number is odd
Step-7: Stop
Result:
Thus the program has been executed successfully.
8
Program:
Output:
Enter a year:2010
2010 is not a leap year
6.Compare Two Number
9
5. Leap year
Aim:.
Find a year whether it is a leap year or no
Algorithm:
Step-1: Start
Step-2: Enter the year
Step-3:if(year%4==0&year%100!=0)
Step-4:The year is a leap year
Step-5: else
Step-6:The year is not a leap year
Step-7:stop
Result:
Thus the program has been executed successfully.
10
Program:
# Program to compare two numbers using relational operators
Num1 = float(input(“Enter the first number: “))
Num2 = float(input(“Enter the second number: “))
# Relational operators
If num1 == num2:
Print(“Both numbers are equal.”)
Elif num1 > num2:
Print(f”{num1} is greater than {num2}.”)
Else:
Print(f”{num1} is less than {num2}.”)
Output:
Enter the first number:10
Enter the second number:15
10 is less than 15
11
6. Compare Two Number
Aim:
To compare two numbers using relational operators
Algorithm:
Step-1: Start
Step-2: Enter the first number
Step-3: Enter the second number
Step-4:if num1== num2
Step-5: Both numbers are equal
Step-6:elif num1>num2
Step-7:num1 is greater than num2
Step-8: else num1<num2
Step-9:num1 is less than num2
Step-10;Stop
Result:
Thus the program has been executed successful
12
Program:
# Program to convert temperature from Celsius to Fahrenheit
Celsius = float(input(“Enter temperature in Celsius: “))
# Conversion formula
Fahrenheit = (Celsius * 9/5) + 32
Output:
Enter temperature in celcius:50
Temperature in Fahrenheit:122
13
7. Convert Temperature
Aim:
To convert Temperature from celcius to Fahrenheit
Algorithm:
Step-1: Start
Step-2: Enter the temperature in celcius
Step-3: Fahrenheit=( celcius*9/5)+32
Step-4: “ Temperature in Fahrenheit:”, Fahrenheit
Step-5: Stop
Result:
Thus the program has been executed successfully
14
Program:
# Program to demonstrate logical operators
Age = int(input(“Enter your age: “))
Income = float(input(“Enter your annual income: “))
# Logical operators
If age >= 18 and income >= 50000:
Print(“You are eligible to apply for a loan.”)
Else:
Print(“You are not eligible for a loan.”)
Output:
Enter your age: 20
Enter your annual income: 73000
You are eligible to apply for a loan.
15
8. Logical operator
Aim:
Demonstrate logical operators
Algorithm:
Step-1: Start
Step-2: Enter the age
Step-3:Enter the income
Step-4: Using the logical operator if check age is>18 and income is >=50000
Step-5: You are eligible to apply for a loan
Step-6: else
Step-7: You are not eligible to apply for a loan
Step-8:Stop
Result:
Thus the program has been executed successfully
16
Program:
# Program to find the maximum number among three numbers
Num1 = float(input(“Enter the first number: “))
Num2 = float(input(“Enter the second number: “))
Num3 = float(input(“Enter the third number: “))
Output:
Enter the first number: 10
Enter the second number: 20
Enter the third number: 30
Maximum number: 30.0
17
9. Maximum of Three Numbers
Aim:
To find the maximum number among three numbers
Algorithm:
Step-1: Start
Step-2: Enter the first number
Step-3: Enter the second number
Step-4: Enter the third number
Step-5: max_ num =max(num1, num2, num3)
Step-6:”Maximum number:”,Max_num
Step-7: Stop
Result:
Thus the program has been executed successfully
18
Program:
# Program to calculate the square root of a number
Import math
Output:
Enter a number: 25
Square root: 5.0
19
10. Square Root
Aim:
To find the square root of a number
Algorithm:
Step-1: Start
Step-2: Enter a number
Step-3: sqrt_result =math.sqrt(num)
Step-4:” Square root:”,sqrt_result
Step-5: Stop
Result:
Thus the program has been executed successfully
20
Program:
# Program to calculate the sum of numbers from 1 to n
N = int(input(“Enter a number: “))
Sum_result = sum(range(1, n + 1))
Print(“Sum:”, sum_result)
Output:
Enter a number: 4
Sum: 10
21
11. Sum Of Numbers
Aim:
To find the sum of numbers from 1 to n
Algorithm:
Step-1: Start
Step-2: Enter a number
Step-3:sum_ result=sum( range(1+n+1)(
Step-4:”Sum”,sum_ result
Step-5:Stop
Result:
Thus the program has been executed successfully
22
Program:
# Input: positive integer n
N = int(input(“Enter a positive integer: “))
# Initialize variables
Sum = 0
i=1
Output:
Enter a positive integer: 7
The sum of natural numbers from 1 to 7 is: 28
23
12. Sum Of Natural Numbers
Aim:
To find sum of natural numbers from 1 to n
Algorithm:
Step-1: Start
Step-2: Enter a positive integer
Step-3:Assign sum=0 &i=1
Step-4: Using while loop find the sum of numbers from 1 to n
Step-5: Print the sum from 1 to n
Step-6: Stop
Result:
Thus the program has been executed successfully.
24
Program:
# Input: positive integer num
num = int(input(“Enter a positive integer: “))
Output:
Enter a positive integer: 4
Factors of 4 are:
1
2
4
25
13. Factors
Aim:
To find factors of the positive integer
Algorithm:
Step-1: Start
Step-2:Enter the positive integer
Step-3: Using for loop check the remainder is zero or not
Step-4: print the factors
Step-5: Stop
Result:
Thus the program has been executed successfully
26
Program:
# Input: positive integer num
Num = int(input(“Enter a positive integer: “))
Output:
Enter a positive integer: 6
6 is not a prime number.
Enter a positive integer:5
5 is a prime number
27
14. Prime Number
Aim:
To find prime number of the positive integer
Algorithm:
Step-1: Start
Step-2: Enter a positive integer
Step-3: Using if else statement check the number is >1
Step-4:if the number is greater than 1 , using for loop check the number is prime or not
Step-5:if the number is prime print the number is prime
Step-6: Otherwise is not prime
Step-7:Stop
Result:
Thus the program has been executed successfully
28
Program:
Def factorial(n):
if n < 0:
Print(“Factorial is not defined for negative numbers.”)
elif n == 0 or n == 1:
return 1
else:
result = 1
For i in range(2, n + 1):
result *= i
return result
# Example usage
Number = int(input(“Enter a number: “))
Print(f”The factorial of {number} is: {factorial(number)}”)
Output:
Enter a number:4
The Factorial
29
15. Factorial
Aim:
To find factorial of a number
Algorithm:
Step-1: Start
Step-2: Enter a number
Step-3:Define a factorial function
Step-4: Using if statement check the number is <0
Step-5: if so print factorial is not defined for negative numbers
Step-6: if the number is 0 or 1, return 1
Step-7: if not, using for loop find the factorial of the number
Step-8:print factorial of a number
Step-9: Stop
Result:
Thus the program has been executed successfully
30
Program:
# Logical AND
Def logical_and_example(a, b):
if a > 0 and b > 0:
Print(“Both numbers are positive.”)
else:
Print(“At least one number is not positive.”)
# Example usage
Logical_and_example(5, 7)
Logical_and_example(-3, 8)
Output:
Both numbers are positive.
At least one number is not positive.
31
16. Logical AND
Aim:
To find both numbers are positive or not using logical AND operator
Algorithm:
Step-1: Start
Step-2:Define logical_and_ example (a,b) function
Step-3: Check a>0 and b>0 if so print both numbers are positive
Step-4:if not atleast one number is positive
Step-5:Stop
Result:
Thus the program has been executed successfully
32
Program:
Def check_number(num):
if num % 2 == 0 or num> 10:
return True
else:
return False
# Example usage
Number_to_check = 7
result = check_number(number_to_check)
Print(f”Is {number_to_check} either even or greater than 10? {result}”)
Output:
Is 7 either even or greater than 10? False
33
17. True Or False
Aim:
To check the result True or False
Algorithm:
Step-1: Start
Step-2: Assign number 7 to num
Step-3: Define a function check number
Step-4: if remainder is when divided by 2 or num>10, return True
Step-5: Otherwise return False
Step-6:Stop
Result:
Thus the program has been executed successfully
34
Program:
Def check_age(age):
if not (13 <= age <= 19):
return True
else:
return False
# Example usage
User_age = 25
result = check_age(user_age)
Print(f”Is the user with age {user_age} not a teenager? {result}”)
Output:
Is the user with age 25 not a teenager? True
35
18. Check Age
Aim:
To check the age whether it is between 13 & 19
Algorithm:
Step-1: Start
Step-2: Assign 25 to user age
Step-3:Define the function check age
Step-4: if age is not between 13 & 19, return True
Step-5: Otherwise return False
Step-6: Stop
Result:
Thus the program has been executed successfully
36
Program:
Import sympy as sp
# Define the variable and the function
x = sp.symbols(‘x’)
f = 3*x**2 + 2*x – 1
# Find the derivative
derivative_f = sp.diff(f, x)
# Print the result
Print(“original Function:”, f)
Print(“derivative:”, derivative_f)
OUTPUT
Original Function: 3*x**2 + 2*x - 1
Derivative:6x+2
37
19. Derivative of algebraic function
Aim:
To find the differentiation of algebraic function
Algorithm:
Step1: Start the program
Step2:Import the python library sympy for symbolic mathematics
Step3:Define the variable and the algrbraic function
Step4:Find the derivative of the function using sp.diff(f,x)
Step5: Print the result
Step6:End the program
RESULT:
Thus the program has been executed successfully.
38
Program:
import sympy as sp
# Define the variable and the function
x = sp.symbols('x')
g = sp.sin(x)
# Find the derivative
derivative_g = sp.diff(g, x)
# Print the result
print("Original Function:", g)
print("derivative:", derivative_g)
OUTPUT
Original Function: sin(x)
Derivative: cos(x)
39
20. DERIVATIVE OF TRIGONOMETRIC FUNCTION
Aim:
To find the differentiation of trigonometric function
Algorithm:
Step1: Start the program
Step2:Import the python library sympy for symbolic mathematics
Step3:Define the variable and the trigonometric function
Step4:Find the derivative of the function using sp.diff(g,x)
Step5: Print the result
Step6:End the program
RESULT:
Thus the program has been executed successfully.
40
Program:
import sympy as sp
# Define the variable and the function
x = sp.symbols('x')
h = sp.exp(2*x)
# Find the derivative
derivative_h = sp.diff(h, x)
# Print the result
print("Original Function:", h)
print("derivative:", derivative_h)
OUTPUT
Original Function: exp(2*x)
Derivative:2* exp(2*x)
41
21. DERIVATIVE OF EXPONENTIAL FUNCTION
Aim:
To find the differentiation of exponential function
Algorithm:
Step1: Start the program
Step2:Import the python library sympy for symbolic mathematics
Step3:Define the variable and the exponential function
Step4:Find the derivative of the function using sp.diff(h,x)
Step5: Print the result
Step6:End the program
RESULT:
Thus the program has been executed successfully.
42
Program:
import sympy as sp
# Define the variable and the function
x = sp.symbols('x')
f = x**3 + 2*x*2 - 4*x + 1
# Find the second derivative
second_derivative_f = sp.diff(f, x, 2)
# Print the result
print("Original Function:", f)
print("Second Derivative:", second_derivative_f)
OUTPUT
Original Function: x**3 + 2*x*2 - 4*x + 1
Second Derivative:2*(3*x+2)
43
22. Second order Derivative
Aim:
To find the second order derivative of algebraic function
Algorithm:
Step1: Start the program
Step2:Import the python library sympy for symbolic mathematics
Step3:Define the variable and the algebraic function
Step4:Find the derivative of the function using sp.diff(f,x,2)
Step5: Print the result
RESULT:
Thus the program has been executed successfully.
44
Program:
import sympy as sp
# Define the variables and the function
x, y = sp.symbols('x y')
f = 2*x*2 + 3*x*y - y*2
# Find the partial derivatives
partial_derivative_x = sp.diff(f, x)
partial_derivative_y = sp.diff(f, y)
# Print the results
print("Original Function:", f)
print("Partial Derivative with respect to x:", partial_derivative_x)
print("Partial Derivative with respect to y:", partial_derivative_y)
OUTPUT
Original Function: 2*x*2 + 3*x*y - y*2
Partial Derivative with respect to x: 4*x+3*y
Partial Derivative with respect to y: 3*x−2*y
45
23. PARTIAL DERIVATIVE OF ALGEBRAIC FUNCTION
Aim:
To find the partial derivative of algebraic function
Algorithm:
Step1: Start the program
Step2:Import the python library sympy for symbolic mathematics
Step3:Define the variable and the algebraic function
Step4:Find the derivative of the function with respect to x using sp.diff(f,x)
Step5:Find the derivative of the function with respect to y using sp.diff(f,y)
Step6: Print the result
RESULT:
Thus the program has been executed successfully.
46
Program:
import sympy as sp
# Define the variables and the function
x, y = sp.symbols('x y')
h = sp.exp(x*y)
# Find the mixed partial derivatives
partial_derivative_xy = sp.diff(h, x, y)
partial_derivative_yx = sp.diff(h, y, x)
# Print the results
print("Original Function:", h)
print("Mixed Partial Derivative (d/dx d/dy):", partial_derivative_xy)
print("Mixed Partial Derivative (d/dy d/dx):", partial_derivative_yx)
OUTPUT
Original Function: exp(x*y)
Mixed Partial Derivative (d/dx d/dy): (x*y+1)* exp(x*y)
Mixed Partial Derivative (d/dy d/dx): (x*y+1)* exp(x*y)
47
24. MIXED PARTIAL DERIVATIVE OF EXPONENTIAL FUNCTION
Aim:
To find the mixed partial derivative of exponential function
Algorithm:
Step1: Start the program
Step2:Import the python library sympy for symbolic mathematics
Step3:Define the variable and the exponential function
Step4:Find the derivative of the function with respect to x using sp.diff(h,x,y)
Step5:Find the derivative of the function with respect to y using sp.diff(h,y,x)
Step6: Print the result
RESULT:
Thus the program has been executed successfully.
48
Program:
import numpy as np
# Define matrices A and B
A = np.array([[2, 3], [4, 1]])
B = np.array([[-1, 5], [2, 0]])
Output:
Matrix A:
[[2 3]
[4 1]]
Matrix B:
[[-1 5]
[ 2 0]]
49
25. Matrix addition
Aim:
To find the matrix addition
Algorithm:
Step 1: start the program
Step 2: import the python library numpy for symbolic mathematics
Step3: Define the variable and algebraic function
Step4: Find the addition of two matrix.
Step 5: print the result
Step6 : End the program
Result:
The program has been executed successfully.
50
Program:
import numpy as np
# Define matrices A and B
A = np.array([[2, 3, 1], [4, 1, 5], [6, 2, 3]])
B = np.array([[1, 5, 2], [3, 2, 1], [0, 4, 2]])
Output:
Matrix A:
[[2 3 1]
[4 1 5]
[6 2 3]]
Matrix B:
[[1 5 2]
[3 2 1]
[0 4 2]]
51
26. Matrix subtraction:
Aim:
To find the matrix subtraction
Algorithm:
Step 1: start the program
Step 2: import the python library numpy for symbolic mathematics
Step 3: Define the variable and algebraic function
Step 4: Find the subtraction of two matrix.
Step 5: print the result
Step 6 : End the program
Result:
The program has been executed successfully.
52
Program:
import numpy as np
# Define matrices A and B
A = np.array([[2, 3], [4, 1]])
B = np.array([[5, 2], [1, 6]])
print("Matrix A:")
print(A)
print("\nMatrix B:")
print(B)
print("\nResult of A * B:")
print(C)
Output:
Matrix A:
[[2 3]
[4 1]]
Matrix B:
[[5 2]
[1 6]]
Result of A * B:
[[13 22]
[21 14]]
53
27. Matrix multiplication
Aim:
To find the matrix multiplication
Algorithm:
Step 1: start the program
Step 2: import the python library numpy for symbolic mathematics
Step3: Define the variable and algebraic function
Step4: Find the multiplication of two matrix.
Step 5: print the result
Step 6 : End the program
Result:
The program has been executed successfully.
54
Program:
import numpy as np
#Define the matrix
Matrix = np.array([
[2, 3, -1, 1],
[4, 5, 1, 2],
[6, 7, 2, 3]
], dtype=float)
# Perform row operation to get row echelon form
Matrix[1] -= 2 * Matrix[0]
Matrix[2] -= 3 * Matrix[0]
Matrix[2] -= 2 * Matrix[1]
Output:
55
28. Row echlon matrix
Aim:
To find the matrix of row echlon
Algorithm:
Step 1: start the program
Step 2: import the python library numpy for symbolic mathematics
Step3: Define the augumented matrix and perform row operation to get row echlon form.
Step4: write the coding to make the leading coefficient.
Step 5: print the result
Step6 : End the program
Result:
The program has been executed successfully.
56
Program:
def rk2_solver(f, y0, t0, tf, h):
# Runge-Kutta 2nd order method [By Bottom Science]
t = t0
y = y0
while t <= tf:
k1 = h * f(t, y)
k2 = h * f(t + h, y + k1)
y = y + (k1 + k2) / 2
t=t+h
return y
y0 = 1
t0 = 0
tf = 0.1
h = 0.1
y_sol = rk2_solver(f, y0, t0, tf, h)
print("Solution at t =", tf, "is", y_sol)
Output:
Solution at t=0.1 is : 1,24205
57
29. Runge Kutta Method Of Second Order
Aim:
To find the dy/dx=x+y by using Rungekutta method of second order for the values at t=0.1
Algorithm:
Step1:Start the program
Strp2: Define the function with the argument (f,y0,t0,tf,h)
Step3:Assign the values t0 and y0 to t and y
Step4:Using while loop find the values of k1,k2,y,t till t<=f
Step5:Return y
Step6:Define the function f(t,y) and return t+y
Step7:Assign the values of t0,y0,tf,h
Step8:Print the result
Step9:End the program
Result:
Thus the program has been executed successfully.
58