0% found this document useful (0 votes)
10 views

Kle Ipc Lab Manual Pu 1

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)
10 views

Kle Ipc Lab Manual Pu 1

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

1) Write a program to swap two numbers using a third variable.

a = int(input("Enter an Integer: "))


b = int(input("Enter an another Integer: "))
print("Numbers before swapping")
print(" a = ", a, “b =”,b)
temp = a
a=b
b = temp
print("Numbers after swapping")
print(" a = ", a, “b =”,b)

OUTPUT:
Enter an Integer: 10
Enter an another Integer: 50
Numbers before swapping
a = 10 b = 50
2. Numbers after swapping
a = 50 b = 10
2) Write a python program to enter two integers and perform all
arithmetic operations on them.

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
print("Printing the result for all arithmetic operations:-")
print("Addition: ", a + b)
print("Subtraction: ", a - b)
print("Multiplication: ", a * b)
print("Division: ", a / b)
print("Modulus: ", a % b)

OUTPUT:
Enter first number: 10
Enter second number: 4
Printing the result for all arithmetic operations:-
Addition: 14
Subtraction: 6
Multiplication: 4 0
Division: 2.5
Modulus: 2
3) Write a Python program to accept length and width of a
rectangle and compute its perimeter and area.

length = float(input("Enter length of the rectangle: "))


width = float(input("Enter width of the rectangle: "))
area = length * width
perimeter = 2 * (length + width)
print("Area of rectangle = ", area)
print("Perimeter of rectangle = ", perimeter)

Output:
Enter length of the rectangle: 6
Enter breadth of the rectangle: 10
Area of rectangle = 60.0
Perimeter of rectangle = 32.0
4) Write a Python program to calculate the amount payable if money has
been lent on simple interest.
Principal or money lent = P
Rate of interest = R% per annum
Time = T years
Then Simple Interest (SI) = (P x R x T)/ 100
Amount payable = Principal + SI.
P, R and T are given as input to the program.

p = float(input('Enter amount: '))


t = float(input('Enter time: '))
r = float(input('Enter rate: '))
si= (p*t*r)/100
amount_payable=p + si
print('Simple interest is: ',simple_interest)
print(‘Amount_payable is: ', amount_payable)

Output:
Enter amount: 1500
Enter time: 10
Enter rate: 2.5
Simple interest is: 375.0
Amount payable is: 1875.0
5) Write a Python program to find largest among three numbers.

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
largest = num1
if num2 > largest:
largest = num2
if num3 > largest:
largest = num3
print("The largest number is", largest)

Output:
Enter first number: 23
Enter second number: 36
Enter third number: 93
The largest number is 93.0
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. (The eligible age is 18 years).

name = input("What is your name? ")


age = int(input("What is your age? "))
if age >= 18:
print("You are eligible to apply for the driving license.")
else:
print("You are not eligible to apply for the driving license.")

Output:
What is your name? Anil
What is your age? 14
You are not eligible to apply for the driving license.
7) Write a program that prints minimum and maximum of five numbers
entered by the user.

START

Count=0

NO
Count < 5

YES
Output : large,small
INPUT: n

NO
count == 0 STOP

YES

Small = large = n

NO
n < small
YES

count=count+1 Small = n
YES
NO
n > large

large = n

largest=smallest=0
for count in range(0,5):
n = int(input("Enter the number: ")) Output:
if count == 0: Enter the number: 22
smallest = largest = n Enter the number: 96
if(n < smallest): Enter the number: 21
smallest = n Enter the number: 3
Enter the number: 5
if(n > largest):
The smallest number is 3
largest = n
The largest number is 96
print("The smallest number is ",smallest)
print("The largest number is ",largest)
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.

n = float(input('Enter the percentage of the student: '))


if(n > 90):
print("Grade A")
elif(n > 80):
print("Grade B")
elif(n > 70): Output:
print("Grade C") Enter the percentage of the student: 75
Grade C
elif(n >= 60):
print("Grade D")
else:
print("Grade E")
9) Write a python program to print the table of a given number. The number
has to be entered by the user.

def print_multi_table (number):


print("Multiplication Table for", number, ":")
for i in range(1, 11):
result = number * i
Output:
print(number, "x", i, "=", result) Enter the number: 5
Multiplication Table for 5 :
5x1=5
number = int(input("Enter the number: ")) 5 x 2 = 10
print_multi_table (number) 5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
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
temp = abs(n)
while temp > 0:
digit = temp % 10
sum = sum + digit
temp = temp // 10
print("The sum of digits of the number is", sum)

Output:
Enter the number: 657
The sum of digits of the number is 18
11) Write a program to check whether an input number is a palindrome or
not.
(A palindrome number is a number that reads the same forward and backward.)

n = int(input("Enter a number:"))
temp = n
reverse = 0
while(n>0):
digit = n % 10
reverse = reverse*10 + digit
n = n // 10 Output:
if(temp == reverse): Enter a number: 16461
print("Palindrome") Palindrome
else:
print("Not a Palindrome")
12) Write a python program to print the following patterns:
12345
1234
123
12
1

START

Rows=5
i=rows

NO
i>0
YES

j=1 END
i=i-1

print () j >= i
NO
YES

print ( j, end=” “)

j += 1

rows = 5
for i in range(rows,0,-1):
for j in range(1, i+1):
print(j, end=" ")
print()

Output:
12345
1234
123
12
1
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 on the basis
of the gender.

def prefix(name,gender):
if (gender == 'M' or gender == 'm'):
print("Hello, Mr.",name)
elif (gender == 'F' or gender == 'f'):
print("Hello, Ms.",name)
else:
print("Please enter only M or F in gender")

name = input("Enter your name: ")


gender = input("Enter your gender: M for Male, and F for Female: ")
prefix(name,gender)
Output:
Enter your name: Raju
Enter your gender: M for Male, and F for Female: M
Hello, Mr. Raju
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
determinant on positive, zero and negative and output appropriate result.

def discriminant(a, b, c):


d=b*b-4*a*c
return d

print("Enter the co-efficients of quadratic equation ")


a = float(input("Enter the value of a: "))
b = float (input("Enter the value of b: "))
c = float (input("Enter the value of c: "))
d = discriminant(a,b,c)
if (d > 0):
print("The roots are real and different")
elif (d == 0):
print("The roots equal ")
else:
print("The roots are imaginary")

Output :
Enter the co-efficients of quadratic equation
Enter the value of a: 5
Enter the value of b: 3
Enter the value of c: 6

The roots are imaginary


15) Write a python 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 placed 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

n1 = int(input("Enter Number 1: "))


n2 = int(input("Enter Number 2: "))
print("Entered values are :")
print("Number 1:", n1," Number 2: ", n2)
n1, n2 = swap(n1, n2)
print("values after swap function :")
print("Number 1:", n1," Number 2: ", n2)

Output :
Enter Number 1: 65
Enter Number 2: 95
Entered values are :
Number 1: 65 Number 2: 95
values after swap function:
Number 1: 95 Number 2: 65
16) Write a python 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).

line = input("Write a sentence: ")


print("Total Characters: ", len(line))
Alpha = Digit = Spl = 0
for ch in line:
if ch.isalpha():
Alpha += 1
elif ch.isdigit():
Digit += 1
else:
Spl += 1
print("Total Alphabets: ", Alpha)
print("Total Digits: ", Digit)
print("Total Special Characters: ", Spl)
total_words = len(line.split())
print("Total Words in the Input :", total_words)

Output :
Write a sentence: The class is at 9 am on 16/12/24.
Total Characters: 33
Total Alphabets: 16
Total Digits: 7
Total Special Characters: 10
Total Words in the Input : 8
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 capitalised)

def convert(str):
if “ “ in str:
b=str.split(“ “)
if len(b)>1:
print("The input string in title case is:", str.title())
else:
print("The String is of one word only")
text = input("Write a sentence more than one word : ")
convert(text)

Output:
Write a sentence: hai how are you
The input string in title case is: Hai How Are You

18) Write a python program 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 replaceChar(string):
return string.replace(' ','-')
userInput = input("Enter a sentence: ")
result = replaceChar(userInput)
print("The new sentence is:",result)

Output:

Enter a sentence: This is my first lab program


The new sentence is: This-is-my-first-lab-program
19) Write a python program to find the number of times an element occurs in
the list.

list1 = list(eval(input("enter the elements separated by commas :”)))


print("The list is: ", list1)
ele = int(input("Which element occurrence would you like to count? "))
count = list1.count(ele)
print("The count of element", ele, " in the list is: ",count)

Output :
enter the elements seperated by commas :12,24,12,45,12,56
The list is: [12, 24, 12, 45, 12, 56]
Which element occurrence would you like to count? 12
The count of element 12 in the list is: 3

20) Write a python function that returns the largest element of the list passed
as parameter.

def largest(list1):
large=max(list1)
return large

list1 = list(eval(input("enter the elements separated by commas :”)))


print("The elements of the list", list1)
print(" The largest number of the list:", largest(list1))

Output:
enter the elements separated by commas :1,2,3,4,5,6
The elements of the list [1, 2, 3, 4, 5, 6]
The largest number of the list: 6
21) Write a python 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.

list1 = list(eval(input("enter the elements separated by commas :”)))


list2 = []
for i in list1:
if i not in list2:
list2.append(i)
print("The list entered is:", list1)
print("List without duplicate element is:", list2)

Output:
enter the elements separated by commas :10,20,30,10,20
The list entered is: [10, 20, 30, 10, 20]
List without duplicate element is: [10, 20, 30]
22) Write a python 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()]

emails = tuple()
username = tuple()
domainname = tuple()
n = int(input("Enter number of students : "))
for i in range(0,n):
emid = input("Enter email ID for student {i+1}> ")
emails = emails + (emid,)
spl = emid.split("@")
username = username + (spl[0],)
domainname = domainname + (spl[1],)
print("\nThe email id’s in the tuple are:")
print(emails)
print("\nThe username in the email id’s are:")
print(username)
print("\nThe domain name in the email id’s are:")
print(domainname)

Enter number of students : 3


Enter the email id's :
> [email protected]
> [email protected]
> [email protected]

The email id’s in the tuple are:


('[email protected]', '[email protected]', '[email protected]')

The username in the email id’s are:


('raj123', 'lucky321', 'indus')

The domain name in the email id’s are:


('gmail.com', 'yahoo.com', 'rediff.com')
23) Write a python 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.

std_names = tuple()
n = int(input("Enter number of students: "))

for i in range(0, n):


name = input("Enter the name of student > ")
std_names = std_names + (num,)
print("\nThe names entered in the tuple are:")
print(std_names)
search_name=input("\nEnter the name of the student to search? ")
if search_name in std_names:
print("The name ",search_name," is present in the tuple")
else:
print("The name ",search_name," is not found in the tuple")

Enter number of students: 3


Enter the name of student > Priya
Enter the name of student > Raju
Enter the name of student > Sunil

The names entered in the tuple are:


('Priya', 'Raju', 'Sunil')

Enter the name of the student to search? Raju


The name Raju is present in the tuple
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': 1, 'n': 1, 'd': 1, ' ': 2, 'P': 1, 'U': 1, 'C': 1, 'o': 1, 'u': 1, 'r': 1, 's':1, 'e': 1}

str = input("Enter a string: ")


dict = {}

for char in str:


dict[char] = dict.get(char, 0) + 1

print("Input String:", str)


print("Character Counts:", dict

Enter a string: Hello Hello


Input String: Hello Hello
Character Counts: {'H': 2, 'e': 2, 'l': 4, 'o': 2, ' ': 1}

You might also like