Mathematics Practical
Mathematics Practical
(Using Python)
B.Sc. (Mathematics) Semester-I
Some Basic Codes and their Results in Python
Numbers & Operators:
print(1+2) 3
print(5-2) 3
print(2*2) 4
print(2**3) 8
print(5/2) 2.5
print(5//2) 2
print(5%2) 1
Text/Strings:
print(“hello world”) hello world
print(“what’s up”) what’s up
Comments:
# is used to comment
Variables:
x=2
print(x) 2
x = "hello world"
print(x) hello world
x = "hello world"
del x
print(x) NameError:name 'x' is not defined
Conditional logic:
print(5==4) False
print(5!=4) True
Symbol Operation Examples
== Equal to 2==2 True
!= Not equal to 4!=4 False
< Less than 3<5 True
> Greater than 3>5 False
<= Less or equal to 3<=5 True
>= Greater or equal to 2>=3 False
def plot_line(a):
x = range(-10, 11) # Define x range
y = [a * xi for xi in x] # Calculate y values
(ii) f(x)=[x]
Code:
import matplotlib.pyplot as plt
import numpy as np
# Generate x values
x_values = np.linspace(-5, 5, 1000) # Choose the range of x values
(iii) f(x)=x^{2n}
Code:
def plot_function(n):
x = np.linspace(-2, 2, 1000) # Define x values
y = x**(2*n) # Calculate y values
(iv) f(x)=x^{2n-1}
Code:
def plot_function(n):
x = np.linspace(-2, 2, 1000) # Define x values
y = x**(2*n-1) # Calculate y values
(v) f(x)=1/x^{2n}
Code:
def plot_function(n):
x = np.linspace(0.1, 2, 1000) # Define x values, avoiding division by zero
y = 1 / x**(2*n) # Calculate y values
(vi) f(x)=1/x^{2n-1}
Code:
def plot_function(n):
x = np.linspace(-2, 2, 1000) # Define x values
y = 1 / x**(2*n-1) # Calculate y values