0% found this document useful (0 votes)
50 views10 pages

PP - Practical

The document contains answers to questions related to Python programming. It includes programs to: 1) Print twin primes less than 1100 using prime checking and generation of twin numbers. 2) Print pairs of amicable numbers in a given range by calculating sum of factors. 3) Demonstrate tuple operations like concatenation, slicing and deletion. 4) Implement data visualization techniques like histogram, box plot, and scatter plot using Matplotlib library. 5) Define a Student class to get student details like roll number, name, class from user. 6) Plot the function y=x^2 using Matplotlib.

Uploaded by

Anmol Rai
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)
50 views10 pages

PP - Practical

The document contains answers to questions related to Python programming. It includes programs to: 1) Print twin primes less than 1100 using prime checking and generation of twin numbers. 2) Print pairs of amicable numbers in a given range by calculating sum of factors. 3) Demonstrate tuple operations like concatenation, slicing and deletion. 4) Implement data visualization techniques like histogram, box plot, and scatter plot using Matplotlib library. 5) Define a Student class to get student details like roll number, name, class from user. 6) Plot the function y=x^2 using Matplotlib.

Uploaded by

Anmol Rai
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/ 10

D20MCA11140

Ques. 1: Write a program to print twin primes less than 1100.


Ans. 1:

Program:

def is_prime(n):

for i in range(2, n):

if n % i == 0:

return False

return True

def generate_twins(start, end):

for i in range(start, end):

j=i+2

if(is_prime(i) and is_prime(j)):

print("({:d} , {:d})".format(i, j))

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 = []

for i in range(1, int(n**0.5) + 1):

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

Output : Enter the Highest Number of Range: 100000

{(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)

Tuple2 = ('Prerna', 'MCA', '3rd Sem')

Tuple3 = Tuple1 + Tuple2

print("Tuple 1: ")

print(Tuple1)

print("\nTuple2: ")

print(Tuple2)

print("\nTuples after Concatenation: ")

print(Tuple3)

Output :
Tuple 1:

(0, 1, 2, 3)

Tuple2:

('Prerna', 'MCA', '3rd Sem')

Tuples after Concatenation:

(0, 1, 2, 3, 'Prerna', 'MCA', '3rd Sem')

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

print(Tuple1 + "Deleted Successfully")

Output :
('Robert', 'Carlos', '1965', 'Terminator 1995', 'Actor', 'Florida')

Traceback (most recent call last):

File "D:/Python/deletetuple.py", line 4, in <module>

print(Tuple1 + "Deleted Successfully")

NameError: name 'Tuple1' is not defined

5
D20MCA11140

Ques. 4: Write a program to implement data visualization on dataset in following modules: -


histogram, box plot chart and scatter plot.
Ans. 4:
Histogram
Step 1: Install the Matplotlib package

If you haven’t already done so, install the Matplotlib package using the following command (under
Windows):
Pip install matplotlib

Step 2: Collect the data for the histogram

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

Step 3: Determine the number of bins

Next, determine the number of bins to be used for the histogram.

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.

Step 4: Plot the histogram in Python using matplotlib

You’ll now be able to plot the histogram based on the template that you saw at the beginning of this guide:

import matplotlib.pyplot as plt

x = [value1, value2, value3,....]

plt.hist(x, bins = number of bins)

plt.show()

6
D20MCA11140

And for our example, this is the complete Python code after applying the above template:

import matplotlib.pyplot as plt

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

Run the code, and you’ll get the histogram below:

7
D20MCA11140

Box Plot Chart Output:


# Import libraries

import matplotlib.pyplot as plt

import numpy as np

# Creating dataset

np.random.seed(10)

data = np.random.normal(100, 20, 200)

fig = plt.figure(figsize =(10, 7))

# Creating plot

plt.boxplot(data)

# show plot

plt.show()

Scatter Plot Output:

import matplotlib.pyplot as plt

x =[5, 7, 8, 7, 2, 17, 2, 9,

4, 11, 12, 9, 6]

y =[99, 86, 87, 88, 100, 86,

103, 87, 94, 78, 77, 85, 86]

plt.scatter(x, y, c ="blue")

# To show the plot

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

self.Rollno=input("Enter Roll Number : ")

self.Name = input("Enter Name : ")

self.Class =input("Enter Class : ")

S1=Student()

S1.getStudentDetails()

Output:
Enter Roll Number : D20MCA11043

Enter Name : Prerna

Enter Class : MCA

9
D20MCA11140

Ques. 7: Write a program to plot the function y = x2 using the matplotlib libraries.
Ans. 7:

import matplotlib.pyplot as plt

x_cords = range(-50,50)

y_cords = [x*x for x in x_cords]

plt.plot(x_cords, y_cords)

Output:

10

You might also like