COMPUTER SCIENCE
PRACTICAL FILE
ON
PYTHON PROGRAMS
Submitted by:
Prince Kumar
Roll No.- 34
COMPUTER SCIENCE
PYTHON PRACTICAL PROGRAMS
I. Program to obtain length and breadth of a rectangle and calculate its area.
Solution.
#to input length and breadth of a rectangle and calculate its area
length = float( input("Enter length of the rectangle : t'))
breadth= float( input Enter breadth of
the rectangle: area=length * breadth
print ("Rectangle specifications print
("Length = ", length) print ("breadth ",
breadth) print ("Area = area)
2. Write a program in python to display
even and odd numbers from I to N. Solution.
num=int(input("Enter the Range:"))
for i in range(l,num+l):
'Ne Eat Sheli -Déug print(i,
"is even number") teln on win32
else: Type "help" , "copy: print(i,"is odd number")
3. Write a program in python to all print prime number from 1 to n.
Solution.
the number:"))
+ I):
# If num is divisible by any number
# between 2 and num, it is not prime
if num > 1:
for n in range(2, val): if (val % n) o:
break
else:
print(val)
4. Write Python script to print the following for n numbers pattern:
1
13
Python 3.8.0 Shell
135
File Edit Shell Debug Options Window Help 1357
3.8.0 01
Solution. tel) on win32
Type "help", "copyright","credits"
num=int(input("Enter the number:'
for a in range (2, num, 2): for b in
range (I, a, 2): print (b, end t)) print
()
5. Write a program to find sum of the series: S=I+ X + X2 +X3+....xn
Solution.
x = int ( input ( "Enter value of X ) )
6. Write a program to check a number whether it is palindrome or not.
num=int(input("Enter a number :
n—num res=0 while num>0: rem=num% 10 res=rem+res* 10
num=num//10 if res==n:
print(n,": Number is Palindrome")
else:
print(n, t' : Number is not
Palindrome")
7. Write a program to test if a string is
palindrome or not. SOURCE CODE:
string = enter your own String : if(string
string[:: - I]):
print(string, is a Palindrome String")
else:
print(string, ": is Not a Palindrome
String")
8. Write a program to calculate Simple and compound interest.
the principal amount : t'))
r=float(input("Enter the rate of interest : "))
the
time
in
p*x-p years
: "))
"copyright", "credits" c print("Simple
interest is : ", round(SI,2)) RESTART: E
amount : 1000
print("Compound interest is , round(CI,2)) interest : S.S
:6
• 330.0
is :
372.24
9. Write a program to input a character and to print whether a given character is
an alphabet, digit or any other character.
SOURCE CODE:
a character: ") if
ch.isalpha():
print(ch, "is an alphabet") elif
ch.isdigit():
print(ch, "is a digit") elif
ch.isalnum():
print(ch, "is alphabet and
numeric")
print(ch, "is a special symbol")
10.Program to count frequency of a given element in a list of numbers
SOURCE CODE:
Lst=eval(input ( " Enter list :"))
length=Ien(Lst) element=int(input( "
Enter element count=O for i in
range(O, length):
if element==Lst [i]:
count I if
count O:
print (element, "not found in given list") else:
print(element, "has frequency as", count, "in given list")
20 has frequency 3 given File3
11.Program to create a dictionary containing names of competition winner
students as key number of their wins as values
SOURCE CODE:
n=int(input("How many students?
winners } for a in range(n):
key=input("Name of the student : i')
("Number of competitions won
print ("The dictionary now is : ")
print (winners)
Name cf the student :
Number ccmpetlticns von : 5 Name
of the student : Sunny
Number cf competitions won : 3
4
Name ) the student : Ezajwai
12.Write a program to calculate the factorial of an integer using recursion.
SOURCE CODE:
def factorial(n):
if n 1.
return n
factorial(n-l)
the
number: if num < O:
factorial does not exist for negative numbers")
0:
elif num print("The factorial of O is 1") else:
print("The factorial of ",num," is ", factorial(num))
RESTART: c: grogram Files
120
13.Write a program to print fibonacci series using recursion. SOURCE
CODE:
def fibonacci(n):
if n<=l:
return n else:
return(fibonacci(n-I)+fibonacci(n-2)) num=int(input("How
many terms you want to display: for i in range(l,num+l):
print(fibonacci(i), end="
RESTART: c: /Prcgram Files How many
terma you want to display: 10
2 3 5 8 13 21 34 55
14.To write a Python program to find the maximum of a list of numbers.
n=int(input("Enter the list size:"))
for i in range(n):
the number"))
a.append(num) print (a)
max=a [O] for i in
range(n): if(max<a[i]):
print
RESTART: C: 'Program Files
Enter the list size;5
Ente: the nurhe:20
the number30 [20, sol
Enter the numberl O [20, 30.
101
Enter the numberSO [20,
30, 10, 501 the
number40
30, 10, so, 401 maximum SO
15.Write a program for linear search.
SOURCE CODE:
L=int(input("Enter the list size:"))
loc=-l for i in range(L):
num=int(input("Enter the number"))
a.append(num) print (a)
the element that you want to search • for i in
range(n):
if
loc=i+l break if loc
> -1:
found at the position : else:
not Found")
RESTART: C: /Eogram Files
Enter the list gize:S
Enter the
[201
Ente: the nurEe:30 [20, 301
Enter the number10 [20, 30,
101 the numbe:40 [20, 30,
10, 401
Ente: the numbe:50
[20, 30, 10, 40, SO]
Enter the element that vant search : 40
Element found at the position : 4
16.Write a program for bubble sort.
SOURCE CODE:
L=int(input("Enter the list size:"))
loc=-l
for i in range(L):
num=int(input("Enter the number"))
a.append(num) print (a)
n=len(a) for p in for i
in range(O,n-I): if
print("The sorted list is : a)
Entez the
Enter the 40 [10, 401
50
[10, 40, 501
Enter the nurEe:20
[10, 40, 50, 201
Enter the number30
[10, 40, so, 20, 30)
The sorted Iidt is : [10, 40, 20, so, 301
The sorted liat is : [10, 40, 20, 30, 501
The sorted list is • [10, 20, 40, 30, 501
The sorted : 20, 30, 40,
17.Program to input two numbers and print their LCM and HCF.
SOURCE CODE:
X=int(input("Enter first number:"))
int (input("Enter second
number:")) if X smaller = Y
smaller = X
for i in range(l,
smaller + I):
% and (Y 0/0 i = O) ) •
hcf=i
Y) / hcf Ente: number:5RESTART: H.C.F. of", X, "and Y, "is",
hcf) Enter second number: 4
The a.C.F. S and 4 1
L.C.M. X, "and Y, "is", lcm) The L.c.Y. s and 4 13 20.0
18 Write a program to display ASCII code of a character and vice versa.
SOURCE CODE:
var=True while var:
choice=int(input("Press-1 to find the ordinal value of a character \
nPress-2 to find a character of a value\n")) if choice==l:
ch=input("Enter a character : ")
print(ord(ch)) elif choice==2:
val=int(input("Enter an integer value: "))
print(chr(val)) else:
print("You entered wrong
choice") print("Do you want to
continue? YIN") option=input()
if var=True else:
var-False
Q.1 9 WAP to calculate the mean
of a given list of numbers.
CODE!
n=int(input("enter the number ot
elements: "))
m=int(input("enter the number:"))
d.app
end(m)
print("th
e mean
is:",
(sum(d)/
len(d)))
OUTPUT
enter
the
num
ber
of
elem
ents:
5
enter
the
num
ber;
I
enter
the
num
ber:2
enter
the
num
ber:3
en
ter
the
num
ber:4
enter
the
num
ber;5
the
mea
n is:
Q20 WAP to search for an element
in a given list of numbers.
CODE:
the number of
elements; 0))
for I in range(n):
m=int(input("enter the number:"))
d.append(m)
element do you want to
search?
OUTPUT.
enter the
number of
elements: 5
enter the
number: 12
enter the
number:34
enter the
number:54
enter the
number:34
enter the
number:23
element do you
want to search?
34 position: I