0% found this document useful (0 votes)
14 views12 pages

I Pu Python Lab Programs - Simpler Version

The document provides a series of Python programming exercises with solutions, covering topics such as swapping numbers, arithmetic operations, calculating area and perimeter, determining eligibility for a driving license, finding the largest number, and checking for palindromes. Each exercise includes sample inputs and outputs to demonstrate functionality. Additionally, it contains a quick reference guide for Python basics, control structures, functions, data types, and common programming tasks.

Uploaded by

avishesh009
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)
14 views12 pages

I Pu Python Lab Programs - Simpler Version

The document provides a series of Python programming exercises with solutions, covering topics such as swapping numbers, arithmetic operations, calculating area and perimeter, determining eligibility for a driving license, finding the largest number, and checking for palindromes. Each exercise includes sample inputs and outputs to demonstrate functionality. Additionally, it contains a quick reference guide for Python basics, control structures, functions, data types, and common programming tasks.

Uploaded by

avishesh009
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

The Following is for Students with Input & Output Values

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

Solution
a = int(input( ))
b = int(input())
temp = a
a=b
b = temp
print(" After swapping " , a,b)

Inputs to be entered during execution

Output

After swapping: 2 3

2) Write a program to enter two integers and perform all arithmetic operations on them.

Solution
a = int(input( ))
b = int(input( ))
print(a + b, a - b, a * b, a / b, a % b, a // b, a ** b)

Inputs to be entered during execution


3
2
Output:
5 1 6 1.5 1 1 9

3. Write a Python program to accept length and width of a rectangle and compute its
perimeter and area.
Solution
length = float(input( ))
width = float(input( ))

perimeter = 2 * (length + width)

area = length * width

print(" Perimeter ", perimeter , " Area ", area)

Inputs to be entered during execution


3
2
Output:
Perimeter 10.0 Area 6.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 and 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
Solution

P = float(input( ))

R = float(input( ))

T = float(input( ))

print(" Simple Interest ", (P * R * T) / 100 , " Amount Payable " , P + (P * R * T) / 100)

Inputs to be entered during execution


1000

10

Output

Simple Interest: 100.0 Amount Payable: 1100.0

5. Write a Python program to find the largest among three numbers.


Solution

a = float(input( ))
b = float(input( ))
c = float(input( ))

if a >= b and a >= c:

largest = a

elif b >= a and b >= c:

largest = b

else:

largest = c

print("The largest number is:", largest)

Inputs to be entered during execution


3
2
1
Output:
The largest number is: 3.0

[Link] 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).
Solution

name = input( )
age = int(input( ) )

if age >= 18:

print(name , " Eligible to apply for a driving license. " )

else:

print(name , " Not eligible to apply for a driving license. " )

Inputs to be entered during execution


Name = "A"

Age = 18
Output:
A , you are eligible to apply for a driving license.
Inputs to be entered during execution
Name = "B"

Age = 17
Output:
B , you are not eligible to apply for a driving license.

7. Write a program that prints minimum and maximum of five numbers entered by the user.

Solution

minimum = None

maximum = None
for i in range(5):

num = float(input( ))

if minimum is None or num < minimum:

minimum = num

if maximum is None or num > maximum:

maximum = num

print(" Minimum " , minimum)

print(" Maximum " , maximum)

Inputs to be entered during execution


5

1
Output:
Minimum: 1.0

Maximum: 5.0
8. Write a 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.

Solution

percentage = float(input( ))

if percentage > 90:

grade = "A"
elif percentage >= 80:

grade = "B"

elif percentage >= 70:

grade = "C"

elif percentage >= 60:

grade = "D"

else:

grade = "E"

print( " Grade " , grade)

Inputs to be entered during execution


Enter the percentage of marks: 95
Output:
Grade: A
Inputs to be entered during execution
Enter the percentage of marks: 50
Output:
Grade: E

9. Write a function to print the table of a given number. The number has to be entered by the
user.

Solution
def table(n):
for i in range(1, 11):

print(n, "x", i, "=", n*i)

num = int(input( ) )

table(num)

Inputs to be entered during execution


num = 3
Output:
3x1=3
3x2=6
3x3=9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

[Link] a program to find the sum of digits of an integer number, input by the user.

Solution

num = int(input( ) )

s=0

while num > 0:

s = s + num % 10

num = num // 10

print(s)

Inputs to be entered during execution


num = 121
Output:
3

[Link] a program to check whether an input number is a palindrome or not.

Solution

num = input( )
if num == num[::-1]:

print("Palindrome")

else:

print("Not Palindrome")

Inputs to be entered during execution


121
Output:
Palindrome
Inputs to be entered during execution
123
Output:
Not Palindrome

[Link] a program to print the following patterns:

12345

1234

123

12

Solution
for i in range(5, 0, -1):

for j in range(1, i+1):

print(j, end=" ")


print( )
Inputs to be entered during execution
5 is the input value which is mentioned in the program already.
Output:
Output pattern will be printed as given in the question.

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.
Solution
def prefix(name, gender):
if gender == 'M':
print("Mr", name)
elif gender == 'F':
print("Ms", name)

n = input( "Enter name: ")


g = input("Enter gender (M/F): ")
prefix(n, g)
Inputs to be entered during execution
Enter name: ABC
Enter gender (M/F): M
Output
Mr ABC

Inputs to be entered during execution


Enter name: XYZ
Enter gender (M/F): F
Output
Mr XYZ

[Link] 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.
Solution
def determinant(a, b, c):
d = b**2 - 4*a*c
if d > 0:
print("Positive determinant")
elif d == 0:
print("Zero determinant")
else:
print("Negative determinant")

a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
determinant(a, b, c)

Inputs to be entered during execution


Enter a: 1
Enter b: 2
Enter c: 1
Calculation: 𝑑 = 𝑏2 − 4𝑎𝑐 = 22 − 4(1)(1) = 4 − 4 = 0
Output
Zero determinant
Inputs to be entered during execution
Enter a: 1
Enter b: 3
Enter c: 1
Calculation: 𝑑 = 32 − 4(1)(1) = 9 − 4 = 5
Output
Positive determinant

[Link] 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.
Solution
def swap(x, y):
if x < y:
x, y = y, x
return x, y

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


b = int(input("Enter second number: "))
a, b = swap(a, b)
print(a, b)

Inputs to be entered during execution


Enter first number: 2
Enter second number: 5
Since 2 < 5, they are swapped.
Output
52
Inputs to be entered during execution
Enter first number: 5
Enter second number: 2
Since 5 > 2 , they are not swapped.
Output
52
🎤 Viva-Voce Quick Reference Guide
🔹 Basics

 Q: Why is indentation important in Python?


A: Indentation defines code blocks. Without proper indentation, Python raises errors
because it cannot group statements correctly.

 Q: What is the difference between = and ==?


A: = is the assignment operator (stores a value in a variable). == is the comparison
operator (checks equality between two values).

 Q: What is the role of input() and print()?


A: input() takes user input as a string. print() displays output to the screen.

🔹 Control Structures

 Q: What is the difference between if, if-else, and if-elif-else?


A:
o if: executes block only if condition is true.

o if-else: executes one block if true, another if false.

o if-elif-else: checks multiple conditions sequentially.

 Q: What is the difference between for and while loops?


A:
o for: used when the number of iterations is known in advance.

o while: used when iterations depend on a condition being true.

 Q: What does the break statement do?


A: It immediately exits the loop, skipping all remaining iterations.

 Q: What does the continue statement do?


A: It skips the current iteration and continues with the next one.

🔹 Functions

 Q: Why do we use functions in Python?


A: Functions make code reusable, organized, and easier to debug.

 Q: What is the difference between a function definition and a function call?


A:
o Definition: writing the function logic (def myfunc():).
o Call: executing the function (myfunc()).

🔹 Data Types & Operators

 Q: Name Python’s basic data types.


A: int, float, str, bool.

 Q: What is the difference between / and //?


A: / gives float division, // gives integer (floor) division.

🔹 Common Programs

 Q: How do you check if a number is a palindrome?


A: Convert to string and compare with its reverse (num == num[::-1]).

 Q: How do you find the largest among three numbers?


A: Use nested if or if-elif-else to compare values.

You might also like