PP - Practical
PP - Practical
PP - Practical
Program:
def is_prime(n):
if n % i == 0:
return False
return True
j=i+2
generate_twins(1, 1100)
1
D20MCA11140
Output :
(1 , 3)
(3 , 5)
(5 , 7)
(11 , 13)
(17 , 19)
(29 , 31)
(41 , 43)
(59 , 61)
(71 , 73)
(101 , 103)
(107 , 109)
(137 , 139)
(149 , 151)
(179 , 181)
(191 , 193)
(197 , 199)
(227 , 229)
(239 , 241)
(269 , 271)
(281 , 283)
(311 , 313)
(347 , 349)
(419 , 421)
(431 , 433)
(461 , 463)
(521 , 523)
(569 , 571)
(599 , 601)
(617 , 619)
(641 , 643)
(659 , 661)
(809 , 811)
(821 , 823)
(827 , 829)
(857 , 859)
(881 , 883)
(1019 , 1021)
(1031 , 1033)
(1049 , 1051)
(1061 , 1063)
(1091 , 1093)
2
D20MCA11140
Ques. 2: Write a program to print pairs of amicable numbers in a range given at runtime.
Ans. 2:
Program:
def sum_factors(n):
result = []
if n % i == 0:
result.extend([i, n//i])
return sum(set(result)-set([n]))
def amicable_pair(number):
result = []
for x in range(1,number+1):
y = sum_factors(x)
if sum_factors(y) == x and x != y:
result.append(tuple(sorted((x,y))))
return set(result)
print (amicable_pair(10000))
{(10744, 10856), (17296, 18416), (1184, 1210), (63020, 76084), (66928, 66992),
(220, 284), (5020, 5564), (67095, 71145), (79750, 88730), (6232, 6368), (12285,
14595), (2620, 2924), (69615, 87633)}
3
D20MCA11140
Ques. 3: Write a program to demonstrate following operations on tuples - adding tuple, slicing
tuple, and deleting tuple.
Ans. 3:
Adding Tuple:
Program:
Tuple1 = (0, 1, 2, 3)
print("Tuple 1: ")
print(Tuple1)
print("\nTuple2: ")
print(Tuple2)
print(Tuple3)
Output :
Tuple 1:
(0, 1, 2, 3)
Tuple2:
4
D20MCA11140
Slicing Tuple:
Program:
Tuple1 = ('Robert', 'Carlos','1965','Terminator 1995', 'Actor','Florida')
print(Tuple1[2:4])
Output :
('1965', 'Terminator 1995')
Deleting Tuple:
Program:
Tuple1 = ('Robert', 'Carlos','1965','Terminator 1995', 'Actor','Florida')
print(Tuple1)
del Tuple1
Output :
('Robert', 'Carlos', '1965', 'Terminator 1995', 'Actor', 'Florida')
5
D20MCA11140
If you haven’t already done so, install the Matplotlib package using the following command (under
Windows):
Pip install matplotlib
For example, let’s say that you have the following data about the age of 100 individuals:
Age
1,1,2,3,3,5,7,8,9,10,10,11,11,13,13,15,16,17,18,18,18,19,20,21,21,
23,24,24,25,25,25,25,26,26,26,27,27,27,27,27,29,30,30,31,33,34,3
4,34,35,36,36,37,37,38,38,39,40,41,41,42,43,44,45,45,46,47,48,48
,49,50,51,52,53,54,55,55,56,57,58,60,61,63,64,65,66,68,70,71,72,
74,75,77,81,83,84,87,89,90,90,91
For simplicity, let’s set the number of bins to 10. At the end of this guide, I’ll show you another way to
derive the bins.
You’ll now be able to plot the histogram based on the template that you saw at the beginning of this guide:
plt.show()
6
D20MCA11140
And for our example, this is the complete Python code after applying the above template:
x=
[1,1,2,3,3,5,7,8,9,10,10,11,11,13,13,15,16,17,18,18,18,19,20,21,21,23,24
,24,25,25,25,25,26,26,26,27,27,27,27,27, 29,30,30,31,33,34,34,34,35,36,
36,37,37,38,38,39,40,41,41,42,43,44,45,45,46,47,48,48,49,50,51,52,53,5
4,55,55,56,57,58,60,61,63,64,65,66,68,70,71,72,74,75,77,81,83,84,87,89
,90,90,91]
plt.hist(x, bins=10)
plt.show()
7
D20MCA11140
import numpy as np
# Creating dataset
np.random.seed(10)
# Creating plot
plt.boxplot(data)
# show plot
plt.show()
x =[5, 7, 8, 7, 2, 17, 2, 9,
4, 11, 12, 9, 6]
plt.scatter(x, y, c ="blue")
plt.show()
8
D20MCA11140
Ques. 6: Write a program to implement a student class with information such as rollno, name,
class. the information must be entered by the user.
Ans. 6:
class Student:
def getStudentDetails(self):
S1=Student()
S1.getStudentDetails()
Output:
Enter Roll Number : D20MCA11043
9
D20MCA11140
Ques. 7: Write a program to plot the function y = x2 using the matplotlib libraries.
Ans. 7:
x_cords = range(-50,50)
plt.plot(x_cords, y_cords)
Output:
10