Python Lab Manual
Python Lab Manual
22PLC15B
Python installation
Open https://www.anaconda.com/products/individual in your browser and scroll
down to find the below installers:
Install the latest version of Anaconda. Make sure to download the installer
based on your computer specification.
For Windows:
Download 64-bit or 32 bit according to your system
After downloading installer, we open it and begin with the installation
process as follows:
1. Getting Started
4. Loading Packages:
5. Finished Installation:
6. Launching Jupyter:
Now start Python Program coding
Programming Exercises:
2.a)Develop a program to generate Fibonacci sequence of length (N). Read N from the console
numerator = fact
sub = n-r
fact = i = 1
while i<=sub:
fact = i*fact
i += 1
denominator = fact
fact = i = 1
while i<=r:
fact = i*fact
i += 1
denominator = fact*denominator
comb = numerator/denominator
print("\nCombination (nCr) =", comb)
3.Read N numbers from the console and create a list. Develop a program to print mean, variance
and standard deviation with suitable messages.
import numpy as np #for declaring an array or simply use list
def mean(data):
n = len(data)
mean = sum(data) / n
return mean
def variance(data):
n = len(data)
mean = sum(data) / n
deviations = [(x - mean) ** 2 for x in data]
variance = sum(deviations) / n
return variance
def stdev(data):
import math
var = variance(data)
std_dev = math.sqrt(var)
return std_dev
data = np.array([7,5,4,9,12,45])
n = len(list1)
res = linear_Search(list1, n, key)
if(res == -1):
print("Element not found")
else:
print("Element found at index: ", res)
4.b) Write a program to search an element using binary search.
def binary_search(list1, n):
low = 0
high = len(list1) - 1
mid = 0
# Initial list1
list1 = [12, 24, 32, 39, 45, 50, 54]
n = 45
# Function call
result = binary_search(list1, n)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in list1")