0% found this document useful (0 votes)
20 views4 pages

Python Programs for Basic Operations

Lab m

Uploaded by

shubanand11
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)
20 views4 pages

Python Programs for Basic Operations

Lab m

Uploaded by

shubanand11
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

No.

Programs Output
01 Write a program to swap two numbers using a third variable. Output:
Enter an Integer: 2
a = int(input("Enter an Integer: "))
Enter an another Integer: 3
b = int(input("Enter an another Integer: "))
Numbers BEFORE swapping:
print("Numbers BEFORE swapping: ")
a = 2, b = 3
print("a = ",a,"b = ",b)
Numbers AFTER swapping:
temp = a
a = 3, b = 2
a=b
b = temp
print("Numbers AFTER swapping: ")
print("a = ",a,"b = ",b)
02 Write a program to enter two integers and perform all arithmetic Output:
operations on them. Enter an Integer: 25
Enter an another Integer: 6
a = int(input("Enter an Integer: ")) Given Numbers are: a = 25, b = 6
b = int(input("Enter an another Integer: ")) Addition: 31
print("Given Numbers are: ") Subtraction: 19
print("a = ",a,", b = ",b) Multiplication: 150
print("Addition: ", a+b) Division: 4.166666666666667
print("Subtraction: ", a-b) Exponent: 244140625
print("Multiplication: ",a*b) Floor division: 4
print("Division: ", a/b) Modula’s division: 1
print("Exponent: ", a**b)
print("Floor division: ",a//b)
print("Modula’s division: ", a%b)
03 Write a Python program to accept length and width of a rectangle Output:
and compute its perimeter and area. Enter the length of the rectangle: 5
Enter the width of the rectangle:
length = float(input("Enter the length of the rectangle: ")) 10 Area of the rectangle: 50.0
width = float(input("Enter the width of the rectangle: ")) Perimeter of the rectangle: 30.0
area = length * width
perimeter = 2 * (length + width)
print("Area of the rectangle: ", area)
print("Perimeter of the rectangle: ", perimeter)
04 Write a Python program to calculate the amount payable if money Output:
has been lent on simple interest. Enter the principal amount (P):
1000 Enter the rate of interest (R)
p = float(input("Enter the principal amount: ")) per annum: 10
r = float(input("Enter the rate of interest per annum: ")) Enter the time (T) in years: 2
t = float(input("Enter the time in years: ")) Simple Interest (SI): 200.0
si= (p* r * t) / 100 Amount Payable: 1200.0
ap = p+ si
print("Simple Interest (SI):", si)
print("Amount Payable:", ap)
06 Write a program that takes the name and age of the user as input Output - 1:
and displays a message whether the user is eligible to apply for a Enter your name: SOUMYA
driving license or not. (the eligible age is 18 years) Enter your age: 13
Sorry, SOUMYA. You are not
name = input("Enter your name: ") eligible yet.
age = int(input("Enter your age: ")) Output – 2:
if age >= 18: Enter your name: PRAKASH
print("Hello,", name + "! You are eligible.") Enter your age: 32
else: Hello, PRAKASH! You are eligible.
print("Sorry,", name + ". You are not eligible yet.")
08 Write a program to find the grade of a student when grades are Output:
allocated as given in the table below. Percentage of Marks Grade Enter the percentage of marks: 90
Above 90% A Grade: B
80% to 90% B
70% to 80% C Enter the percentage of marks: 45
60% to 70% D Grade: E
Below 60% E

per = float(input("Enter the percentage of marks: "))


if per > 90:
grade = 'A'
elif per >= 80:
grade = 'B'
elif per >= 70:
grade = 'C'
elif per >= 60:
grade = 'D'
else:
grade = 'E'
print("Grade:", grade)
09 Write a program to print the table of a given number. The number Output: Enter the number: 8
has to be entered by the user. Multiplication Table for 8:
8x1=8
number = int(input("Enter the number: ")) 8 x 2 = 16
print("Multiplication Table for", number, ":") 8 x 3 = 24
for i in range(1, 11): 8 x 4 = 32
result = number * i 8 x 5 = 40
print(number, "x", i, "=", result) 8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
12 Write a program to print the following patterns: Output:
12345 12345
1234 1234
123 123
12 12
1 1
rows = 5
for i in range(rows, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")
print( )
13 Write a program that uses a user defined function that accepts Output:
name and gender (as M for Male, F for Female) and prefixes Enter your name: PRIYA
Mr./Ms. based on the gender. Enter your gender (M for Male, F
for Female): F
def add_prefix(name, gender): Greetings: Ms. PRIYA
if gender == 'M':
return "Mr. " + name
elif gender == 'F':
return "Ms. " + name
else:
return "Unknown Gender"
name = input("Enter your name: ")
gender = input("Enter your gender (M for Male, F for Female):
")
result = add_prefix(name, gender)
print("Greetings:", result)
15 Write a program that has a user defined function to accept 2 Output:
numbers as parameters, if number 1 is less than number 2 then Enter the first number: 23
numbers are swapped and returned, i.e., number 2 is returned in Enter the second number: 28
place of number1 and number 1 is reformed in place of number 2, Original Numbers: 23.0 28.0
otherwise the same order is returned. Swapped Numbers: 28.0 23.0

def swap_numbers(num1, num2):


if num1 < num2:
temp = num1
num1 = num2
num2 = temp
return num1, num2
n1 = float(input("Enter the first number: "))
n2 = float(input("Enter the second number: "))
res1, res2 = swap_numbers(n1, n2)
print("Original Numbers:", n1, n2)
print("Swapped Numbers:", res1, res2)
18 Write a function that takes a sentence as an input parameter Output:
where each word in the sentence is separated by a space. The Enter a sentence : what an
function should replace each blank with a hyphen and then return amazing experience is learning
the modified sentence.
Original Sentence: what an amazing
def replace(s): experience is learning
m = [Link](' ', '-')
return m Modified Sentence: what-an-
s = input("Enter a sentence ") amazing-experience-is-learning
ms = replace(s)
print("Original Sentence:", s)
print("Modified Sentence:", ms)
19 Write a program to find the number of times an element occurs in Output:
the list. Enter the elements: [1,2,3,4,2,2,6]
list=eval(input(“Enter the elements:”)) Enter the element to check: 2
num=int(input(“ Enter the element to check:”) ) 2 is present 3 number of times
if num in list:
print(num,”is present”, [Link](num),”number of time”)
else:
print(num,” is not present in list”)
20 Write a function that returns the largest element of the list Output:
passed as parameter. Enter the elements[1,2,4,7,8,9]
The largest element in the given list
def largest(list): is 9
largest = max(list)
return largest
l=eval(input(“enter the element”) )
print(“The largest element in the given list is”,largest(l))
21 Write a program to read a list of elements. Modify this list so that Output:
it does not contain any duplicate elements, i.e., all elements Enter the list:[1,2,3,1,4,1,2,5]
occurring multiple times in the list should appear only once. List without duplicate elements
[1, 2, 3, 4, 5]
list=eval(input(“Enter the list:”))
list1=[ ]
for i in list:
if i not in list1:
[Link](i)
print(“List without duplicate elements”, list1)
23 Write a program to input names of n students and store them in a Output:
tuple. Also, input a name from the user and find if this student is Enter the number of students: 3
present in the tuple or not. Enter name for student 1: PRIYA
Enter name for student 2: KANNAN
n = int(input("Enter the number of students: ")) Enter name for student 3: ZAID
snames = [ ] Enter a name to check if the student
for i in range(n): is present: KANNAN
name = input("Enter name for student {}: ".format(i + 1)) KANNAN is present in the list of
[Link](name) students.
stuple = tuple(snames)
search_name = input("Enter a name: ")
if search_name in stuple:
print(search_name + " is present .")
else:
print(search_name + " is not present.")
24 Write a Python program to create a dictionary from a string. Output:
Enter a string: 2nd puc course
str = input("Enter a string: ") Input String: 2nd puc course
counts = { } Character Counts:
for char in str: {'2': 1, 'n': 1, 'd': 1, ' ': 2, 'p': 1, 'u': 2,
counts[char] = [Link](char, 0) + 1 'c': 2, 'o': 1, 'r': 1, 's': 1, 'e': 1}
print("Input String:", str)
print("Character Counts:", counts)

You might also like