Programming Fundamentals in Python
1) Aim: - Write a Python code to input three numbers. Display the sum and
average of three numbers.
Sample Input: 64, 72, 65
Sample Output: Sum= 201
Average= 67.0
Source code: -
# Input three numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Calculate the sum of the numbers
total_sum = num1 + num2 + num3
# Calculate the average of the numbers
average = total_sum / 3
# Display the sum and average
print(f"The sum of the three numbers is: {total_sum}")
print(f"The average of the three numbers is: {average}")
Output of the code: -
Enter the first number: 64
Enter the second number: 72
Enter the third number: 65
The sum of the three numbers is: 201.0
The average of the three numbers is: 67.0
Conclusion: - The program was executed successfully.
2) Aim: - Write a Python code to calculate and display the following:
(a) Area and perimeter of a square
(b) Area and perimeter of a rectangle
Take side of square, and length and breadth of rectangle as inputs.
Source code: -
# (a) Area and Perimeter of Square
# Input the side of the square
side = float(input("Enter the side of the square: "))
# Calculate the area and perimeter of the square
area_square = side ** 2
perimeter_square = 4 * side
# Display the area and perimeter of the square
print(f"Area of the square: {area_square}")
print(f"Perimeter of the square: {perimeter_square}")
# (b) Area and Perimeter of Rectangle
# Input the length and breadth of the rectangle
length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))
# Calculate the area and perimeter of the rectangle
area_rectangle = length * breadth
perimeter_rectangle = 2 * (length + breadth)
# Display the area and perimeter of the rectangle
print(f"Area of the rectangle: {area_rectangle}")
print(f"Perimeter of the rectangle: {perimeter_rectangle}")
Output of code: -
Enter the side of the square: 15
Area of the square: 225.0
Perimeter of the square: 60.0
Enter the length of the rectangle: 18
Enter the breadth of the rectangle: 12
Area of the rectangle: 216.0
Perimeter of the rectangle: 60.0
Conclusion: - The program was executed successfully.
3) Aim: - Write a Python code to input the name and basic salary of
an employee. Calculate and display the name, gross salary and
net salary of the employee when:
da = 30% of basic
hra=18% of basic
pf = 12.5% of basic
gross=basic+da+hra
net=gross-pf
Source code: -
# Input the name and basic salary of the employee
name = input("Enter the employee's name: ")
basic_salary = float(input("Enter the basic salary of the employee: "))
# Calculate DA, HRA, and PF
da = 0.30 * basic_salary # 30% of basic salary
hra = 0.18 * basic_salary # 18% of basic salary
pf = 0.125 * basic_salary # 12.5% of basic salary
# Calculate gross salary
gross_salary = basic_salary + da + hra
# Calculate net salary
net_salary = gross_salary - pf
# Display the name, gross salary, and net salary
print(f"Employee Name: {name}")
print(f"Gross Salary: {gross_salary:.2f}")
print(f"Net Salary: {net_salary:.2f}")
Output of code: -
Enter the employee's name: Amit Jaiswal
Enter the basic salary of the employee: 52000
Employee Name: Amit Jaiswal
Gross Salary: 76960.00
Net Salary: 70460.00
Conclusion: - The program was executed successfully.
4) Aim: - A jewellery shop charges 20% making charges on gold price for
purchasing any ornament. However, the shopkeeper offers a special discount of
30% to their customers on making charges.
Write a Python code to input gold price/gm and weight of the ornament. Now,
calculate and display the following:
(a) Actual cost of the ornament
(b) Actual making charges
(c) Discount on making charges
(d) Amount to be paid by the customer
Sample Input: Gold price/gm = 5200 Weight of ornament = 10.4 gm.
Sample Output: Actual cost = ₹ 54080 Actual making charges = 10816 Discount
on making charges = 3244.8 Amount to be paid by the customer = 61651.2
Source code: -
# Input the gold price per gram and the weight of the ornament
gold_price_per_gm = float(input("Enter the gold price per gram: "))
weight_of_ornament = float(input("Enter the weight of the ornament in gm: "))
# (a) Calculate the actual cost of the ornament
actual_cost = gold_price_per_gm * weight_of_ornament
# (b) Calculate the actual making charges (20% of the actual cost)
making_charges = 0.20 * actual_cost
# (c) Calculate the discount on making charges (30% of making charges)
discount_on_making_charges = 0.30 * making_charges
# (d) Calculate the amount to be paid by the customer
amount_to_be_paid = actual_cost + (making_charges - discount_on_making_charges)
# Display the results
print(f"Actual cost of the ornament: ₹{actual_cost:.2f}")
print(f"Actual making charges: ₹{making_charges:.2f}")
print(f"Discount on making charges: ₹{discount_on_making_charges:.2f}")
print(f"Amount to be paid by the customer: ₹{amount_to_be_paid:.2f}")
Output of code: -
Enter the gold price per gram: 5200
Enter the weight of the ornament in gm: 10.4
Actual cost of the ornament: ₹54080.00
Actual making charges: ₹10816.00
Discount on making charges: ₹3244.80
Amount to be paid by the customer: ₹61651.20
Conclusion: - The program was executed successfully.
5) Aim: - Write a Python code to input two numbers. Display the
numbers after swapping them without using built-in functions or a
third variable. Sample Input: a = 95 b = 46
Sample Output: a = 46 b = 95
Source code: -
# Input two numbers
a = int(input("Enter the first number (a): "))
b = int(input("Enter the second number (b): "))
# Swap the numbers without using a third variable
a = a + b # Step 1: a now contains the sum of a and b
b = a - b # Step 2: b now contains the original value of a
a = a - b # Step 3: a now contains the original value of b
# Display the numbers after swapping
print(f"After swapping:")
print(f"a = {a}")
print(f"b = {b}")
Output of code: -
Enter the first number (a): 95
Enter the second number (b): 46
After swapping:
a = 46
b = 95
Conclusion: - The program was executed successfully.
6) Aim: - Write a Python code to input three numbers and display the smallest
number.
[Hint: Use min() function]
Sample Input: a = 58, b= 46, c= 99
Sample Output: Smallest Number = 46
Source code: -
# Input three numbers
a = int(input("Enter the first number (a): "))
b = int(input("Enter the second number (b): "))
c = int(input("Enter the third number (c): "))
# Find the smallest number using the min() function
smallest_number = min(a, b, c)
# Display the smallest number
print(f"Smallest Number = {smallest_number}")
Output of code: -
Enter the first number (a): 58
Enter the second number (b): 46
Enter the third number (c): 99
Smallest Number = 46
Conclusion: - The program was executed successfully.
7) Aim: - The final velocity of a vehicle can be calculated by using the formula: v²
= u² + 2as where, u = initial velocity, v = final velocity, a = acceleration and d =
distance covered
Write a Python code to calculate and display the final velocity by taking initial
velocity, acceleration and distance covered as inputs
Source code: -
import math
# Input initial velocity, acceleration, and distance covered
u = float(input("Enter the initial velocity (u) in m/s: "))
a = float(input("Enter the acceleration (a) in m/s²: "))
s = float(input("Enter the distance covered (s) in meters: "))
# Calculate the final velocity using the formula v² = u² + 2as
v_squared = u**2 + 2 * a * s
# Calculate the square root of v_squared to get the final velocity
v = math.sqrt(v_squared)
# Display the final velocity
print(f"The final velocity (v) is: {v:.2f} m/s")
Output of code: -
Enter the initial velocity (u) in m/s: 40
Enter the acceleration (a) in m/s²: 20
Enter the distance covered (s) in meters: 50
The final velocity (v) is: 60.00 m/s
Conclusion: - The program was executed successfully.
8) Aim: - Write a Python code to find the difference between Simple Interest (SI)
and Compound Interest (CI) taking Principal, Rate and Time as inputs.
[Hint: CI = A - P where, A=P^ * (1 + R/100) ^ T]
Source code: -
# Input Principal, Rate, and Time
P = float(input("Enter the Principal amount (P): "))
R = float(input("Enter the Rate of interest (R) in percentage: "))
T = float(input("Enter the Time period (T) in years: "))
# Calculate Simple Interest (SI)
SI = (P * R * T) / 100
# Calculate Compound Interest (CI)
A = P * (1 + R / 100) ** T # Final amount
CI = A - P # Compound Interest
# Calculate the difference between CI and SI
difference = CI - SI
# Display the results
print(f"Simple Interest (SI): {SI:}")
print(f"Compound Interest (CI): {CI:}")
print(f"Difference between CI and SI: {difference:}")
Output of code: -
Enter the Principal amount (P): 5000
Enter the Rate of interest (R) in percentage: 10
Enter the Time period (T) in years: 2
Simple Interest (SI): 1000.0
Compound Interest (CI): 1050.000000000001
Difference between CI and SI: 50.00000000000091
Conclusion: - The program was executed successfully.
9) Aim: - Write a Python code to accept the number of days and display the result
after converting it into number of years, number of months and the remaining
number of days
Source code: -
# Input the number of days
total_days = int(input("Enter the number of days: "))
# Constants for days in a year and a month
days_in_year = 365
days_in_month = 30
# Calculate the number of years
years = total_days // days_in_year
remaining_days_after_years = total_days % days_in_year
# Calculate the number of months
months = remaining_days_after_years // days_in_month
remaining_days = remaining_days_after_years % days_in_month
# Display the result
print(f"{total_days} days is equivalent to:")
print(f"{years} years")
print(f"{months} months")
print(f"{remaining_days} days")
Output of code: -
Enter the number of days: 842
842 days is equivalent to:
2 years
3 months
22 days
Conclusion: - The program was executed successfully.
10) Aim: - Write a Python code to enter the three sides of a scalene triangle and
calculate the area of the scalene triangle by using the formula: Area= sqrt(s(s - a)(s
- b)(s - c)) where, s = (a + b + c)/2
[Hint: The sum of any two sides must be greater third side.]
Source code: -
import math
# Input the three sides of the triangle
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))
# Check if the sides form a valid triangle
if (a + b > c) and (a + c > b) and (b + c > a):
# Calculate the semi-perimeter
s = (a + b + c) / 2
# Calculate the area using Heron's formula
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
# Display the area
print(f"The area of the scalene triangle is: {area:}")
else:
print("The given sides do not form a valid triangle.")
Output of code: -
Enter the length of side a: 12
Enter the length of side b: 18
Enter the length of side c: 20
The area of the scalene triangle is: 106.6536450385077
Conclusion: - The program was executed successfully.
11) Aim: - Write a Python code to input a three-digit number. Find
and display the sum of the first digit and the third digit, without
using a loop. Sample Input: 653
Sample Output: 6+3=9
Source code: -
# Input a three-digit number
number = int(input("Enter a three-digit number: "))
# Extract the digits
first_digit = number // 100 # Get the first digit
third_digit = number % 10 # Get the third digit
# Calculate the sum
sum_digits = first_digit + third_digit
# Display the result
print(f"{first_digit} + {third_digit} = {sum_digits}")
Output of code: -
Enter a three-digit number: 653
6 + 3 = 9
Conclusion: - The program was executed successfully.
12) Aim: - Write a Python code to display three rational numbers between any two
consecutive natural numbers, taking inputs from the console.
Sample Input: 7 and 8
Sample Output: 7.25
7.5
7.75
[Hint: Rational number between m and n can be calculated as: (m+n)/2].
Source code: -
# Input two consecutive natural numbers
m = int(input("Enter the first natural number (m): "))
n = int(input("Enter the second natural number (n): "))
# Ensure the numbers are consecutive
if n != m + 1:
print("The numbers are not consecutive natural numbers.")
else:
# Calculate three rational numbers between m and n
step = (n - m) / 4 # We need 3 steps between m and n, so divide the interval
into 4 parts
# Generate and display the three rational numbers
for i in range(1, 4):
rational_number = m + i * step
print(f"{rational_number:}")
Output of code: -
Perimeter of the reEnter the first natural number (m): 7
Enter the second natural number (n): 8
7.25
7.5
7.75ctangle: 60.0
Conclusion: - The program was executed successfully.