0% found this document useful (0 votes)
397 views9 pages

Python Practical Programs for Class IX

The document contains a series of Python practical programs for Class IX, covering various topics such as arithmetic operations, calculating area and perimeter, converting units, and string operations. Each program includes sample code, expected output, and confirms successful execution. The programs aim to enhance students' understanding of Python programming through practical applications.

Uploaded by

areebwais
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)
397 views9 pages

Python Practical Programs for Class IX

The document contains a series of Python practical programs for Class IX, covering various topics such as arithmetic operations, calculating area and perimeter, converting units, and string operations. Each program includes sample code, expected output, and confirms successful execution. The programs aim to enhance students' understanding of Python programming through practical applications.

Uploaded by

areebwais
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

ACADEMIC YEAR 24-25

PYTHON PRACTICAL PROGRAMS


CLASS IX

# 1. To find the square of number


n=int(input("Enter any number"))
square= n * n
print (" The square value of n is",square)

Output:
Enter any number 6
The square value of n is 36

Result: Thus the above program has been executed successfully.

# 2. To Perform Arithmetic Operations:


a=int(input("Enter first number"))
b=int(input("Enter second number"))
s= a+b
M= a*b
d= a/b
fl=a//b
r=a%b
p=a ** b
print(" The Sum is",s)
print(" The Multiply is",M)
print(" The Division is",d)
print(" The floor division is",fl)
print(" The remainder is",r)
print(" The power is",p)

Output:
Enter first number 4
Enter second number 2
The Sum is 6
The Multiply is 8
The Division is 2.0
The floor division is 2
The remainder is 0
The power is 16

Result: Thus the above program has been executed successfully.


# [Link] print the table of 10 terms
n= int(input("No of Terms"))
for i in range(1,n):
print (n,"x",i,"=", n*i)

Output:
No of Terms 6
6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30

Result: Thus the above program has been executed successfully.

#4. To calculate Simple interest:


amount = float(input("Principal Amount"))
time = int(input("Duration"))
rate = float(input("Interest Rate"))
simple_interest = (amount * time * rate) / 100
print("Total interest you have to pay: ", simple_interest)

Output:
Principal Amount 50000
Duration 3
Interest Rate 3
Total interest you have to pay: 4500.0

Result: Thus the above program has been executed successfully.

#5. To convert given kilometer into Meter


km = float(input("Enter distance in kilometer: "))
m = km * 1000;
print ("%0.3f Kilometer = %0.3f Meter" %(km,m))

Output:
Enter distance in kilometer: 12
12.000 Kilometer = 12000.000 Meter

Result: Thus the above program has been executed successfully.


#6. To Find Area and Perimeter of a Rectangle
length = int(input('Length : '))
width = int(input('Width : '))
area = length * width
perimeter= 2 * (length + width)
print (“Area of the Rectangle:”, area)
print (“Perimeter of the Rectangle : “,perimeter)

Output:
Length : 8
Width : 5
Area of the Rectangle: 40
Perimeter of the Rectangle : 26

Result: Thus the above program has been executed successfully.

# 7. To calculate Area of a triangle with Base and Height


base = float (input ('Please Enter the Base of a Triangle: '))
height = float (input ('Please Enter the Height of a Triangle: '))
# calculate the area
area = (base * height) / 2
print ("The Area of a Triangle using", base, "and", height, " = ", area)

Output:
Please Enter the Base of a Triangle: 7
Please Enter the Height of a Triangle: 6
The Area of a Triangle using 7.0 and 6.0 = 21.0

Result: Thus the above program has been executed successfully.


# 8. Python Program to Calculate Total Marks Percentage and Grade of a Student
print ("Enter the marks of five subjects::")
s1 = int (input ("Enter subject1 Mark"))
s2 = int (input ("Enter subject2 Mark"))
s3 = int (input ("Enter subject3 Mark"))
s4 = int (input ("Enter subject4 Mark"))
s5 = int (input ("Enter subject5 Mark"))
total, average, percentage, grade = None, None, None, None
total = s1 + s2 + s3 + s4 + s5
average = total / 5.0
percentage = (total / 500.0) * 100
if average >= 90:
grade = 'A'
elif average >= 80 and average < 90:
grade = 'B'
elif average >= 70 and average < 80:
grade = 'C'
elif average >= 60 and average < 70:
grade = 'D'
else:
grade = 'E'
print ("\nThe Total marks is: \t", total, "/ 500.00")
print ("\nThe Average marks is: \t", average)
print ("\nThe Percentage is: \t", percentage, "%")
print ("\nThe Grade is: \t", grade)

Output:
Enter the marks of five subjects:
Enter subject1 Mark 89
Enter subject2 Mark 87
Enter subject3 Mark 76
Enter subject4 Mark 98
Enter subject5 Mark 90
The Total marks is: 440 / 500.00
The Average marks is: 88.0
The Percentage is: 88.0 %
The Grade is: B

Result: Thus the above program has been executed successfully.


# 9. Python program to find the largest number among the three input numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)

Output:
Enter first number: 8
Enter second number: 9
Enter third number: 10
The largest number is 10.0

Result: Thus the above program has been executed successfully.

# 10 To Check if a Number is Positive, Negative or 0


num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")

Output:
Enter a number: 8
Positive number
Enter a number: -7
Negative number

Result: Thus the above program has been executed successfully.


# 11. Python program to find the factorial of a number provided by the user.
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Output:
Enter a number: 5
The factorial of 5 is 120

Result: Thus the above program has been executed successfully.

12. # Python program for String Functions:


s = "Strings are awesome!"
# Length should be 20
print("Length of s = %d",len(s))

# First occurrence of "a" should be at index 8


print("The first occurrence of the letter a = %d", [Link]("a"))

# Number of a's should be 2


print("a occurs %d times" , [Link]("a"))

# Slicing the string into bits


print("The first five characters are %s”, s[:5]) # Start to 5
print("The next five characters are '%s'" , s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" , s[12]) # Just number 12
print("The characters with odd index are '%s'" ,s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" , s[-5:]) # 5th-from-last to end

# Convert everything to uppercase


print("String in uppercase: %s" , [Link]())

# Convert everything to lowercase


print("String in lowercase: %s" , [Link]())
# Check how a string starts and ends
s="welcome to python"
[Link]("wel")
print(s)
[Link]("on")
print(s)

# Split the string into three separate strings, each containing only a word
print("Split the words of the string: %s" % [Link](" "))

Output:

Length of s = 20
The first occurrence of the letter a = 8
a occurs 2 times
The first five characters are 'Strin'
The next five characters are 'gs ar'
The thirteenth character is 'a'
The characters with odd index are 'tig r wsm!'
The last five characters are 'some!'
String in uppercase: STRINGS ARE AWESOME!
String in lowercase: strings are awesome!
welcome to python
welcome to python
Split the words of the string: ['welcome', 'to', 'python']

Result: Thus the above program has been executed successfully.

# 13. Python program for String operations:


str1 = "Hello, world!" # compare str1 and str2
str2 = "I love Swift."
str3 = "Hello, world!"
print(str1 == str2)
print(str1 == str3)
greet = "Hello" # join string using + operator
name = "Jack"
result = greet+name
print(result)
result1= greet*5 # Copy/Replication of string using * operator.
print(result1)
for letter in greet: # iterating through greet string
print(letter)
print(len(greet)) # count length of greet string
print('a' in 'program') # String Membership
print('at' not in 'battle')
Output:
False
True
HelloJack
HelloHelloHelloHelloHello
H
e
l
l
o
5
True
False
Result: Thus the above program has been executed successfully.

14. # Python program for List operations:


list1 = [11, 5, 17, 18, 23]
print(list1)
print(list1[2])
print(list1[4])
print (list1[: :])
print(list1[: :-1])
print(list1[-2])
print(list1[-2])

[Link](9) #Append an element to the list


print(list1[: :])

print( “The Length of List”, len(list1)) # Length of the list


for ele in range(0, len(list1)):
total = total + list1[ele]
print("Sum of all elements in given list: ", total)

[Link]() #Deleting last element from a list


print (L)

list3=["one",8,10,"three"]
list4=["two","five",3.5,2]
[Link](list4) #Extend the list
print(list1)

print(max(list1)
print(max(list1)

Output:

[11, 5, 17, 18, 23]


17
23
[11, 5, 17, 18, 23]
[23, 18, 17, 5, 11]
18
5
[11, 5, 17, 18, 23, 9]
The Length of List 6
Sum of all elements in given list: 83
[11, 5, 17, 18, 23]
['one', 8, 10, 'three', 'two', 'five', 3.5, 2]
23
5

Result: Thus the above program has been executed successfully.

Common questions

Powered by AI

The conversion from kilometers to meters uses multiplication because there are 1,000 meters in a kilometer. This conversion simplifies the process by multiplying the kilometer input by 1,000 to convert to meters, effectively scaling the unit as required .

The program calculates the total and average of the marks from five subjects and uses conditional statements to assign a grade. For example, an average of 90 or above gets an 'A', 80 to under 90 gets 'B', continuing this pattern down to 'E' for averages under 60. This structured logic converts numerical performance into qualitative grades .

The program takes two integers as input and performs several arithmetic operations: addition (a+b), multiplication (a*b), division (a/b), floor division (a//b), modulus (a%b), and exponentiation (a**b). Each operation has a corresponding print statement to display the result .

The string operations include slicing, which extracts specific parts of the string, as seen in operations like s[:5] or s[5:10] to get substrings . Splitting divides a string into a list of substrings based on a separator, for example, using s.split(' ') results in ['welcome', 'to', 'python']. Transformations like s.upper() and s.lower() convert the entire string to uppercase and lowercase, respectively, changing the case of the characters within the string .

The program compares the three inputs using conditional statements. It first checks if the first number is greater than or equal to the other two. If true, the first number is identified as the largest. Otherwise, it checks if the second number is greater than or equal to the others, and determines it as the largest if true. If both previous conditions fail, the third number is the largest .

Defining a variable as 'float' provides a way to handle real numbers, offering precision for decimal values. This is crucial in financial calculations like simple interest, where fractional amounts may occur, and in unit conversions where precise measurements are needed .

The Python program calculates factorial by initializing a 'factorial' variable to 1 and multiplying it by each number in the range from 1 to num inclusively. This iterative approach accumulates products until the final factorial value is reached. Factorials are significant in computing for permutations, combinations, and in various algorithmic processes that require scalable computation handling .

The list operations illustrated include accessing elements, reversing the list, appending elements, and extending the list with another list. Accessing is done with index positions, reversing with slicing [::-1], appending with list1.append(), and extending with list3.extend(list4). These modify the list by changing the sequence and adding new elements, affecting both content and order .

The Python program calculates the area of a triangle using the formula area = (base * height) / 2. This calculation requires input for the base and height of the triangle, which the program multiplies together and then divides by 2 to get the area .

The program first checks if the number is greater than or equal to zero. If true, it checks further if the number equals zero to print 'Zero'; otherwise, it outputs 'Positive number'. If the initial condition is false, indicating the number is less than zero, it outputs 'Negative number' .

You might also like