➜ This Python program accepts two numbers from the user and calculates their sum.
💻 Python Code
# Program to find the sum of two numbersa=int(input(“Enter value of a: “))b=int(input(“Enter value of b: “))c=a + bprint(“Sum of given two numbers is:”, c)
💡 How the Program Works
1.input() gets values from the user.
2.int() converts the values into integers.
3.a + b calculates the sum.
4.print() displays the result.
▶ Sample Output
Enter value of a: 25
Enter value of b: 15
Sum of given two numbers is: 40
📌 Quick Note:input() returns text. Therefore,
int() is used to convert the input into a whole number.
📊 2. Average of Three Numbers
➜ This Python program accepts three numbers and calculates their average.
💻 Python Code
# Program to calculate the average of three numbersA=float(input(“Enter value of A: “))B=float(input(“Enter value of B: “))C=float(input(“Enter value of C: “))average=(A + B + C) / 3print(“Average of given 3 numbers is:”, average)
📐 Formula:
Average = (A + B + C) ÷ 3
💡 How the Program Works
1.
The user enters three numbers.
2.float() allows decimal values to be entered.
3.
The three values are added and divided by 3.
4.print() displays the calculated average.
▶ Sample Output
Enter value of A: 10
Enter value of B: 20
Enter value of C: 30
Average of given 3 numbers is: 20.0
📌 Quick Note:
To display the average with exactly two decimal places, use:
f”{average:.2f}”
◼ 3. Area of a Square
➜ This Python program accepts the side length of a square and calculates its area.
💻 Python Code
# Program to find the area of a squareL=int(input(“Enter length of square L: “))area=L * Lprint(“Area of square is:”, area)
📐 Formula:
Area of a square = L × L
💡 How the Program Works
1.
The user enters the length of the square.
2.int() converts the input into an integer.
3.L × L calculates the area.
4.print() displays the calculated area.
▶ Sample Output
Enter length of square L: 12
Area of square is: 144
📌 Quick Note:
If the side of a square is 12 units,
its area is 12 × 12 = 144 square units.
◼ 4. Area and Perimeter of a Square
➜ This Python program calculates both the area and perimeter of a square from its side length.
💻 Python Code
# Program to find the area and perimeter of a squareL=int(input(“Enter length of a square L: “))A=L * LP=4 * Lprint(“Area =”, A)print(“Perimeter =”, P)
📐 Formulas
Area = L × L
Perimeter = 4 × L
💡 How the Program Works
1.
The user enters the side length of the square.
2.L * L calculates the area.
3.4 * L calculates the perimeter.
4.
Two print() statements display the results.
▶ Sample Output
Enter length of a square L: 8
Area = 64
Perimeter = 32
📌 Quick Note:
If the side of the square is 8 units,
then Area = 8 × 8 = 64 square units and
Perimeter = 4 × 8 = 32 units.
▭ 5. Area of a Rectangle
➜ This Python program accepts the length and breadth of a rectangle and calculates its area.
💻 Python Code
# Program to find the area of a rectanglea=int(input(“Enter side length a: “))b=int(input(“Enter side length b: “))area=a * bprint(“Area of rectangle is:”, area)
📐 Formula:
Area of a rectangle =
Length × Breadth
💡 How the Program Works
1.
The user enters the length of the rectangle.
2.
The user enters the breadth of the rectangle.
3.a * b calculates the area.
4.print() displays the result.
▶ Sample Output
Enter side length a: 8
Enter side length b: 5
Area of rectangle is: 40
📌 Quick Note:
If the length is 8 units and the breadth is
5 units, then
Area = 8 × 5 = 40 square units.
🔺 6. Area of a Triangle Using Heron’s Formula
➜ This Python program calculates the area of a triangle when all three side lengths are known.
💻 Python Code
# Program to find the area of a triangle using Heron’s Formulaimportmatha=float(input(“Enter side a: “))b=float(input(“Enter side b: “))c=float(input(“Enter side c: “))s=(a + b + c) / 2area=math.sqrt(s * (s – a) * (s – b) * (s – c))print(“Area of triangle is:”, f”{area:.2f}”)
📐 Heron’s Formula
s = (a + b + c) / 2
Area = √[s(s − a)(s − b)(s − c)]
💡 How the Program Works
1.
The user enters the three sides of the triangle.
2.float() allows decimal side lengths.
3.
The program calculates the semi-perimeter s.
4.math.sqrt() calculates the square root required by Heron’s Formula.
5.
The calculated area is displayed with two decimal places.
▶ Sample Output
Enter side a: 5
Enter side b: 8
Enter side c: 10
Area of triangle is: 19.81
📌 Quick Note:
The float() function is used instead of
int() because triangle sides may contain decimal values.
🏆 7. Greatest of Two Numbers
➜ This Python program compares two numbers and displays which number is greater.
💻 Python Code
# Program to find the greatest of two numbersA=int(input(“Enter value of A: “))B=int(input(“Enter value of B: “))ifA > B:print(“A is Larger”)else:print(“B is Larger”)
🧠 Decision Logic
If A > B → A is larger
Otherwise → B is larger
💡 How the Program Works
1.
The user enters two numbers.
2.
The if condition checks whether A is greater than B.
3.
If the condition is true, the program prints “A is Larger”.
4.
Otherwise, else prints “B is Larger”.
▶ Sample Output
Enter value of A: 25
Enter value of B: 18
A is Larger
📌 Quick Note:
Python uses if and else to make decisions
based on conditions. Notice that the statements inside the
if and else blocks are indented.
🔄 8. Swap Two Numbers Using a Temporary Variable
➜ This Python program swaps the values of two numbers using a temporary variable.
💻 Python Code
# Program to swap two numbers using a temporary variablea=int(input(“Enter value of a: “))b=int(input(“Enter value of b: “))c=aa=bb=cprint(“Values of a & b after swapping:”)print(“a =”, a)print(“b =”, b)
🔄 How Swapping Works
c = a → Save the value of a
a = b → Put b into a
b = c → Put the original a into b
💡 How the Program Works
1.
The user enters two numbers.
2.
The original value of a is stored in c.
3.
The value of b is assigned to a.
4.
The saved value in c is assigned to b.
▶ Sample Output
Enter value of a: 15
Enter value of b: 25
Values of a & b after swapping:
a = 25
b = 15
📌 Quick Note:
The temporary variable c safely stores the original value
of a so that it is not lost during the swap.
🔄 9. Swap Two Numbers Without a Temporary Variable
➜ Python allows two variables to be swapped directly without using an extra variable.
💻 Python Code
# Program to swap two numbers without a temporary variablea=int(input(“Enter value of a: “))b=int(input(“Enter value of b: “))a, b=b, aprint(“Values of a & b after swapping:”)print(“a =”, a)print(“b =”, b)
🔄 Python Swapping Trick
a, b = b, a
Python exchanges the values of a and b
in a single statement.
💡 How the Program Works
1.
The user enters two numbers.
2.
Python evaluates b, a on the right side.
3.
The values are assigned to a and b in reverse order.
4.
The swapped values are displayed using print().
🔎 Before & After Swapping
Before: a = 15 b = 25
After: a = 25 b = 15
▶ Sample Output
Enter value of a: 15
Enter value of b: 25
Values of a & b after swapping:
a = 25
b = 15
📌 Quick Note:
Unlike the previous method, this approach does not require a temporary
variable such as c. Python handles the exchange directly
using a, b = b, a.
💰 10. Calculate Simple Interest
➜ This Python program calculates Simple Interest using the principal amount,
number of years, and rate of interest.
💻 Python Code
# Program to calculate Simple InterestP=float(input(“Enter Principal Amount (P): “))N=float(input(“Enter Number of Years (N): “))R=float(input(“Enter Rate of Interest (R): “))SI=(P * N * R) / 100print(f”Simple Interest is: {SI:.2f}”)
📐 Simple Interest Formula
SI = (P × N × R) / 100
P = Principal |
N = Number of Years |
R = Rate of Interest
💡 How the Program Works
1.
The user enters the principal amount, number of years, and rate of interest.
2.float() allows decimal values to be entered.
3.
The formula (P × N × R) / 100 is used to calculate SI.
4.
The result is displayed with two decimal places.
▶ Sample Output
Enter Principal Amount (P): 10000
Enter Number of Years (N): 2
Enter Rate of Interest (R): 8
Simple Interest is: 1600.00
📌 Quick Note:
If P = ₹10,000, N = 2 years, and R = 8%, then
SI = (10,000 × 2 × 8) / 100 = ₹1,600.
🌡️ 11. Convert Fahrenheit to Celsius
➜ This Python program converts a temperature from Fahrenheit to Celsius.
💻 Python Code
# Program to convert Fahrenheit to CelsiusF=float(input(“Enter temperature in Fahrenheit: “))C=(F – 32) * 5 / 9print(f”Temperature in Celsius is: {C:.2f}”)
🌡️ Conversion Formula
Celsius = (Fahrenheit − 32) × 5 / 9
💡 How the Program Works
1.
The user enters a temperature in Fahrenheit.
2.float() allows decimal temperatures such as 98.6.
3.
The conversion formula is applied to calculate Celsius.
4.
The result is displayed with two decimal places.
▶ Sample Output
Enter temperature in Fahrenheit: 98.6
Temperature in Celsius is: 37.00
📌 Quick Note:98.6°F is approximately 37°C.
The :.2f format displays the result with exactly two decimal places.
Hey Engineers, welcome to the award-winning blog,Engineers Tutor.
I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India.
Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".