0% found this document useful (0 votes)
13 views4 pages

App Lab Expt 3

The document contains Python code snippets for various tasks, including printing prime numbers, finding factors of a number, reading from a file, checking for palindromes, and implementing the Collatz conjecture. It also includes a function for calculating a normal distribution and visualizing it using Matplotlib. Additionally, there is a mention of creating a matrix with random numbers using NumPy.

Uploaded by

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

App Lab Expt 3

The document contains Python code snippets for various tasks, including printing prime numbers, finding factors of a number, reading from a file, checking for palindromes, and implementing the Collatz conjecture. It also includes a function for calculating a normal distribution and visualizing it using Matplotlib. Additionally, there is a mention of creating a matrix with random numbers using NumPy.

Uploaded by

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

b) Printing all the primes less than a given number

n=int(input("please enter the no to see prime no"))

for i in range (1, n+1):

count = 0

for j in range(1, i + 1):

if(i % j == 0):

count = count + 1

if (count == 2):

print(i, end = ' ')

c) Finding all the factors of a number and show whether it is a perfect number, i.e.,
the sum of all its factors (excluding the number itself) is equal to the number itself
n = int(input("Enter any number: "))
sum = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum + i
if (sum == n):
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")

3. Defining and Using Functions


a) Write a function to read data from a file and display it on the screen

def readfile(str):
file=open(str,'r')
line=file.readline()
while(line!=""):
print(line)
line=file.readline()
file.close()
str=input("Enter the name of the file with .txt extension:")
readfile(str) # function calling

output:
D:\Sree>python file1.py
Enter the name of the file with .txt extension:file.txt
Hello srikanth b
Welcome to tkrcet
All the best for future

b) Define a boolean function is palindrome


def isPalindrome(s):
return s == s[::-1]
s = input("please enter a string: ")
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")

output:
D:\Sree>python poli.py
please enter a string: madam
Yes
D:\Sree>python poli.py
please enter a string: sir
No

D:\Sree>python poli.py
please enter a string: level
Yes

c) Write a function collatz(x) which does the following: if x is odd, x = 3x + 1; if x is even, then x = x/2.
Return the number of steps it takes for x = 1
def collatz(n):
while n > 1:
print(n, end=' ')
if (n % 2):
# n is odd
n = 3*n + 1
else:
# n is even
n = n//2
print(1, end='')
n = int(input('Enter n: '))
print('Sequence: ', end='')
collatz(n)

output:
D:\Sree>python collatz.py
Enter n: 10
Sequence: 10 5 16 8 4 2 1
D:\Sree>python collatz.py
Enter n: 50
Sequence: 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
D:\Sree>python collatz.py
Enter n: 25
Sequence: 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

d) Write a function N(m, s) = exp(-(x-


import numpy as np
import matplotlib.pyplot as plt

# Creating a series of data of in range of 1-50.


x = np.linspace(1,20,50)

#Creating a Function.
def normal_dist(x , mean , sd):
prob_density = (np.pi*sd) * np.exp(-0.5*((x-mean)/sd)**2)
return prob_density
#Calculate mean and Standard deviation.
mean = np.mean(x)
sd = np.std(x)
print("mean is : ",mean)
print("standard deviation is : ",sd)
#Apply function to the data.
pdf = normal_dist(x,mean,sd)
print("pdf is : ",pdf)

#Plotting the Results


plt.plot(x,pdf , color = 'red')
plt.xlabel('Data points')
plt.ylabel('Probability Density')
plt.show()

output:

mean is : 10.5
standard deviation is : 5.59564334905254
pdf is : [ 4.16011092 4.66826665 5.21339885 5.79429765 6.40907276 7.05511596
7.72907751 8.42685887 9.14362342 9.87382657 10.61126621 11.3491537
12.08020482 12.79674954 13.49085873 14.15448497 14.77961431 15.35842504
15.88344912 16.34773173 16.74498415 17.06972528 17.31740742 17.48452225
17.56868344 17.56868344 17.48452225 17.31740742 17.06972528 16.74498415
16.34773173 15.88344912 15.35842504 14.77961431 14.15448497 13.49085873
12.79674954 12.08020482 11.3491537 10.61126621 9.87382657 9.14362342
8.42685887 7.72907751 7.05511596 6.40907276 5.79429765 5.21339885
4.66826665 4.16011092]

4. The package numpy

a) Creating a matrix of given order m x n containing random numbers in the range 1 to 99999

You might also like