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

Lab programs

Uploaded by

ujspucprincipal
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 views10 pages

Lab programs

Uploaded by

ujspucprincipal
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/ 10

Lab Programs

PART - A
Chapter 5 : Getting Started with Python
1.Write a program to swap two numbers using a third variable.
a=int(input('Enter the value of a:'))
b=int(input('Enter the value of b:'))
print('Values of a and b before swapping')
print('Value of a=', a)
print('Value of b=', b)
temp = a
a=b
b = temp
print('Values of a and b after swapping')
print('Value of a=', a)
print('Value of b=', b)
2.Write a program to enter two integers and perform all arithmetic operations on
them.
a=int(input('Enter the value of a:'))
b=int(input('Enter the value of b:'))
a1=a+b
a2=a-b
a3=a*b
a4=int(a/b)
a5=a%b
a6=a//b
a7=a**b
print('Result for all arithmetic operations')
print('Addition=',a1)
print('Subtraction=',a2)
print('Multiplication=',a3)
print('Division=',a4)
print('Modulus=', a5)
print('Floor division=', a6)
print('Exponential=', a7)

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District
3.Write a Python program to accept length and width of a rectangle and compute its
perimeter and area.
l= float(input('Enter length of the rectangle:'))
b= float(input('Enter breadth of the rectangle:'))
a=l*b
p=2*(l+b)
print('Area=',a)
print('Perimeter=',p)
4.Write a Python program to calculate the amount payable if money has been lent on
simple interest. Principle or money lent = P, Rate of 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.
p= float(input('Enter the principle amount: '))
t= float(input('Enter the time: '))
r= float(input('Enter the rate: '))
si= (p*t*r)/100
amount=p+si
print('Simple interest=',si)
print('Amount payable =',amount)
Chapter 6 : Flow of Control
5.Write a Python program to find largest among three numbers.
a= float(input('Enter the value of a:'))
b= float(input('Enter the value of b:'))
c= float(input('Enter the value of c:'))
if (a >= b) and (a >= c):
large=a
elif (b>= a) and (b >=c):
large = b
else:
large = c
print("The largest number=", large)
6.Write a program that takes the name and age of the user as input and displays a
message whether the user is eligible to apply for a driving license or not.
name = input('Enter your name:')
age = int(input('Enter the age:'))
if age >= 18:
print('Eligible to apply for the driving license.')
else:
print('Not eligible to apply for the driving license.')

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District
7.Write a program that prints minimum and maximum of five numbers entered by
the user.
small= 0
large= 0
for i in range(0,5):
a= int(input('Enter the number:'))
if i == 0:
small = large = a
if(a < small):
small = a
if(a > large):
large= a
print('Smallest number=',small)
print('Largest number=',large)
8.Write a python program to find the grade of a student when grades are allocated as
given in the table below.
Percentage of Marks Grade.
Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
Below 60% E
Percentage of the marks obtained by the student is input to the program.
p= float(input('Enter the percentage of the student:'))
if(p > 90):
print('Grade A')
elif(p > 80):
print('Grade B')
elif(p > 70):
print('Grade C')
elif(p >= 60):
print('Grade D')
else:
print('Grade E')

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District
9.Write a function to print the table of a given number. The number has to be
entered by the user.
a = int(input("Enter the number: "))
i= 1
while i <= 10:
b=a*i
print(a, 'x', i, '=', b)
i=i+1
10.Write a program to find the sum of digits of an integer number, input by the user.
n = int(input('Enter the number:'))
sum = 0
while n > 0:
rem = n % 10
sum = sum + rem
n = n//10
print('The sum of digits=',sum)
11.Write a program to check whether an input number is a palindrome or not.
n = int(input('Enter a number:'))
temp = n
rev = 0
while(n>0):
digit = n % 10
rev= rev*10 + digit
n = n // 10
if(temp == rev):
print('It is a Palindrome')
else:
print('Not a Palindrome')
12.Write a program to print the following patterns:
12345
1234
123
12
1
n = int(input('Enter the number of rows: '))
for i in range(n,0,-1):
for j in range(1,i+1):
print(j, end=' ')
print( )

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District
PART - B
Chapter 7 : Functions
13.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 the gender.
def student(name,gender):
if (gender == 'M' or gender == 'm'):
print('Mr.',name)
elif (gender == 'F' or gender == 'f '):
print('Ms.',name)
else:
print('Please enter only M or F in gender')
name = input('Enter the name:')
gender = input('Enter the gender: M for Male, and F for Female:')
student(name,gender)
14.Write a program that has a user defined function to accept the coefficients of a
quadratic equation in variables and calculates its determinant. For example : if
the coefficients are stored in the variables a, b, c then calculate determinant as
b2-4ac. Write the appropriate condition to check determinants on positive, zero
and negative and output appropriate result.
a = int(input('Enter the value of a: '))
b = int(input('Enter the value of b: '))
c = int(input('Enter the value of c: '))
def disc(a, b, c):
d = b**2 - 4 * a * c
return d
det = disc(a,b,c)
if (det> 0):
print('The quadratic equation has two real roots.')
elif (det == 0):
print('The quadratic equation has one real root.')
else:
print('The quadratic equation does not have any real root.')

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District
15.Write a program that has a user defined function to accept 2 numbers as
parameters, if number 1 is less than number 2 then numbers are swapped and
returned, i.e., number 2 is returned in place of number1 and number 1 is
reformed in place of number 2, otherwise the same order is returned.
def swap(a, b):
if(a < b):
return b,a
else:
return a,b
x = int(input('Enter the value of a:'))
y = int(input('Enter the value of b:'))
print('Values of a and b before swap function')
print('Value of a=', x)
print('Value of b=', y)
print(‘After swap function:’)
x,y= swap(x,y)
print('Value of a=',x)
print('Value of b=',y)
16.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 of special symbols and total
number of words in the given text. (Assume that each word is separated by one
space).
string= input('Enter the string:')
l = len(string)
alpha = digit = special = 0
for a in string:
if a.isalpha():
alpha += 1
elif a.isdigit():
digit += 1
else:
special += 1
print('Total number of characters:',l)
print('Total number of alphabets:',alpha)
print('Total number of digits:',digit)
print('Total number of special sharacters:',special)
space = 0
for b in string:
if b.isspace():
space += 1
print('Total Words in the Input :',(space + 1))

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District
17.Write a user defined function to convert a string with more than one word into
title case string where string is passed as parameter. (Title case means that the
first letter of each word is capitalized)
def student(s):
s1 = s.title();
print('The input string in title case is:',s1)
name= input('Type the sentence:')
space = 0 OR
for b in name: def case(s):
if b.isspace( ): print(s1.title( ))
space += 1 s1 = input('Enter the sentence: ')
if(name.istitle( )): case(s1)
print('The String is already in title case')
elif(space > 0):
student(name)
else:
print('The String is in one word ')
18.Write a function that takes a sentence as an input parameter where each word in
the sentence is separated by a space. The function should replace each blank with
a hyphen and then return the modified sentence.
def replace1(s):
return s.replace(' ','-')
s1 = input('Enter the sentence: ')
replace1(s1)

Chapter 9 : Lists
19.Write a program to find the number of times an element occurs in the list.
list1 = [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
print('Elements in list is=',list1)
a = int(input('Enter the number to check times of an element occurs in the list:'))
b = list1.count(a)
print('An element ',a, 'is present in ',b,'times')

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District
20.Write a function that returns the largest element of the list passed as parameter.
def large(list1):
l = len(list1)
n= 0
for i in range(l):
if(i == 0 or list1[i] > n):
n = list1[i]
return n
list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90,100]
max = large(list1)
print('The elements of the list are:',list1)
print('The largest number=',max)
21.Write a program to read a list of elements. Modify this list so that it does not
contain any duplicate elements, i.e., all elements occurring multiple times in the
list should appear only once.
def dup(list1):
length = len(list1)
newlist = [ ]
for a in range(length):
if list1[a] not in newlist:
newlist.append(list1[a])
return newlist
list1= [ ]
n = int(input('Number of elements:'))
for i in range(n):
a = int(input('Enter the elements: '))
list1.append(a)
print('The numbers in list=',list1)
print('List without duplicate element is=',dup(list1))

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District
Chapter 10 : Tuples and Dictionaries
22.Write a program to read email IDs of n number of students and store them in a
tuple. Create two new tuples, one to store only the usernames from the email IDs
and second to store domain names from the email IDs. Print all three tuples at the
end of the program. [Hint: You may use the function split( )]
email = tuple( )
username = tuple( )
domainname = tuple( )
n = int(input('Enter the no. of valid mail ID:'))
for i in range(0,n):
eid = input('Enter the email ID:')
email = email +(eid,)
e = eid.split("@")
username = username + (e[0],)
domainname = domainname + (e[1],)
print('The ids in the tuple :')
print(email)
print('The username of email ids are:')
print(username)
print('The domain name of email IDs are:')
print(domainname)
23.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.
name = tuple()
n = int(input('Enter the no. names:'))
for i in range(0, n):
num = input('Enter the name:')
name = name + (num,)
print("Names entered are:")
print(name)
search=input('Enter the name to search?')
if search in name:
print('Name ',search,' is present')
else:
print('Name ', search,' is not found')

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District
24.Write a Python program to create a dictionary from a string.
Note: Track the count of the letters from the string.
Sample string : ‘2nd pu course’.
Expected output: {‘ ‘:2, ‘2’:1, ‘n’:1, ‘d’:1, ‘o’:1, ‘p’:1, ‘u’:2, ’c’:1, ‘s’:1, ‘r’:1, ‘e’:1}
s=input('Enter a string:')
print("The input string is:",s)
s1=dict( )
for character in s:
if character in s1:
s1[character]+=1
else:
s1[character]=1
print("The dictionary created from characters of the string is:")
print(s1)
OR
Create a dictionary with the roll number, name and marks of n students in a class
and display the names of students who have marks above 75.
n=int(input('Enter number of students:'))
result={ }
for i in range(n):
print('Enter the details of students',i+1)
rno=int(input('Enter roll no: '))
name=input('Enter the name: ')
marks=int(input('Enter the marks:'))
result[rno]=[name,marks]
print(result)
for j in result:
if(result[j][1]>75):
print('The name of students who have marks above 75:',result[j][0])

Ramachandra
Dept. Of Computer Science
PRN Amratha Bharathi PU College
Hebri, Udupi District

You might also like