CBSE Practical File
Computer Science with Python (083)
Class XII
Session: 2022-23
Date of submission: / /
Submitted by: Simran Kaur
(Class XII, Non-Med)
Supervised by: Mr. Prabhjot Singh
(PGT, Computer Science)
1|Page
Acknowledgement
I sincerely wish to thank Mrs. Shalini Khullar (Principal) for providing all the
necessary resources and facilities in the school without which it would not have been
possible to complete this practical file.
I also wish to thank our computer science teacher Mr. Prabhjot Singh for the
encouragement, guidance and support which help me to understand the concept of
programming and execute them in well manner to complete my practical file in due
time.
2|Page
Declaration
I hereby certify that this material, which I now submit for assessment on the program
of study leading to the award of class XII practical file is entirely my own work, that I
have exercised reasonable care to ensure that the work is original, and has not been
taken from the work of others and is to the best of my knowledge.
Signature: ___________________
Roll no: _______
Date: _______
3|Page
List of Contents
Sr.
Program Name Page no.
No.
1. Display the following code. 5-6
2. Input two numbers and display the greater number. 7
3. Input three numbers and display the greater number. 8
4. Input a number and check whether it’s a prime or composite. 9
5. Create a Fibonacci series. 10
Determine whether a number is a perfect number, an Armstrong
6. 11-12
number or a Palindrome.
7. Write a python program to implement a stack using list. 13-14
8. To find the area of triangle and rectangle. 15
9. Find factorial, Cube and square of a number. 16
10. Find compound interest. 17
11. Find length of a string. 18
Creating a list of tuples from given list having number and its
12. cube in each tuple. 19
13. Replace the string with another string. 20
Write a program to create empty tuple, tuple adding integers,
14. tuple with mixed data type and nested tuple. 21
Program for replace an item, check if a list item exists, remove
15. an item 22
in a list.mathematical calculator in python.
Simple
16. 23-24
Consider the following table FITNESS with the details about
17. fitness products being sold in the store. Write the commands for 25
(i) to (iv).
4|Page
Program 1: Input a welcome message and display it.
Input:
message=input("enter a welcome message:")
print("hello,",message)
Output:
5|Page
Program 2: Input two number and display the larger/ smaller number.
Input:
#input first number
num1=int(input("Enter First Number:"))
#input Second number
num2=int(input("Enter Second Number:"))
#Check if first number is greater than second
if (num1>num2):
print("The Larger number is:", num1)
else:
print ("The Larger number is:", num2)
Output :
Input:
#input first number
num1=int(input("Enter First Number:"))
#input Second number
num2=int(input("Enter Second Number:"))
#Check if first number is lesser than second
if (num1<num2):
print("The Smaller number is:", num1)
else:
print ("The Smaller number is:", num2)
6|Page
Output:
7|Page
Program 3: Input three number and display the larger/ smaller number.
Input:
#input first,Second and third number
num1=int(input("Enter First Number:"))
num2=int(input("Enter Second Number:"))
num3=int(input("Enter Third Number:"))
#Check if first number is greater than rest of the two numbers.
if (num1> num2 and num1> num3):
print("The Largest number is:", num1)
#Check if Second number is greater than rest of the two numbers.
elif (num2 > num1 and num2> num3):
print ("The Largest number is:", num2)
else:
print ("The Largest number is:", num3)
Output:
8|Page
Program 4: Generate the following pattern using nested loop.
Pattern 1:
*
**
***
****
*****
Input:
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
def pypart(n):
# outer loop to handle number of rows
# n in this case
for i in range(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ",end="")
# ending line after each row
print("\r")
# Driver Code
n=5
pypart(n)
Output:
9|Page
Pattern 2:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Input :
rows = int(input("Enter number of rows: "))
for i in range(rows, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print("\n")
Output :
10 | P a g e
Pattern 3:
A
A B
A B C
A B C D
A B C D E
Input:
for i in range(1,6):
for j in range(65,65+i):
a = chr(j)
print(a, end=" ")
print("\r")
output:
11 | P a g e
Program 5: Input a number and check if the number is prime or composite
number.
Input :
num = int(input("Enter any number : "))
if num > 1:
for i in range(2, num):
if (num % i) == 0:
print(num, "is NOT a PRIME number, it is a COMPOSITE number")
break
else:
print(num, "is a PRIME number")
elif num == 0 or 1:
print(num, "is a neither Prime NOR Composite number")
else:
print()
Output :
12 | P a g e
Program 6: Display the terms of a Fibonacci series Python Program .
Input:
# Program to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms ? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
13 | P a g e
Output :
14 | P a g e
Program 7: Determine the whether the number is a perfect number and
Armstrong number or a Palindrome number.
Input:
# Palindrome (12321 is Palindrome)
number=int(input("Enter any number:"))
num=number
num1= number
rev=0
while num>0:
digit=num%10
rev=rev*10+digit
num=int(num/10)
if number==rev:
print(number ,'is a Palindrome')
else:
print(number , 'Not a Palindrome')
# Armstrong number is equal to sum of cubes of each digit
sum = 0
temp = num1
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num1 == sum:
print(num1," is an Armstrong number")
else:
print(num1," is not an Armstrong number")
# Perfect number, a positive integer that is equal to the sum of its proper divisors.
sum1 = 0
for i in range(1, number):
if(number % i == 0):
sum1 = sum1 + i
if (sum1 == number):
print(number, " The number is a Perfect number")
15 | P a g e
else:
print(number, " The number is not a Perfect number")
Output:
16 | P a g e
Program 8: write a program to find the area of rectangle and area of triangle.
Input:
# define a function for calculating
# the area of a shapes
def calculate_area(name):\
# converting all characters
# into lower cases
name = name.lower()
# check for the conditions
if name == "rectangle":
l = int(input("Enter rectangle's length: "))
b = int(input("Enter rectangle's breadth: "))
# calculate area of rectangle
rect_area = l * b
print(f"The area of rectangle is{rect_area}.")
elif name == "triangle":
h = int(input("Enter triangle's height length: "))
b = int(input("Enter triangle's breadth length: "))
# calculate area of triangle
tri_area = 0.5 * b * h
print(f"The area of triangle is{tri_area}.")
# driver code
if __name__ == "__main__" :
print("Calculate Shape Area")
shape_name = input("Enter the name of shape whose area you want to find: ")
# function calling
calculate_area(shape_name)
Output :
17 | P a g e
18 | P a g e
Program 9: write a program to find the square root, cube root and factorial of a
number.
Input :
# calculate cube root
num=10
cuberoot =num**(1/3)
# disply the output
print("cube root of ",str(num),"is",str(cuberoot))
# print pogram to calculate the square root
num=8
# to take the input from the user
num=float(input('enter a number'))
num_sqrt=num**0.5
print('the square root of %0.3f is %0.3f'%(num, num_sqrt))
# python program to find the factoroial of a number
num=int(input('enter a number'))
factorial=1
# cheack if the number is positive, negative or zero
if num<0:
print("sorry factorial does not exist for negative number")
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 :
19 | P a g e
Program 10: write a program to find the compound interest.
Input :
#Python program to compute compound interest
20 | P a g e
p = float(input("Enter the principal amount : "))
t = float(input("Enter the number of years : "))
r = float(input("Enter the rate of interest : "))
#compute compound interest
ci = p * (pow((1 + r / 100), t))
#print
print("Compound interest : {}".format(ci))
output :
Program 11: write a program to find the length of string in python.
Input :
# User inputs the string and it gets stored in variable str
str = input("Enter a string: ")
# counter variable to count the character in a string
counter = 0
for s in str:
counter = counter+1
print("Length of the input string is:", counter)
output :
21 | P a g e
Program 12: python program to replace a string with another string.
Input :
string=input("Enter string:")
string=string.replace('a','$')
string=string.replace('A','$')
print("Modified string:")
print(string)
output :
22 | P a g e
Program 13: Python program to create empty tuple, tuple having integers, tuple
with mixed data types and nested tuple.
Input :
# Different types of tuples
# Empty tuple
my_tuple = ()
print(my_tuple)
# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple)
# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
output :
23 | P a g e
Program 14: python program to used nested for loop in python .
Input :
# Running outer loop from 2 to 3
for i in range(2, 4):
# Printing inside the outer loop
# Running inner loop from 1 to 10
for j in range(1, 11):
# Printing inside the inner loop
print(i, "*", j, "=", i*j)
# Printing inside the outer loop
print()
output :
24 | P a g e
Program 15: Python program to change the value of list item , check if a list item
exist and remove an item .
Input :
# Initializing list
test_list = [1, 6, 3, 5, 3, 4]
# Checking if 4 exists in list
for i in test_list:
if(i == 4):
25 | P a g e
print("Element Exists")
# Replace Values in a List using indexing
# define list
l = [ 'Hardik','Rohit', 'Rahul', 'Virat', 'Pant']
# replace first value
l[0] = 'Shardul'
# remove an item
print(l)
lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
'Lily', 'Carnations']
print("Original List is :", lst)
# using del statement
# to delete item (Orchids at index 1)
# from the list
del lst[1]
print("After deleting the item :", lst)
output :
26 | P a g e
27 | P a g e