[go: up one dir, main page]

0% found this document useful (0 votes)
17 views20 pages

Cs File Kchi - Merged

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)
17 views20 pages

Cs File Kchi - Merged

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/ 20

INDEX

1. Creating a python program to generate random


number between 1 to 6
2. Creating a python program to read a text file line by
line and display each word separated by '#'.
3. Creating a python program to read a text file and
display the number of vowels/consonants/lower case/
upper case characters
4. Creating python program to display short words
from a text file
5. Creating a python program to create and
update/modify records in binary file
6. Creating a menu driven program to perform
arithmetic operations
7. Creating a python program to display Fibonacci
series
8. Creating a menu driven program to find factorial
and sum of list of numbers using function
9. Creating a python program to implement returning
value(s) from function.
10.Creating a python program to implement
mathematical functions.
DATE :

PRACTICAL NO-1
CREATING A PYTHON PROGRAM TO GENERATE RANDOM

BETWEEN 1 TO 6
AIM :
To write a python program to generate random number between 1 ton
6 to simulate dice.
SOURCE CODE :

>>> import random

>>> while True:

... Choice=input("\nDo you want to roll the dice(y/n);")

... no=random.randint(1,6)

... if Choice=='y':

... print("nYour Number is:",no)

... else:

... break

RESULT :

Thus, the above python program has been executed and the
output is verified successfully.
SAMPLE OUTPUT :
PYTHON EXECUTED OUTPUT PROGRAM :

Do you want to roll the


dice(y/n);y nYour Number is: 6
Do you want to roll the
dice(y/n);y nYour Number is: 4
Do you want to roll the
dice(y/n);y nYour Number is: 2
Do you want to roll the dice(y/n);n
DATE :

PRACTICAL NO- 2

CREATING A PYTHON PROGRAM TO READ A TEXT FILE


LINE BY LINE AND DISPLAY EACH WORD SEPARATED BY '#'
AIM :

To write a Python Program to Read a text file "Story.txt" line by


line and display each word separated by '#'.

SOURCE CODE :
>>> f=open(“story.txt”,’r’)

>>> Contents=f.readlines()

>>> for line in Contents:

... words=line.spilt()

... for i in words :

... print(i+’#’,end=’ ‘)

... print(“ “)

>>> f.close()

RESULT :

Thus, the above Python program has been executed and the
output is verified successfully

SAMPLE OUTPUT :
STORY.TXT :

Like a Joy on the heart of a


sorrow. The sunset hangs on a
cloud.
PYTHON PROGRAM EXECUTED OUTPUT :

Like# a# Joy# on# the# heart# of# a# sorrow.#


The# sunset# hangs# on# a# cloud.#

DATE :

PRACTICAL NO- 3

CREATING A PYTHON PROGRAM TO READ A TEXT FILE AND


DISPLAY THE NUMBER OF VOWELS/CONSONANTS/LOWER CASE/
UPPER CASE CHARACTERS.
AIM :

To write a Python Program to read a text file "Story.txt" and


displays the number of Vowels/ Consonants/ Lowercase /
Uppercase/characters in the file.

SOURCE CODE :
>>> f=open("story.txt",'r')

>>> Content=f.read()

>>> Vowels=0

>>> Consolants=0

>>> Lower_case=0

>>> Upper_case=0

>>> for ch in Contents:

... if ch in'aeiouAEIOU':

... Vowels=Vowels+1

... if ch in'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':

... Consolants=Consolants+1

... if ch.islower():

... Lower_case=Lower_case+1

... if ch.isupper():
... Upper_case=Upper_case+1

>>> f.close()

>>> print("The total numbers of vowels in the file:",Vowels)

>>> print("The total numbers of consolants in the file:",Consolants)

>>> print("The total numbers of uppercase in the file:",Upper_case)

>>> print("The total numbers of lowercase in the file:",Lower_case)

RESULT :

Thus, the above Python program has been executed and

the output is verified successfully.

SAMPLE OUTPUT :
STORY.TXT:

Like a Joy on the heart of a


sorrow. The sunset hangs on a
cloud.
PYTHON PROGRAM EXECUTED OUTPUT:
The total numbers of vowels in the file: 0
The total numbers of consolants in the
file: 0 The total numbers of uppercase in
the file: 0 The total numbers of
lowercase in the file: 0

DATE :

PRACTICAL NO- 4

CREATING PYTHON PROGRAM TO DISPLAY SHORT

WORDS FROM A TEXT FILE


AIM :
To Write a method Disp() in Python, to read the lines
from poem.txt and display those words which are less than 5
characters.
SOURCE CODE :
>>> def Disp():

... F=open("poem.txt")

... S=F.read()

... W=S.spilt()

... print("The following words are less than 5 characters")

... for i in W:

... if len(i)<5:

... print(i,end='')

... F.close()

>>> Disp()

RESULT :

Thus, the above Python program has been executed and the
output is verified successfully.
SAMPLE OUTPUT :
POEM.TXT

Dancing rays of light,


Painting colors on the
sky, Nature’s
masterpiece.
PYTHON EXECUTED PROGRAM OUTPUT :

The following words are less than 5


characters Rays of on the sky,

DATE :

PRACTICAL NO – 5
CREATING A PYTHON PROGRAM TO CREATE AND UPDATE/MODIFY

RECORDS IN BINARY FILE

AIM :
To write a Python Program to Create a binary file with roll number,
name, mark and update/modify the mark for a given roll number
SOURCE CODE :
Type "help", "copyright", "credits" or "license()" for more
information. import pickle
def Create():
F=open("Students.dat",'a
b') opt='y'
while opt=='y':

Roll_No=int(input('Enter roll
number:')) Name=input("Enter
Name:") L=[Roll_No,Name]
pickle.dump(L,F)

opt=input("Do you want to add another student


detail(y/n):") F.close
...

...

... def Search():

... F=open("Students.dat",'rb')

... no=int(input("Enter Roll.No of student to search:"))

... found=0

... try:

... while True:

... S=pickle.load(F)

... if S[0]==no:

... print("The searched Roll.No is found and Details are:",S)

... found=1
... break

... except:

... F.close()

...

...

... def Search():

... F=open("Students.dat",'rb')

... no=int(input("Enter Roll.No of student to search:"))

... found=0

... try:

... while True:

... S=pickle.load(F)

... if S[0]==no:

... print("The searched Roll.No is found and Details are:",S)

... found=1

... break

... except:

... F.close()

... if found==0 :

... print(“The Searched Roll.No is not found”)

>>> #Main program

... Create()

... Update()

RESULT :

Thus, the above Python program has been executed and the
output is verified successfully.

SAMPLE OUTPUT :
PYTHON PROGRAM EXECUTED OUTPUT :

Enter roll number:1


Enter Name: Arun
Enter Marks : 450
Do you want to add another student detail
(y/n): y Enter roll number:2
Enter Name: Bala
Enter Marks : 342
Do you want to add another student detail
(y/n): y Enter roll number:3
Enter Name:
Charan Enter Marks
: 423
Do you want to add another student detail
(y/n): y Enter roll number:4
Enter Name:
Dinesh Enter Marks
: 356
Do you want to add another student detail
(y/n): y Enter roll number:5
Enter Name: Divya
Enter Marks : 476
Do you want to add another student detail
(y/n): n Enter Student Roll.No to modify
marks😊
The searched Roll.No is found and Details are: [3, ‘Charan’ ,
423] Enter New Mark to be update : 470

Mark updated Successfully and Details are : [3, ‘Charan’ , 470]

PRACTICAL NO: 6

CREATING A MENU DRIVEN PROGRAM TO PERFORM ARITHMETIC


OPERATIONS

AIM: To write a menu driven Python Program to perform Arithmetic operations (+,-*,/)
based on the user’s choice.

SOURCE CODE:

print("1. Adddition")
print("2. Subtraction")
print("3.
Multiplication")
print("4. Division")
opt=int(input("Enter your choice:"))
a=int(input("Enter the First Number:"))
b=int(input("Enter the Second
Number:")) if opt==1:
c=a+b

print("The Addition of two number


is:",c) elif opt==2:
c=a-b

print("The Subtraction of two number


is:",c) elif opt==3:
c=a*b

print("The Multiplication of two number


is:",c) elif opt==4:
if b==0:

print("Enter any other number other than 0")

else:

else:

c=a/b

print("The Division of two number is:",c)

print("Invalid Option")

Result:
Thus, the above Python program has been executed and the output is verified successfully.

Sample Output:

Python Program Executed Output:


• Addd
ition

• Subtr
action

• Multiplication

• Division

Enter your

choice:3

Enter the First Number:10


Enter the Second
Number:23
The Multiplication of two number is: 230

PRACTICAL NO: 7
DATE:

CREATING A PYTHON PROGRAM TO DISPLAY FIBONACCI SERIES

AIM: To write a Python Program to display Fibonacci Series up to ‘n’ numbers.

SOURCE CODE:

First=0
Second
=1
no=int(input("How many Fibonacci numbers you want
to display?")) if no<=0:
print("Please Enter Positive Integer")

else:

print (First) print


(Second)
for i in range(2, no):
Third=First+Second
First=Second Second=Third
print (Third)

Result:
Thus, the above Python program has been executed and the output is verified successfully.

SAMPLE OUTPUT:

Python Executed Program Output:

How many Fibonacci numbers you want to display?5

3
PRACTICAL NO: 8
DATE:

CREATING A MENU DRIVEN PROGRAM TO FIND FACTORIAL AND SUM OF LIST OF


NUMBERS USING FUNCTION

AIM: To write a menu driven Python Program to find Factorial and sum of list of numbers
using function.

SOURCE
CODE:

def Factorial(no):

F=1

if no<0:

print("Sorry, we cannot take Factorial for


Negative number") elif no==0:
print("The Factorial of 0 is 1")

else:

for i in
range(1,no+1)
: F=F*i
print("The Factorial of", no, "is:",F)

def Sum_List(L):

Sum=0

for i in range(n):

Sum=Sum+L[i]

print("The Sum of List is:",


Sum) #Main Program
print("1. To Find Factorial")

print("2. To Find sum of List


elements") opt=int(input("Enter
your choice:"))
if opt==1:
n=int(input("Enter a number to find
Factorial:")) Factorial(n)
elif opt==2:

L=[]

n=int(input("Enter how many elements you want to store in


List?:")) for i in range(n):
ele=int(input
())
L.append(ele
)
Sum_List(L)

Result:
Thus, the above Python program has been executed and the output is verified successfully.
Sample Output:

Python Executed Program Output:

• To Find Factorial

• To Find sum of
List elements Enter your
choice:1
Enter a number to find
Factorial:7 The Factorial of 7 is:
5040
Practical No:9
DATE:

CREATING A PYTHON PROGRAM TO IMPLEMENT RETURNING


VALUE(S) FROM FUNCTION

AIM: To Write a Python program to define the function Check(no1,no2) that take two numbers
and Returns the number that has minimum ones digit.

Source Code:

def Check(no1,no2):
if (no1%10) <
(no2%10): return
no1
else:
return no2

a=int(input("Enter the First number:"))


b=int(input("Enter the Second
number:"))

r=Check(a,b)

print("The Number that has minimum one's digit is:",r)

Result:
Thus, the above Python program has been executed and the output is verified successfully.

Sample Output:

Enter the First


number:1000 Enter the
Second number:12
The Number that has minimum one's digit is: 1000
PRACTICAL NO: 10
DATE:

CREATING A PYTHON PROGRAM TO IMPLEMENT MATHEMATICAL FUNCTIONS

AIM: To write a Python program to implement python mathematical functions to find:


• To find Square of a Number.
• To find Log of a Number(i.e. Log10)
• To find Quad of a Number

Source Code:

import math
def Square(num):
S=math.pow(num,
2) return S
def Log(num):
S=math.log10
(num) return S
def Quad(X,Y):
S=math.sqrt (X**2 +
Y**2) return S
print("The Square of a Number is:", Square
(7)) print("The Log of a Number is:", Log
(100)) print("The Quad of a Number
is:", Quad (2,2))

Result:
Thus, the above Python program has been executed and the output is verified successfully.

Sample Output:

Python Executed Program Output:

The Square of a Number is:

49.0 The Log of a Number is:


2.0
The Quad of a Number is: 2.8284271247461903

You might also like