0% found this document useful (0 votes)
88 views34 pages

Lakshya Rautela 12-B CS Pratical File

The document contains 30 Python programs with their summaries and code. The programs cover a range of concepts in Python including finding the maximum number in a list, checking voter eligibility, grading a student based on percentage, finding factors of a number, sorting lists, reading and writing to files, and more. The Python code provided implements these concepts to demonstrate practical Python programming.

Uploaded by

lakshya rautela
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)
88 views34 pages

Lakshya Rautela 12-B CS Pratical File

The document contains 30 Python programs with their summaries and code. The programs cover a range of concepts in Python including finding the maximum number in a list, checking voter eligibility, grading a student based on percentage, finding factors of a number, sorting lists, reading and writing to files, and more. The Python code provided implements these concepts to demonstrate practical Python programming.

Uploaded by

lakshya rautela
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/ 34

COMPUTER SCIENCE

Practical file

By Lakshya Rautela
(12-B)
PROGRA PROGRAM
M NO.
1 Python program to find maximum number
2 Python program to check whether a person is eligible to
vote or not.
3 A program to accept percentage of a student and display
its grade.
4 Python program to find factor of a number
5 Python program to print names of languages python,
java, ruby
6 Python program to print sum of numbers from 1 to 4
7 Python program to find area of a circle
8 Python program to convert temperature in Fahrenheit to
Celsius
9 Python program to multiply 2 complex numbers
10 Python program to display multiplication table of a
number
11 Python program to replace “ “ with “-“
12 Floyd’s triangle
13 Python program to convert Decimal to Binary , Octal ,
Hexadecimal
14 Python program to sort a given list
15 Python program to count the frequency of given element
in list
16 Python program to read a file line by line and print it
17 Python program to remove all the lines that contain the
character “a” in a file and write it to another file
18 Python program to read a text file and display the
number of vowels/consonants/uppercase/lowercase
characters in the file
19 Program to read data from data file in read mode and
count the particular word occurrences in given string
20 Python program to read a text file and display each word
separated by a #
21 Python program to write a random number generator
that generates random numbers between 1 and 6
(simulates a dice)
22 Python program to write a python program to implement
a stack using a list data structure
23 Python program to recursively find the factorial of a
natural number
24 Python program to read lines from a text file python to
read lines from a text file STORY.TXT, and display those
words, which are less than 4 characters
25 Program to enter few lines of text file
26 Program to calculate arc length of an angle
27 Python program to print number pyramid pattern
28 Python program to separate odd and even indexed
elements
29 Python program to repeat each element k times in a list
30 Python program to repeat tuple n times
PROGRAM 1
#Python program to find maximum number
x=float(input("1st Number: "))
y=float(input("2nd Number: "))
z=float(input("3rd Number: "))
if x > y and x > z:
maximum = x
elif y >x and y > z:
maximum = y
else:
maximum=z
print(maximum)
PROGRAM 2
#Python program to check whether a person is eligible to vote or not.
age=int(input("Enter age of a person:"))
if age>=18:
print ("He is eligible to vote")
else:
print("He is not eligible to vote")
PROGRAM 3
# A program to accept percentage of a student and display its grade.
perc=float(input('Enter percentage of student:'))
if perc>85:
print('A')
elif perc>70 and perc<=85:
print('B')
elif perc>60 and perc<=70:
print('C')
elif perc>45 and perc<=60:
print('D')
else:
print('E')
PROGRAM 4
#Python program to find factor of a number
n = int(input( " Enter a number to find its factor : "))
print(1, end=' ')
factor = 2
while factor <= n/2 :
if n% factor ==0 :
print(factor, end=' ')
factor += 1
print (n, end=' ')
PROGRAM 5
#Python program to print names of languages python, java, ruby
language=['Python' , 'Java' , 'Ruby']
for lang in language:
print("Current language is: " , lang)
PROGRAM 6

#Python program to print sum of no.s from 1 to 4

number=5
sum=0
i=0
while(i<number):
sum=sum+i
i=i+1
print(sum)
PROGRAM 7
# Python program to find area of a circle

def findArea(r):
PI = 3.142
return PI * (r*r);

# Driver method
print("Area is %.6f" % findArea(5));
PROGRAM 8
#Python program to convert temperature in Fahrenheit to Celsius

Fahrenheit=float(input("Enter temperature in fahreheit:"))


Celsius = ((Fahrenheit-32)*5)/9
print("Temperature in Celsius is: ")
print(Celsius)
PROGRAM 9
#Python program to multiply 2 complex no.s

def mulComplex( z1, z2):


return z1*z2

# driver code
z1 = complex(2, 3)
z2 = complex(4, 5)

print("Multiplication is :", mulComplex(z1,z2))


PROGRAM 10
#Python program to display multiplication table of a number

num = int(input("Display multiplication table of? "))

# Iterate 10 times from i = 1 to 10


for i in range(1, 11):
print(num, 'x', i, '=', num*i)
PROGRAM 11
#Python program to replace “ “ with “-“

def replacespace():
string = input("Enter a sentence: ") #take user input
newstr = string.replace(" ","-") # replaces all spaces with hyphen
print(newstr)

replacespace()
PROGRAM 12
#Floyd’s triangle

n=int(input("Enter the number :"))


for i in range(5,0,-1):
for j in range(5,i-1,-1):
print (j,end=' ')
print('\n')
PROGRAM 13
#Python program to convert Decimal to Binary , Octal , Hexadecimal

dec = int(input("Enter a decimal number: "))

print(bin(dec),"in binary.")
print(oct(dec),"in octal.")
print(hex(dec),"in hexadecimal.")
PROGRAM 14
#Python program to sort a given list

given_list = ["Hello", "This", "Is", "Lakshya"]


# printing the list before sorting
print("Before sorting the list given list :")
print(given_list)
# sorting the given list using sort() function
given_list.sort()
# printing thre list after sorting
print("after sorting the list given list :")
print(given_list)
PROGRAM 15
# Python program to count the frequency of given element in list

given_list = ["Hello", "Lakshya", "This", "Is", "Lakshya"]


# given element to search
element = "Lakshya"
# using count function to count the given element in list
countelement = given_list.count(element)
# printing the count
print("The given element", element, "occurs",
countelement, "times in the given list")
PROGRAM 16
#Python program to read a file line by line and print it

File1=open(‘myfile.txt’, ‘r’)
Lines=file1.readlines()

count=1
for line in Lines:
print(“Line(): {}”.format(count, line.strip()))
count+=count
PROGRAM 17
#Python program to remove all the lines that contain the character “a”
in a file and write it to another file

file1=open(‘myfile.txt’)
file2=open(“myfilenew.txt” , “w”)
for line in file1:
if “a” in line:
line=line.replace(“a” , “ ”)
else:
file2.write(line)
file1.close()
file2.close()
Program 18
#Python program to  read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the file
PROGRAM 19
#Program to read data from data file in read mode and

#count the particular word occurrences in given string,

#number of times in python.

f=open("test.txt",'r')

read=f.readlines()

f.close()

times=0 #the variable has been created to show the number of times the loop runs

times2=0 #the variable has been created to show the number of times the loop runs

chk=input("Enter String to search : ")

count=0

for sentence in read:

line=sentence.split()

times+=1

for each in line:

line2=each

times2+=1

if chk==line2:

count+=1

print("The search String ", chk, "is present : ", count, "times")

print(times)
print(times2)

PROGRAM 20
#Python program to read a text file and display each word separated by a #
#myfile.txt has following data
"""
welcome to python class
welcome to CBSE class 12 program 15
School programming
"""
fh=open(r"myfile.txt","r")
item=[]
a=""
while True:

a=fh.readline()

words=a.split()
for j in words:
item.append(j)
if a =="":
break
print("#".join(item))
PROGRAM 21
#Python program to write a random number generator that generates
random numbers between 1 and 6 (simulates a dice)
import random
a=[]
for I in range (7):
a.append(random.randint(1,6))
print(“Randomised list is”,a)
PROGRAM 22
#Python program to write a python program to implement a stack using
a list data structure
PROGRAM 23
#Python program to recursively find the factorial of a natural number
PROGRAM 24
#Python program to read lines from a text file python to read lines from a
text file STORY.TXT, and display those words, which are less than 4
characters
def DISPLAYWORDS() :
file = open("story.txt", "r")
lst = file.readlines()
for i in lst :
word = i.split()
for j in word :
if len( j ) < 4 :
print( j )
file.close()
DISPLAYWORDS()
PROGRAM 25
# Program to enter few lines of text file

f = open("friends.txt")
l = f.readline()
l2 = f.readline(18)
ch3=f.read(10)
print(l2)
print(ch3)
print(f.readline())
f.close()

PROGRAM 26
# Program to calculate arc length of an angle
import math
def arclength():
diameter = float(input("Diameter of a circle: "))
angle = float(input("Angle measure: "))
if angle>=360:
print("Angle is not possible")
return
arc_length = (math.pi*diameter) * (angle/360)
print("Arc length is: ",arc_length)
arclength()
PROGRAM 27
#Python program to print  number pyramid pattern
given_rows = 15
for i in range(1, given_rows+1):
for j in range(1, i+1):
print(j, end=&quot; &quot;)
print()
1
12
123
1234
12345
123456
1234567
12345678
123456789
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

PROGRAM 28
#Python program to separate odd and even indexed elements
# Separating odd and even index elements
# using naive method
 
# initializing list
test_list = [3, 6, 7, 8, 9, 2, 1, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using naive method
# Separating odd and even index elements
odd_i = []
even_i = []
for i in range(0, len(test_list)):
    if i % 2:
        even_i.append(test_list[i])
    else :
        odd_i.append(test_list[i])
 
res = odd_i + even_i
 
# print result
print("Separated odd and even index list: " + str(res))
PROGRAM 29
#Python program to repeat each element k times in a list
# repeat element K times
# using list comprehension
  
# initializing list of lists
test_list = [4, 5, 6]
  
# printing original list 
print("The original list : " +  str(test_list))
  
# declaring magnitude of repetition
K = 3
  
# using list comprehension
# repeat elements K times
res =  [ele for ele in test_list for i in range(K)]
  
# printing result 
print("The list after adding elements :  " + str(res))
PROGRAM 30
#Python program to repeat tuple n times
# Repeating tuples N times
# using * operator
  
# initialize tuple 
test_tup = (1, 3)
  
# printing original tuple 
print("The original tuple : " + str(test_tup))
  
# initialize N 
N = 4
  
# Repeating tuples N times
# using * operator
res = ((test_tup, ) * N)
  
# printing result
print("The duplicated tuple elements are : " + str(res))

You might also like