Professional Documents
Culture Documents
#Program
#Output
fact=1
i=1
while i<=n :
if i%2 == 0 :
p=p+q
print(i," st term Value : ",p)
fact = fact * i
i=i+1
else:
q=q+p
print(i," st term Value : ",q)
fact = fact * i
i=i+1
#Output
#Program Using Recursive
print("Name : nikhil\nUsn : 18BTRCE020")
def fact(n):
if n <= 1:
return n
else:
return n*fact(n-1)
if n <= 0:
print("Please enter a positive integer")
else:
print("Fibonacci sequence:")
print(Fib(n))
#Output
#Program using recursive and iteration for to print a
range of values
print("Fibonacci")
def Fib(n):
if n <= 1:
return n
else:
return (Fib(n - 1) + Fib(n - 2))
if n <= 0:
print("Please enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(n):
print("i"," term of : ",Fib(i))
#Output
3a)To print all prime numbers between N and M.
if(flag==1):
print(j)
//OUTPUT
3b)To find largest among three numbers, input by
user.
#large Number
print("Name: nikhil\nUsn: 18BTRCE020")
a=int(input("enter a value:"))
b=int(input("enter b value:"))
c=int(input("enter c value:"))
large = a
if(b>large):
large=b
if(c>large):
large=c
print(large," is a large")
//OUTPUT
3C)To find GCD for two numbers, input by user
#GCD
print("Name: nikhil\nUsn: 18BTRCE020")
a=int(input("enter divisor:"))
b=int(input("enter dividend:"))
while a!=b :
if(a>b):
a=a-b;
else:
b=b-a
print(a," is Your GCD");
//OUTPUT
4a)Elaborate string operations such as string
declaration, traversing, slicing, concatenating, and
sorting.
#String declaration
print("String declaration??")
s="nikhil"
#String traversing
print("String traversing??")
s="nikhil"
for i in range(len(s)):
print(s[0:i])
#String slicing
print("String slicing??")
print(s[2:5])
#String concatenation
print("String concatenation??")
s1="sai"
s2="nikhil"
s=s1+" "+s2
print(s)
#String sorting
print("String sorting??")
s="suresh"
print(sorted(s))
//OUTPUT
4b)Implement a python script to check the element is
in the list or not by using linear search and Binary
search.
print("name:nikhil\nusn:18BTRCE020")
#Binary Search
def binary_search(lst, key):
low = 0
high = len(lst) - 1
else:
high = mid - 1
# if we reach here the element is not found
return -1
lst = []
n = int(input("Enter the size of list:"))
for i in range(n):
num = int(input("Enter the any number: \t"))
lst.append(num)
key = int(input("\nEnter the number to search:"))
else:
print('{} was found at index {}'.format(key, index))
#Linear Search
lst=[]
n=int(input("Enter the size of list:"))
for i in range(n):
num = int(input("Enter the any number: \t"))
lst.append(num)
key = int(input("\nEnter the number to search:"))
found = False
for i in range(len(lst)):
if lst[i] == key:
found = True
print("\n{} found at index position
{}".format(key,i))
break
if found==False:
print("\n{} is not in list ".format(key))
//OUTPUT
print("name:nikhil\nUsn:18BTRCE020")
# performing the bubble sort operation
def bubble_sort(lst):
n = len(lst)
for i in range(n - 1):
for j in range(0, n - i - 1):
if lst[j] > lst[j + 1]:
lst[j], lst[j + 1] = lst[j + 1], lst[j]
lst = []
n = int(input("Enter the size of list:"))
for i in range(n):
num = int(input("Enter the any number: \t"))
lst.append(num)
bubble_sort(lst)
A = lst
# Traverse through all array elements
for i in range(len(A)):
//Output
# Python program for implementation of MergeSort
print("name:nikhil\nUsn:18BTRCE020")
def mergeSort(arr):
if len(arr) > 1:
mid = len(arr) // 2 # Finding the mid of the array
L = arr[:mid] # Dividing the array elements
R = arr[mid:] # into 2 halves
i=j=k=0
def printList(arr):
for i in range(len(arr)):
print(arr[i], end=" ")
print()
if __name__ == '__main__':
arr = [1,-10,100,11,-90]
print("Given array is", end="\n")
printList(arr)
mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)
//OUTPUT
def insertion_sort(lst):
j=i-1
lst[j + 1] = lst[j]
j=j-1
lst[j + 1] = temp
lst = []
for i in range(n):
lst.append(num)
for i in range(len(lst)):
print(lst[i], end='')
insertion_sort(lst)
for i in range(len(lst)):
print(lst[i], end='')
//OUTPUT
#Program
print("name:nikhil\nUsn:18BTRCE020")
#To perform mathematical operations such as
addition, subtraction, multiplication, division, modulo,
and power
num1 = int(input('Enter First number: '))
num2 = int(input('Enter Second number '))
add = num1 + num2
dif = num1 - num2
mul = num1 * num2
div = num1 / num2
floor_div = num1 // num2
power = num1 ** num2
modulus = num1 % num2
print('Sum of ',num1 ,'and' ,num2 ,'is :',add)
print('Difference of ',num1 ,'and' ,num2 ,'is :',dif)
print('Product of' ,num1 ,'and' ,num2 ,'is :',mul)
print('Division of ',num1 ,'and' ,num2 ,'is :',div)
print('Floor Division of
',num1 ,'and' ,num2 ,'is :',floor_div)
print('Exponent of ',num1 ,'and' ,num2 ,'is :',power)
print('Modulus of ',num1 ,'and' ,num2 ,'is :',modulus)
e = (a + b) * c / d
print ("Value of (a + b) * c / d is ", e)
e = ((a + b) * c) / d
print ("Value of ((a + b) * c) / d is ", e)
e = (a + b) * (c / d)
print ("Value of (a + b) * (c / d) is ", e)
e = a + (b * c) / d
print ("Value of a + (b * c) / d is ", e)
#logical operator
x = True
y = False
print('x or y is',x or y)
print('not x is',not x)
#Identity Operators
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is z)
print(x is y)
print(x == y)
print(x is not y)
#MemberShip Operator
x = ["apple", "banana"]
print("banana" in x)
print('banana' not in x)
#Output
5b)Write a program to calculate overtime pay of 10
employees. Overtime is paid at the rate of Rs.12.00
per hour for every hour worked above 40 hours.
Assume that employee do not work for fractional part
of an hour.
#program
print("name:nikhil\nusn:18BTRCE020")
x=1
working_hours=0
over_time=0
over_time_pay=0
#Output
c. For the given numpy array return array of odd rows and
even columns..
d) To add the two NumPy arrays and modify a result array by
calculating the square root of each element.
rectangle=plt.Rectangle((0,0),200,200,fc='r')
plt.gca().add_patch(rectangle)
rectangle=plt.Rectangle((50,50),100,100,fc='w')
plt.gca().add_patch(rectangle)
rectangle=plt.Rectangle((75,75),50,50,fc='r')
plt.gca().add_patch(rectangle)
plt.axis('scaled')
plt.show()
OUTPUT:
8b) Create a 5x5 rectangle whose top left
corner is at (row*5, col*5). If the sum of the
rows and columns’ number is even, set the fill
color of the rectangle to white, otherwise set it
to the black. Then draw the rectangle.
#lab program 8b
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
plt.figure()
plt.axis([0,25,25,0])
currentAxis = plt.gca()
for i in range(0,5):
for j in range(0,5):
if((i+j)%2==0):
currentAxis.add_patch(Rectangle((i*5,j*5),5,5,c
olor='white'))
else:
currentAxis.add_patch(Rectangle((i*5,j*5),5,5,c
olor='black'))
plt.show()
OUTPUT:
home: create urls.py
urlpatterns = [
path("",views.index,name='home'),
path("about",views.about,name='about'),
path("services",views.services,name="serv
ices"),
path("contact",views.contact,name="contac
t")
Views.py
def about(request):
return HttpResponse("This is about
page")
def services(request):
return HttpResponse("This is about
servivce page")
def contact(request):
return HttpResponse("This is about
contact page")
Templates
Index.html
Static
Create text.txt file (don’t keep sensitive data
file)
http://127.0.0.1:8000/
Index.html
<!DOCTYPE html>
<html>
<body>
<b>{{variable}}</b>
</body>
</html>
Views.py
from django.shortcuts import render,
HttpResponse
def about(request):
return HttpResponse("This is about
page")
def services(request):
return HttpResponse("This is about
servivce page")
def contact(request):
return HttpResponse("This is about
contact page")
Experiment-10
Create a "Student" JSON document with the following fields:
Usn_No, Name, Branch, Contact_Numbers and Mail_Id.
Perform read and write operations on the above document
using python API's.
dictionary={
"name":"sai nikhil",
"usn":"18BRCE020",
"BRANCH":"CSE-MACT",
"PHONE NUMBER":"7036082622",
"EMAIL":"18btrce020@jainuniversity.ac.in"
}
json_object = json.dumps(dictionary,index=5)
with open("sample.json","w")as outfile:
outfile.write(json_object)
import jason
with open('sample.jason','r')as openfile:
json_object = json.load(openfile)
print(jason_object)
print(type(jason_object))