0% found this document useful (0 votes)
9 views16 pages

Document (1) (3) - KSH

The document contains a series of Python programming exercises for a 10th-grade class, covering topics such as list operations, statistical calculations using NumPy, data visualization with Matplotlib, file handling with CSV, and basic algorithm implementations. Each exercise includes a brief description, code snippets, and expected output. The exercises aim to enhance students' programming skills and understanding of Python libraries.

Uploaded by

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

Document (1) (3) - KSH

The document contains a series of Python programming exercises for a 10th-grade class, covering topics such as list operations, statistical calculations using NumPy, data visualization with Matplotlib, file handling with CSV, and basic algorithm implementations. Each exercise includes a brief description, code snippets, and expected output. The exercises aim to enhance students' programming skills and understanding of Python libraries.

Uploaded by

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

PYTHON

January 1
PROGRAM
S 2025

NAME : KUSH MEHRA A.I.


CLASS : 10th ROSE
PRACTICA
ROLL NO. : 14
SUBJECT TEACHER : Ms. ASHA
L FILE
1.Write a program to add the elements of the two lists.
31-132.45,403
22-135,30,42,391
print("*** List 81
print (51, len (S1))
print("*** List S2)
print (82, len (82))
All-81-52
print("** List All ****
print (All, len (All))
Output:
List S1
(32, 45, 40) 3
**List 52
(35, 30, 42, 38] 4
List All**
(32, 45, 40, 35, 30, 42, 38] 7

2. Write a program to calculate mean, median and mode


using NumPy of the following list of distance travelled by
a car in a week:
[95,90,49,71,90,100,55]
Ans import statistics
d [95,90,49,71,90,100,55)
statistica.mean (d)
m2 statistics.median (d)
m3 statistics.mode(d)
print("The mean is: ", ml)
print("The median is: ", m2)
print("The mode is: ", m3)
Output:
The mean is: 78.57142857142857
The median is: 90
The mode is: 90
Write a program to display line chart from (2,5) to (9,10).
Output
Ans import matplotlib.pyplot as plt
import numpy as np
#Deline X and Y variable data
X=np-array([2,3,9,10))
Y=x×2
plt.plot(x, y)
plt.xlabel("X-axis") add X-axis label
plt.ylabel("Y-axis"). #add Y-axis label plt.title("Line
Chart") # add title plt.show()
3.Write a program to display a scatter chart for the
following points (2,5), (9,10), (8,3),(5,7),(6,18).
Ans. Import matplotlib.pyplot as pit
import numpy as np
(2,5), (9,10), (8,3), (5,7), (6,18)
x= np.array([2,9,8,5,6])
ynp.array([(5,10,3,7,18))
plt.xlabel("X-axis") add X-axis label
plt.ylabel("Y-axis") add Y-axis label
plt.title("Scatter Chart") # add title
plt.scatter(x, y)
plt.show()

5. Read csv file saved in your system and display 10


rows.
Ans import esv
With open(‘CLASS.CSV’, ‘a’, newline=””) as CF:
Cwcsv.writer (CF)
Cw.writerow([‘Acim No’, ‘Name’, ‘Percentage’])
Cw.writerow ([111, ‘AmanVerma’, 871)
Cw.writerow([152, ‘Vartika Sen’, 891)
CW.writerow([123, ‘Shipra Virmani’,801)
CW.writerow ([214, ‘Jyoti Batra’,781)
CW.writerow ([167, ‘Ayana Joshi’,92])
CW.writerow ([142, ‘Ekta Mishra’, 761)
Cw.writerow([216, ‘Mansi Kapoor’, 721)
CW.writerow([147, ‘ShivaniMonga’, 811)
CW.writerow([181, ‘Poonam Gupta’, 67])
with open (“CLASS.CSV”, “r”) as CF:
CV csv.reader (cF, delimiter=’,’)
For c in CV:
Print ©
Output:
[‘Adm No’, ‘Name’, ‘Percentage’]
[‘111’, ‘AmanVerma’, ‘87’]
[‘152’, ‘Vartika Sen’, ‘89’]
[‘123’, ‘ShipraVirmani’, ‘80’]
[‘214’, ‘JyotiBatra’, ‘78’]

(‘Adm No’, ‘Name’, ‘Percentage’]


(‘111’, ‘AmanVerma’, ‘87’]5. Read csv file saved in your
system and display 10 rows.
1152. Vartika Sen', 89'
[123. ShipraVirmani, 80
[214, JyotiBatra'. 178]
[167, Ayana Joshi', '927
[142', 'Ekta Mishra, 76]
[216", "Mansi Kapoor', '72]
[147", "ShivaniMonga', '81']
[181", "Poonam Gupta', '67']

6. Read csv file saved in your system and display its


information.
Ans. Smportcav
with open ("Books.csv", "w") as CF:
cwcsv.writer (CF)
cw.writerow('Bookno', 'Bookname', 'Price"])
CW.writerow( 'B101', 'The Kite Runner', 5001)
cW.writerow(['B102', 'Julius Ceasar', 250])
cw.writerow(['B103', 'Fault in our Stars',6501)
ow.writerow(['B111', 'Freedom Fighters', 1100])
try:
with open ("Books.csv", "r") as CF:
CR-csv.reader (CF)
for L in CR:
print (L)
except FileNotFoundError:
print ("File Not Found")
Output:
['Bookno', 'Bookname', 'Price']
['B101', 'The Kite Runner', '500']
['B102', 'Julius Ceasar', '250']
['B103', 'Fault in our Stars', '650']
['B111', 'Freedom Fighters', '1100']

7. Write a program to read an image and display using


Python library: import cv2, matplotlib Upload an image of
your favourite city in the world on the screen and give a
proper title to it.

Ans. Program:
import cv2
from matplotlib import pyplot as pit
import numpy as np
img= cv2.imread('D:\London.jpg')
plt.imshow(img)
Plecitier My Favourite City’
Plt.axis(‘off’)
Pit.show
Output:

8.Write a program to read an image and identify its


shape using Python.
Ans import cv2
From matplotlib import pyplot as plt
Import numpy as np
Image cv2.imread(‘D:\Panda.jpg’)
Height, width, channels image.snape
Print(“Image shape:”)
Print(“lleight:”, height)
Print(“Width:”, width)
Print(“Number of channels:”, channels)
Output

9.vowels and somenesWite a program te input a string


and display the count of
Ans Program
ati
conaanantsP for i in REET it in metoUAE TOU vowels-1
consonants
print("the number of vowelsr". vowelsi
print("The number of consonants", consonants)
Output
Please enter a string as you wish: Orange Education
The number of vowels: 8
The number of consonants: 8

10. Write a program to input a string and display the


string in the reverse order
Ans. Program:
detreverse_string(str):
stri for i in atrrstrl 1 atrl
return strl
str "Artificial Intelligence"
print("The original string is: "str)
print("The reverse string is", reverse string (str))
Output:
The original string is: ArtificialIntelligence
The reverse string is ecnegilletnllaicifitrA

11. Write a Python code to take the input of a number n


and then find and display its factorial (n). For ange
515x4x3x2x1 i.e., 120.
Ans. Program:
int (input("Enter a number: "))
factorial 1
if n < 0:
print(“Factorial does not exist for negative numbers”)
n=0
print(“The factorial of O is 1”)
aise:
for I in range(I,n)
factorial factoriali
print(“The factorial of “in,” is”, factorial)
Output:
Enter a number: 7
The factorial of 7 is 5040

12.Write a Python code to input the lengths of the three


sides of a triangle and display whether a triangle can be
formed with the inputs or not. If a triangle can be formed
then display whether the triangle will be scalene,
isosceles or equilateral triangle.
Program:
Print(“Input the sides of the triangle: “)
Aint(input(“A: “))
Bint(input(“B: “))
C = (input(“C: “))
If ABC
Print(“Equilateral triangle”)
Elif A ==B or B = aC or lambda == mathbb C
Print(“isosceles triangle”)
Else:
Print(“Scalene triangle”)
Output:
Input the sides of the triangle.
A 10
8. 10
C: 10
Equilateral triangle

13. Write a program to input two numbers and display


the LCM of the two numbers
Example
Ans. Program:
Def calculate_lcm (x, y) :
If x >y: greater = x
Else:
Greater-y
While (True):
If(greaterx0) and (greatery-01):
Ics greater
Denak
Greater
Return icm
Nuelinttinput(“Enter first number: “))
NumInt (input(“Enter second number: “))
Print (“The L.C.M. of”, numi. “and”, num2, “is”,
calculate_icm(numi, num2))
Output:
Enter first number: 5
Enter second number: 6
The LC.M. of 5 and 6 is 30

14. Write a program to input a number and display


whether it is a palindrome or not.
Ans. Program:
Num-int(input(“Enter a number:”))
Temp-num
Rev-0
While (num>0):
Dig-num 10 zev-rev 10-dig num-num//10
If (temp—rev):
Print(“The number is a palindrome!”)
Else:
Print(“The number is not a palindrome!”)
Output:
Enter a number:1002001
The number is a palindrome!

15. Write a program to input a number and display


whether it is a prime number or not.
Ans. Program:
Numint (input(“Enter a number: “))
If num> 1:
For I in range(2, int (num/2)+1);
If num1)0:
Print(num, is not prime)
Break
Else:
Printínum, “is a prime number”)
else
print thum, "is a composite number")
Output:
Enter a number: 12
12 is not a prime number

16. Write a program to input two numbers and swap both


the numbers without using the third number.
Ans. Program:
x = int (input ("Enter the value of x: "))
y = 1 int (input("Enter the value of y: ")) print ("Numbers
before swapping: d x - x + y print("Numbers after
swapping: Ad \d\n" h (x, y) ) d\n" (x,y))
y-x-y
x-x-y
Output:
Enter the value of * 22
Enter the value of * 14
Numbers before swapping: 22 14
Numbers after swapping: 14 22

17. Using Numpy Package:


Create a 4 * 2 array with random integer.
Create 3 * 3 array with all zeros.
Ans. Program:
Import numpy as np
arraylnp.random.randint(0, 10, (4, 2))
print("4x2 Array with Random Integers:")
print (array1)
import numpy as np
array2 np.zeros((3, 3))
print("\n3x3 Array with Zeros:")
print(array2)
Output:
4x2 Array with Random Integers:
[126]
1777
[65]
19
3x3 Array with Zeros:
[10.0.0.1
10.0.0.1
10.0.0]

18. Using Matplotlib and the given data, plot a bar chart
No of people voted (23.45.31.40.35)
Area covered = [‘al,a2, a3 a4 a51
Ans. Program:
Import matplotlib.pyplot as pit
No_of people voted – 123,45,31,40,351
Pit.bar(‘a’,’a’,’3’, ‘a’, ‘a’), No_of_people_voted)
Pit.title(‘Bar Chart”)
Plt.ylabel(‘No of people voted’)
Pit.show()
Output:

19. Using Matplotlib and the given data, plot a line chart:
X-axis [3,4,6,2,81
Y-axis
Ans. Program:
19,10,8,7,61
Import matplotlib.pyplot as pit
Import numpy as np
Xnp.array([3,4,6,2,8))
Ynp.array([9,10,8,7,611
Plt.plot(x, y)
Plt.show()

20. Using Matplotlib and the given data, Plot a pie chart:
Data (45,23,41,78,651
Ans. Program:
From matplotlib import pyplot as pit
Import numpy as np
Fig plt.figure()
Axfig.add_axes ((0,0,1,1])
Ax.axisf’equal’)
Data (45,23,41,78,651
Ax.pie (Data, autopot=’41.21%)
Pit.show()
Output:

You might also like