0% found this document useful (0 votes)
20 views3 pages

Slip 1

The document contains Python programs to evaluate mathematical expressions, repeat strings, generate squares of numbers, construct identity, zero and ones matrices, find prime numbers between ranges, and estimate integrals using Simpson's 1/3rd rule and Trapezoidal rule.

Uploaded by

Dipesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Slip 1

The document contains Python programs to evaluate mathematical expressions, repeat strings, generate squares of numbers, construct identity, zero and ones matrices, find prime numbers between ranges, and estimate integrals using Simpson's 1/3rd rule and Trapezoidal rule.

Uploaded by

Dipesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#Q.

1 Python program to evaluate the following expression

a=20%2+7-(3+7)*20/2
print(a)

b=30*10//3+10%3
print(b)

c=2**5-2**4+4//4
print(c)

Q. 1 (2) Python program to repeat following string 9 times using the string
operator '*'
s1="Python "
s2=s1*9
print(s2)

s1="Mathematics "
s2=s1*9
print(s2)

#Q.1 (3) python program to generate square of numbers from 1 to 10


sqr_list = []
for i in range(1,11):
sqr_list.append(i*i)
print(*sqr_list)

#Q.2 (1)using python construct identity and zero matrix and ones matrix
import numpy as np
a=np.identity(10)
b=np.zeros([7,3])
c=np.ones([5,4])
print(a,b,c)

#Q.2 (2) Generate all the prime numbers between 1 to 100 using Python code.

for Number in range (1, 101):


count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break

if (count == 0 and Number != 1):


print(" %d" %Number, end = ' ')

#Q.3 (1) Q.Write Python program to estimate the value of the integral fo sin(x)dx
using Simp#son's (1/3)rd rule (n=6).

# Define function to integrate


import math as mt
def f(x):
return mt.sin(x)

# Implementing Simpson's 1/3


def simpson13(x0,xn,n):
# calculating step size
h = (xn - x0) / n

# Finding sum
integration = f(x0) + f(xn)

for i in range(1,n):
k = x0 + i*h

if i%2 == 0:
integration = integration + 2 * f(k)
else:
integration = integration + 4 * f(k)

# Finding final integration value


integration = integration * h/3

return integration

# Input section
lower_limit = float(input("Enter lower limit of integration: "))
upper_limit = float(input("Enter upper limit of integration: "))
sub_interval = int(input("Enter number of sub intervals: "))

# Call simpson() method and get result


result = simpson13(lower_limit, upper_limit, sub_interval)
print("Integration result by Simpson's 1/3 method is: %0.6f" % (result) )

#Q.3 (b) (2) write the python program to estimate the value of integral 1/(1+x)
using trapezoidal
rule (n=8)

# Define function to integrate


def f(x):
return 1/(1 + x)

# Implementing trapezoidal method


def trapezoidal(x0,xn,n):
# calculating step size
h = (xn - x0) / n

# Finding sum
integration = f(x0) + f(xn)

for i in range(1,n):
k = x0 + i*h
integration = integration + 2 * f(k)

# Finding final integration value


integration = integration * h/2

return integration

# Input section
lower_limit = float(input("Enter lower limit of integration: "))
upper_limit = float(input("Enter upper limit of integration: "))
sub_interval = int(input("Enter number of sub intervals: "))
# Call trapezoidal() method and get result
result = trapezoidal(lower_limit, upper_limit, sub_interval)
print("Integration result by Trapezoidal method is: %0.6f" % (result) )

You might also like