0% found this document useful (0 votes)
19 views37 pages

Computer Science of Vaibhav Sengar

Uploaded by

workswitharnav
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)
19 views37 pages

Computer Science of Vaibhav Sengar

Uploaded by

workswitharnav
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/ 37

PRACTICAL FILE

COMPUTER SCIENCE (083)

PYTHON

Neetu Chauhan
(CLASS 11TH (B)
ACKNOWLEDGEMENT

I wish to express my deep sense of gratitude and indebtedness


to our learned teacher Mr. Adhayyan Morya , PGT COMPUTER
SCIENCE, Pt. SALAGRAM JR. HIGH SCHOOL for his invaluable
help, advice and guidance in the preparation of this practical
file.

I am also greatly indebted to our principal Mr. Mukesh Bhati


and school authorities for providing me with the facilities and
requisite laboratory conditions for making this practical file.

I also extended my thanks to a number of teachers, and myself


who helped me to complete this practical file successfully.

Neetu Chauhan
(Class 11th)
Certificate

This is to certify that


_______________________________________
Student of Class- XI Science has successfully
completed their Computer
Science (New - 083) Practical File.

Computer Teacher External Examiner


_______________ ______________

PRINCIPAL
______________________
INDEX
PRACTICAL FILE - COMPUTER SCIENCE (083)
LIST OF PRACTICALS (2024-25)
Programming Language: Python

S.NO. NAME OF PRACTICAL


1 Input any number from user and calculate
factorial of a number
2 Input any number from user and check it is Prime
no. or not
3 Write a program to find sum of elements of List
recursively

4 Write a program to calculate the nth term of


Fibonacci series
5 Program to search any word in given
string/sentence
6 Program to read and display file content line by
line with each word separated by “#”
7 Program to read the content of file and display the
total number of consonants, uppercase, vowels
and lower case characters
8 Program to create binary file to store Roll no and
Name, Search any Roll no and display name if Roll
no found otherwise “Roll no not found”
9 Program to create binary file to store Rollno,Name
and Marks and update marks of entered Roll no
10 Program to read the content of file line by line and
write it to another file except for the lines contains
“a” letter in it
Write a Python program to accepts two integers and
11 print their sum and show me output
Write a Python program that accepts radius of a circle
12 and prints its area.
Write a Python program to accept length and width of a
13 rectangle and compute its perimeter and area.
. Write a Python program to compute simple interest
14 for given Principal amount, time and rate of interest.
15 Write a Python program to generate prime
numbers for given range.
Program 1 : Input any number from user and
calculate factorial of a number

# Program to calculate factorial of entered


number
num = int(input("Enter any number :"))
fact = 1
n = num # storing num in n for printing
while num>1: # loop to iterate from n to 2
fact = fact * num
num-=1

print("Factorial of ", n , " is :",fact)


OUTPUT:

Enter any number : 6


Factorial of 6 is : 720
Program 2 : Input any number from user and
check it is Prime no. or not

#Program to input any number from user


#Check it is Prime number of not
import math
num = int(input("Enter any number :"))
isPrime=True
for i in range(2,int(math.sqrt(num))+1):
if num % i == 0:
isPrime=False
if isPrime:
print("## Number is Prime ##")
else:
print("## Number is not Prime ##")
OUTPUT :

Enter any number :117


## Number is not Prime ##
>>>
Enter any number :119
## Number is not Prime ##
>>>
Enter any number :113
## Number is Prime ##
>>>
Enter any number :7
## Number is Prime ##
>>>
Enter any number :19
## Number is Prime ##
Program 3 : Write a program to find sum of
elements of List recursively

#Program to find sum of elements of list recursively


def findSum(lst,num):
if num==0:
return 0
else:
return lst[num-1]+findSum(lst,num-1)

mylist = [] # Empty List


#Loop to input in list
num = int(input("Enter how many number :"))
for i in range(num):
n = int(input("Enter Element "+str(i+1)+":"))
mylist.append(n) #Adding number to list
sum = findSum(mylist,len(mylist))
print("Sum of List items ",mylist, " is :",sum)
OUTPUT:

Enter how many number :6


Enter Element 1:10
Enter Element 2:20
Enter Element 3:30
Enter Element 4:40
Enter Element 5:50
Enter Element 6:60
Sum of List items [10, 20, 30, 40, 50, 60] is : 210
Program 4 : Write a program to calculate
the nth term of Fibonacci series

#Program to find 'n'th term of fibonacci series


#Fibonacci series : 0,1,1,2,3,5,8,13,21,34,55,89,...
#nth term will be counted from 1 not 0

def nthfiboterm(n):
if n<=1:
return n
else:
return (nthfiboterm(n-1)+nthfiboterm(n-2))

num = int(input("Enter the 'n' term to find in fibonacci


:"))
term =nthfiboterm(num)
print(num,"th term of fibonacci series is :",term)
OUTPUT:
Enter the 'n' term to find in fibonacci :10
10 th term of fibonacci series is : 55
Program 5 : Program to search any word in
given string/sentence

#Program to find the occurence of any word in a string


def countWord(str1,word):
s = str1.split()
count=0
for w in s:
if w==word:
count+=1
return count

str1 = input("Enter any sentence :")


word = input("Enter word to search in sentence :")
count = countWord(str1,word)
if count==0:
print("## Sorry! ",word," not present ")
else:
print("## ",word," occurs ",count," times ## ")
OUTPUT:

Enter any sentence :my computer your computer our


computer everyones computer Enter word to search in
sentence :computer
## computer occurs 4 times ##

Enter any sentence :learning python is fun


Enter word to search in sentence :java
## Sorry! java not present
Program 6 : Program to read and display file content
line by line with each word separated by „#‟

#Program to read content of file line by line


#and display each word separated by '#

f = open("file1.txt")
for line in f:
words = line.split()
for w in words:
print(w+'#',end='')
print()
f.close()

NOTE : if the original content of file is:

India is my country
I love python
Python learning is fun
OUTPUT:
India#is#my#country#
I#love#python#
Python#learning#is#fun#
Program 7 : Program to read the content of file and
display the total number of consonants, uppercase,
vowels and lower case characters‟

#Program to read content of file


#and display total number of vowels, consonants, lowercase and uppercase
characters
f = open("file1.txt")
v=0
c=0
u=0
l=0
o=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1 if
ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' ' and ch!='\n':
o+=1
print("Total Vowels in file :",v)
print("Total Consonants in file n :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
print("Total Other than letters :",o)
f.close()
OUTPUT:

Total Vowels in file : 16


Total Consonants in file n : 30
Total Capital letters in file : 2
Total Small letters in file : 44
Total Other than letters : 4
Program 8 : Program to create binary file to store
Rollno and Name, Search any Rollno and display name
if Rollno found otherwise “Rollno not found”
#Program to create a binary file to store Rollno and name
#Search for Rollno and display record if found
#otherwise "Roll no. not found"

import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
student.append([roll,name])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close() f=open('student.dat','rb')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to search :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Search more ?(Y) :")
f.close()
OUTPUT:
Enter Roll Number :1
Enter Name :Amit
Add More ?(Y)y
Enter Roll Number :2
Enter Name :Jasbir
Add More ?(Y)y
Enter Roll Number :3
Enter Name :Vikral
Add More ?(Y)n
Enter Roll number to search :2
## Name is : Jasbir ##
Search more ?(Y) :y
Enter Roll number to search :1
## Name is : Amit ##
Search more ?(Y) :y
Enter Roll number to search :4
####Sorry! Roll number not found ####
Search more ?(Y) :n
Program 9 : Program to create binary file to
store Rollno,Name and Marks and update
marks of entered Rollno

#Program to create a binary file to store Rollno and name


#Search for Rollno and display record if found
#otherwise "Roll no. not found
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb+')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to update :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
print("## Current Marks is :",s[2]," ##")
m = int(input("Enter new marks :"))
s[2]=m
print("## Record Updated ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Update more ?(Y) :")
f.close()
OUTPUT:

Enter Roll Number :1


Enter Name :Amit
Enter Marks :99
Add More ?(Y)y
Enter Roll Number :2
Enter Name :Vikrant
Enter Marks :88
Add More ?(Y)y
Enter Roll Number :3
Enter Name :Nitin
Enter Marks :66
Add More ?(Y)n
Enter Roll number to update :2
## Name is : Vikrant ##
## Current Marks is : 88 ##
Enter new marks :90
## Record Updated ##
Update more ?(Y) :y
Enter Roll number to update :2
## Name is : Vikrant ##
## Current Marks is : 90 ##
Enter new marks :95
## Record Updated ##
Update more ?(Y) :n
Program 10 : Program to read the content of file line by
line and write it to another file except for the lines
contains „a‟ letter in it.

#Program to read line from file and write it to another


line
#Except for those line which contains letter 'a

f1 = open("file2.txt")
f2 = open("file2copy.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print(“## File Copied Successfully! ##”)
f1.close()
f2.close()
NOTE: Content of file2.txt
a quick brown fox
one two three four
five six seven
India is my country
eight nine ten
bye!
OUTPUT:

## File Copied Successfully! ##


NOTE: After copy content of file2copy.txt
one two three four
five six seven
eight nine ten
bye!
Program 11 : Write a Python program to accepts
two integers and print their sum

# Python program to accept two integers and print their


sum

# Accept two integers from the user


num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

# Calculate their sum


result = num1 + num2

# Print the result


print("The sum of", num1, "and", num2, "is:", result)
OUTPUT:

The sum of 7 and 5 is: 12


Program 12 : Write a Python program that accepts
radius of a circle and prints its area.

import math

# Accept radius from the user


radius = float(input("Enter the radius of the circle: "))

# Calculate the area


area = math.pi * (radius ** 2)

# Print the area


print("The area of the circle is:", area)
OUTPUT:

The area of the circle is: 153.93804002589985


Program 13 : Write a Python program to accept
length and width of a rectangle and compute its
perimeter and area.

# Accept length and width of the rectangle


length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

# Calculate perimeter and area


perimeter = 2 * (length + width)
area = length * width

# Display the results


print("The perimeter of the rectangle is:", perimeter)
print("The area of the rectangle is:", area)
OUTPUT:

The perimeter of the rectangle is: 30.0


The area of the rectangle is: 50.0
Program 14 : . Write a Python program to compute
simple interest for given Principal amount, time and
rate of interest.

# Accept Principal, Time, and Rate of Interest


principal = float(input("Enter the Principal amount: "))
time = float(input("Enter the Time (in years): "))
rate = float(input("Enter the Rate of Interest: "))

# Calculate simple interest


simple_interest = (principal * time * rate) / 100

# Display the result


print("The Simple Interest is:", simple_interest)
OUTPUT:

The Simple Interest is: 100.0


Program 15 : Write a Python program to generate
prime numbers for given range.

# Function to check if a number is prime


def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

# Accept the range from the user


start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))

# Generate prime numbers in the given range


prime_numbers = [num for num in range(start, end + 1) if is_prime(num)]

# Display the result


print("Prime numbers in the range", start, "to", end, "are:", prime_numbers)
OUTPUT:

Prime numbers in the range 10 to 30 are: [11, 13, 17, 19,


23, 29]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy