P1. Write a program to calculate area and perimeter of a rectangle.
length = float(input("Enter length of the rectangle: "))
breadth = float(input("Enter breadth of the rectangle: "))
# Calculating area
area = length * breadth
# Calculating perimeter
perimeter = 2 * (length * breadth)
# Displaying results
print("Area of rectangle = ", area)
print("Perimeter of rectangle = ", perimeter)
Output
Enter length of the rectangle: 23
Enter breadth of the rectangle: 17
Area of rectangle = 391.0
Perimeter of rectangle = 782.0
P2: Write a program to enter marks in 5 subjects , calculate and display total
and average marks.
m1 = int(input("Enter first subject marks: "))
m2 = int(input("Enter second subject marks: "))
m3 = int(input("Enter third subject marks: "))
m4 = int(input("Enter fourth subject marks: "))
m5 = int(input("Enter fifth subject marks: "))
avg = (m1 + m2+ m3+ m4 + m5) / 5;
print("Average Marks =", avg)
OUTPUT
Enter first subject marks: 65
Enter second subject marks: 78
Enter third subject marks: 79
Enter fourth subject marks: 80
Enter fifth subject marks: 85
Average Marks = 77.4
P3:write a program to print sum of first 10 natural number.
Sum of natural numbers up to num
num = 16
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
Output
The sum is 136
P4: write a program to calculate simple interest if the principle_amount=2000 rate_of_interest=8 time=10.
P=2000
T=10
R=8
SI=(P*T*R)/100
print(“Simple Interest is”,SI)
o/p: Simple Interest is 1600.0
P5: write code to check whether a person is eligible to vote or not.
# input age
age = int(input("Enter Age : "))
# condition to check voting eligibility
if age >= 18:
status = "Eligible"
else:
status = "Not Eligible"
print("You are ", status, " for Vote.")
Output
The output of the above program is:
RUN 1:
Enter Age : 21
You are Eligible for Vote.
RUN 2:
Enter Age : 17
You are Not Eligible for Vote.
P6: write a program to add the elements of the two lists.
l1=[20,30,40]
l2=[30,50,10]
l3=l1+l2
print("Addition of",l1,"and",l2,"is",l3)
o/p:
Addition of [20, 30, 40] and [30, 50, 10] is [20, 30, 40, 30, 50, 10]
P7: Write a program to calculate mean, median and mode using Numpy.
import numpy as np
import statistics as
st l=[30,20,50,60,20]
l1=np.array(l)
print("Mean of",l1,"is",st.mean(l1))
print("Median of",l1,"is",st.median(l1))
print("Mode of",l1,"is",st.mode(l1))
o/p:
Mean of [30 20 50 60 20] is 36
Median of [30 20 50 60 20] is 30
Mode of [30 20 50 60 20] is 20
P 8: Write a program to display line chart from (2,5) to (9,10).
import matplotlib.pyplot as plt
x=(2,9)
y=(5,10)
plt.plot(x,y)
plt.title("Line chart")
plt.show()
o/p: output shows in page no 193 in study material.
P 9: Write a program to display a scatter chart for the following points (2,5), (9,10),(8,3),(5,7),(6,18).
import matplotlib.pyplot as plt
x=[2,9,8,5,6]
y=[5,10,3,7,18]
plt.scatter(x,y)
plt.title("Line chart")
plt.show()
o/p: output shows page no 193 in study material.
P 10: Write a program to display bar chart for the following data with appropriate titles:
Subjects=[“Eng”,”Sci”,”Soc”,”Maths”,”AI”]
Marks=[89,87,78,90,99] i
mport matplotlib.pyplot as plt
Sub=["Eng","Sci","Soc","Maths","AI"]
Marks=[89,87,78,90,99] plt.bar(Sub,Marks)
plt.title("Term-1 Performance")
plt.xlabel("Subjects")
plt.ylabel("Marks")
plt.show() O/p: shows in page no 194 in study material
P11: To find the square of number 7.
# Declaring the number.
n = 7
# Finding square by multiplying them
# with each other
square = n * n
# Printing square
print(square)
Output:
49
P12: To find the sum, difference, multiplication and division of two number 15 and 20.
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
print("Enter which operation would you like to perform?")
ch = input("Enter any of these char for specific operation
+,-,*,/: ")
result = 0
if ch == '+':
result = num1 + num2
elif ch == '-':
result = num1 - num2
elif ch == '*':
result = num1 * num2
elif ch == '/':
result = num1 / num2
else:
print("Input character is not recognized!")
print(num1, ch , num2, ":", result)
Output 1: Addition
Enter First Number: 100
Enter Second Number: 5
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: +
100 + 5 : 105
Output 2: Division
Enter First Number: 20
Enter Second Number: 5
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: /
20 / 5 : 4.0
Output 3: Subtraction
Enter First Number: 8
Enter Second Number: 7
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: -
8 - 7 : 1
Output 4: Multiplication
Enter First Number: 6
Enter Second Number: 8
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: *
6 * 8 : 48
P13:To calculate Area of a triangle with base and height.
b = int(input("Input the base : "))
h = int(input("Input the height : "))
# Calculate the area of the triangle using the formula: (base *
height) / 2.
area = b * h / 2
# Print the calculated area of the triangle.
print("area = ", area)
Sample Output:
Input the base : 20
Input the height : 40
area = 400.0
P 14: Input a number and check if the number is positive, negative or zero and display an appropriate message.
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
output:
Enter a number: 5
Positive number
P15 : To print the following patterns using multiple print commands-
*
* *
* * *
* * * *
* * * * *
# Function to print a half pyramid pattern
def half_pyramid(n):
for i in range(1, n + 1):
for j in range(1, i + 1):
print("* ", end="")
print("")
# Example: Print a half pyramid with 5 rows
n = 5
half_pyramid(n)
Output:
* *
* * *
* * * *
* * * * *
print("\r")