I PU Comp SC Practicals - Python
I PU Comp SC Practicals - Python
MARY’S PU COLLEGE
I PUC – COMPUTER SCIENCE LAB MANUAL - PYTHON
PROGRAM 1: Write a program to swap two numbers using a third variable.
FLOWCHART:
Program: Output:
a=int(input("Enter first number")) Enter first number 10
b=int(input("Enter second number")) Enter second number 20
temp=a After interchanging
a=b Value of a = 20
b=temp Value of b = 10
print("After interchanging")
print("Value of a =",a)
print("Value of b=",b)
PROGRAM 2: Write a program to enter two integers and perform all arithmetic operations on
them.
FLOWCHART:
Program: Output
a=int(input("Enter first number")) Enter first number20
b=int(input("Enter second number")) Enter second number 10
print("Addition= ",a+b) Addition= 30
print("Subtraction= ",a-b) Subtraction= 10
print("Multiplication=",a*b) Multiplication= 200
print("Division=",a/b) Division= 2.0
print("Quotient=",a//b) Quotient= 2
print("Remainder=",a%b) Remainder= 0
PROGRAM 3: Write a Python program to accept length and width of a rectangle and compute its
perimeter and area.
Program Output
l=float(input("Enter the length")) Enter the length 2.5
w=float(input(“Enter the width”)) Enter the width 3.5
a=l*w Area= 8.75
p=2*(l+w) Perimeter= 12.0
print(“Area=”,a)
print(“Perimeter=”,p)
1
PROGRAM 4 : Write a Python program to calculate the amount payable if money has been lent on simple
interest. Principle or money lent = P, Rateof interest = R% per annum and Time = T years. Then Simple
Interest (SI) =(P x R x T)/ 100. Amount payable = Principle + SI. P, R and T are given as input to the
program.
Program Output
p=int(input("Enter the Principle:")) Enter the Principle:10000
r=int(input("Enter rate of interest:")) Enter rate of interest:12
t=int(input("Enter time:")) Enter time:2
si=(p*t*r)/100 Simple interest= 2400.0
ap=p+si Amount Payable=
print("Simple interest=",si) 12400.0
print("Amount Payable=",ap)
FLOWCHAT:
PROGRAM 5: Write a Python program to find largest among three numbers.
Program Output1
a=int(input("Enter First number:")) Enter First number:10
b=int(input("Enter Second number:")) Enter Second number:20
c=int(input("Enter third number:")) Enter third number:30
big=a The largest number = 30
if (b>big): Output2
big=b Enter First number:50
if (c>big): Enter Second number:20
big=c Enter third number:30
print("The largest number = ",big) The largest number = 50
PROGRAM 6: Write a program message whether the user is eligible to apply for adriving license
or not. (the eligible age is 18 years).
that takes the name and age of the user as input and displays a message whether the user is
eligible to apply for adriving license or not. (the eligible age is 18 years).
2
Program Output1
name=input("Enter your name:") Enter your name:Sheela
age=int(input("Enter your age:")) Enter your age:15
if (age>=18): Sheela is not eligible for
print(name,"is eligible for driving license") driving license
else: Output2
print(name, "is not eligible for driving Enter your name:Raj
license") Enter your age:21
Raj is eligible for driving
license
PROGRAM 7: Write a program to find the grade of a student when grades are allocated as given below. Percentage
of Marks Grade: Above 90% - A, 80% to 90% - B, 70% to 80% - C, 60% to 70% - D, Below 60% E
FLOWCHART
Program Output
perc=int(input("Enter the percentage:")) Enter the percentage76
if perc>90: Grade= C
print("Grade=A")
elif (perc>=80 and perc<90):
print("Grade=B")
elif (perc>=70 and perc<80):
print("Grade=C")
elif (perc>=60 and perc<70):
print("Grade=D")
else:
print("Grade=E")
3
PROGRAM 8: Write a program to find the sum of digits of an integernumber, input by the user.
FLOWCHART:
Program Output
n=int(input("Enter the number:")) Enter the number:23
sum=0 Sum of the digits= 5
while(n>0):
r=n%10
sum=sum+r
n=n//10
print("Sum of the digits=",sum)
Program Output
rows=5 12345
for i in range(rows,0,-1): 1234
for j in range(1,i+1): 123
print(j,end=' ') 12
print() 1
4
PROGRAM 10: Write a program that uses a user defined function that accepts name and gender
(as M for Male, F for Female) and prefixes Mr./Ms. based on thegender.
Program Output
def prefix(name,gender): Enter the name Sonia
if(gender=='M'or gender=='m'): Enter the gender f
return "Mr."+name Hi Ms.Sonia
elif(gender=='F'or gender=='f'):
return "Ms."+name
else:
return "Please enter the gender correctly"
name=input("Enter the name")
gender=input("Enter the gender")
result=prefix(name,gender)
print("Hi "+result)
OWCHART:
Program 11.
Write a program to input line(s) of text from the user until enter is pressed. Count the total number of
characters in the text (including white spaces), total number of alphabets, total number of digits,
total number ofspecial symbols and total number of words in the given text. (Assume thateach word is
separated by one space).
5
FLOWCHART:
6
Program Output
a=input("Enter the text") Enter the text Hi Welcome 123#
print("No of chars",len(a)) No of chars 15
alpha=digit=space=spl=0 No.of Alphabets: 9
for i in range(len(a)): No.of digits: 3
if a[i].isalpha(): No.of words: 3
alpha+=1 No.of Special Symbols: 1
elif a[i].isdigit():
digit+=1
elif a[i].isspace():
space+=1
else:
spl+=1
print("No.of Alphabets: ",alpha)
print("No.of digits: ",digit)
print("No.of words: ",space+1)
print("No.of Special Symbols: ",spl)
PROGRAM 12.
Write a function that takes a sentence as an input parameter where eachword in the sentence is
separated by a space. The function should replaceeach blank with a hyphen and then return the modified
sentence.
FLOWCHART:
7
Program Output
def hypen(string): Enter a sentence Proud to be an Indian
print(str.replace(' ','-')) Proud-to-be-an-Indian
str=input("Enter a sentence")
hypen(str)
PROGRAM 13:
Write a program to find the number of times an element occurs in the list.
FLOWCHART:
Program Output
list1=eval(input("Enter the elements")) Enter the elements[1,2,1,3,4]
num=int(input("Enter the element to check")) Enter the element to check1
if num in list1: 1 is present 2 number of times
print(num,"is present",list1.count(num),"number of times")
else:
print(num,"is not present in the list")
8
PROGRAM 14
Write a program to input names of n students and store them in a tuple.Also, input a name from the user and
find if this student is present in the tuple or not.
FLOWCHART:
Program Output
name=tuple() How many names do you want to enter?2
n=int(input("How many names do you want to enter?")) > Ram
for i in range(0,n): > Sita
num=input("> ") The names entered in the tuple are:
name=name+(num,) ('Ram', 'Sita')
print("\n The names entered in the tuple are:") Enter the name you want to searchRam
print(name) The name Ram is present in the tuple
search=input("\n Enter the name you want to search")
if search in name:
print("The name",search,"is present in the tuple")
else:
... print("The name",search,"is not found in the tuple")
9
PROGRAM:15
Write a Python program to create a dictionary from a string. Note: Track the count of the
letters from the string. Sample string : ‘2nd pucourse’
Expected output : {‘2’:1, ‘n’:1, ‘d’:1, ‘o’:1, ‘p’:1, ‘u’:2, ’c’:1, ‘s’:1, ‘r’:1, ‘e’:1}
FLOWCHART:
Program Output
st=input("Enter a string") Enter a string2nd pu course
dic={ } 2:1
for ch in st: n:1
if ch in dic: d:1
dic[ch]+=1 :2
else: p:1
dic[ch]=1 u:2
for key in dic: c:1
print(key,':',dic[key]) o:1
r:1
s:1
e:1
10