Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Introduction to Python Programming

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

2. Getting through the License Agreement:


3. Select Installation Type: Select Just Me (if you want the software to be used by a single
User)

4. Choose Installation Location:


5. Advanced Installation Option:

6. Getting through the Installation Process:


7. Finishing up the Installation:

For Mac OS Click on 64-Bit Graphical Installer (462 MB)

And follow same method.

Now we install the Jupyter notebook:

Installing Jupyter Notebook using Anaconda:


To install Jupyter using Anaconda, we follow the following steps

1. Launch Anaconda Navigator:


2. Click on the Install Jupyter Notebook Button:
3. Beginning the Installation:

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

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
2.b)Write a function to calculate factorial of a number. Develop a program to compute binomial
coefficient (Given N and R).

print("Enter the Value of n: ", end="")


n = int(input())
print("Enter the Value of r: ", end="")
r = int(input())
fact = i = 1
while i<=n:
fact = i*fact
i += 1

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])

print("Standard Deviation of the sample is % s "% (stdev(data)))


print("Mean of the sample is % s " % (mean(data)))
4.a) Write a program to search an element using linear search.

def linear_Search(list1, n, key):


# Searching list1 sequentially
for i in range(0, n):
if (list1[i] == key):
return i
return -1
list1 = [1 ,3, 5, 4, 7, 9]
key = 7

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

while low <= high:


# for get integer result
mid = (high + low) // 2

# Check if n is present at mid


if list1[mid] < n:
low = mid + 1

# If n is greater, compare to the right of mid


elif list1[mid] > n:
high = mid - 1

# If n is smaller, compared to the left of mid


else:
return mid

# element was not present in the list, return -1


return -1

# 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")

You might also like