Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 11

Practice Programs: Swap, Interest, Circle, Rectangle

Apply input() and print() to swap values, simple interest, and area/perimeter of a circle and rectangle.

Beginner20 min

What you will learn

By the end of this lesson, you'll apply:

  • How do you use input() for keyboard input?
  • How do you use float() for numeric conversion?
  • How do you use print() and format() for formatted output?
  • How do you use variables and arithmetic formulas in four practical Batch Mode programs?

Four practical programs

These programs represent the complete beginner workflow:

Four programs: Swap, Simple Interest, Circle, Rectangle

Each program combines the concepts you've learned in Lessons 1–9.

1. Swap two values (SwapLogic1.py)

Exchange the values of two variables using a temporary variable.

Before swapping:

a = Apple and b = Ball

After swapping:

a = Ball and b = Apple
python
# SwapLogic1.py
a = input("Enter Val of a:")
b = input("Enter Val of b:")
print("=" * 50)
print("Orignal value of a:{}".format(a))
print("Orignal value of b:{}".format(b))
print("-" * 50)
k = a  # k is called Temporary variable
a = b
b = k
print("Swapped value of a:{}".format(a))
print("Swapped value of b:{}".format(b))
print("=" * 50)

The source prints Orignal (that spelling is in SwapLogic1.py).

text
Enter Val of a:10
Enter Val of b:20
==================================================
Orignal value of a:10
Orignal value of b:20
--------------------------------------------------
Swapped value of a:20
Swapped value of b:10
==================================================
Swap steps: k = a, a = b, b = k

Because input() returns str, this swaps text values. Use float() or int() if numeric calculations are needed.

2. Simple interest (SimpleInt.py)

text
SI = (P * T * R) / 100
Total Amount = P + SI
Symbol Meaning
P Principal amount
T Time
R Rate of interest
SI Simple interest
python
# SimpleInt.py
p = float(input("Enter principle Amount:"))
t = float(input("Enter Time:"))
r = float(input("Enter Rate of Interest:"))
si = (p * t * r) / 100
totamt = p + si
print("*" * 50)
print("\t\tResults of Simple Interest")
print("*" * 50)
print("\t\tPrinciple Amount:{}".format(p))
print("\t\tTime:{}".format(t))
print("\t\tRate of Interest:{}".format(r))
print("\t\tSimple Interest:{}".format(si))
print("\t\tTOTAL AMOUNT TO PAY:{}".format(totamt))
print("*" * 50)

The source prompt is Enter principle Amount: (that spelling is in SimpleInt.py). \t is a tab.

text
Enter principle Amount:10000
Enter Time:2
Enter Rate of Interest:5
**************************************************
		Results of Simple Interest
**************************************************
		Principle Amount:10000.0
		Time:2.0
		Rate of Interest:5.0
		Simple Interest:1000.0
		TOTAL AMOUNT TO PAY:11000.0
**************************************************
P times T times R, divide by 100, SI = 1000

3. Area and perimeter of a circle (CircleEx.py)

text
Area = 3.14 * r * r
Perimeter = 2 * 3.14 * r

Where r is the radius.

python
# CircleEx.py
rad = float(input("Enter Radius:"))
ac = 3.14 * rad * rad
pc = 2 * 3.14 * rad
print("*" * 50)
print("Radius={}".format(rad))
print("Area of Circle={}".format(ac))
print("Perimter if Circle={}".format(pc))
print("*" * 50)

The source prints Perimter if Circle (that spelling is in CircleEx.py).

text
Enter Radius:7
**************************************************
Radius=7.0
Area of Circle=153.86
Perimter if Circle=43.96
**************************************************
Circle with radius r from the center

4. Area and perimeter of a rectangle (RectEx.py)

text
Area = L * B
Perimeter = 2 * (L + B)

Where L is length and B is breadth.

python
# RectEx.py
length = float(input("Enter Length:"))
breadth = float(input("Enter Breadth:"))
ra = length * breadth
rp = 2 * (length + breadth)
print("=" * 50)
print("Length={}".format(length))
print("Breadth={}".format(breadth))
print("Area of Rect={}".format(ra))
print("Perimeter of Rect={}".format(rp))
print("*" * 50)
text
Enter Length:10
Enter Breadth:5
==================================================
Length=10.0
Breadth=5.0
Area of Rect=50.0
Perimeter of Rect=30.0
**************************************************
Rectangle labeled Length and Breadth

String repetition reminder

Your source also demonstrates that * behaves differently for strings (RectEx.py notes).

python
print("3" * 4)
text
3333
python
print("2+3" * 2)
text
2+32+3
python
print(str(2 + 3) * 4)
text
5555
Expression Result Reason
"3" * 4 3333 Repeat string
"2+3" * 2 2+32+3 Repeat text
3 * 4 12 Numeric multiplication

In these practice programs, "*" * 50 creates separator lines — it is string repetition, not arithmetic.

What you have learned so far

Complete beginner workflow:

  1. Write a Batch Mode program (.py)
  2. Read values using input()
  3. Convert with float()
  4. Perform calculations
  5. Display formatted results with print() and format()

These four programs combine every concept from Lessons 1–10 into practical applications.

Mini project challenge

Write one Batch Mode program that asks for length and breadth, then displays:

  • Area
  • Perimeter
  • A separator line using "=" * 40

Expected format:

text
========================================
Length : 12
Breadth: 8
Area   : 96
Perimeter: 40
========================================

Try building it yourself before looking back at RectEx.py.

Key terms

Term Meaning
Temporary variable A helper variable used during swapping (k)
Principal Original amount of money
Radius Distance from the center of a circle
Perimeter Total boundary length
String repetition Repeating text using *

Summary

  • Swapping uses a temporary variable (k) in SwapLogic1.py
  • Financial and geometry formulas require float() for numeric calculations
  • print() with format() produces clean, labeled console output
  • "*" * 50 and "=" * 50 are examples of string repetition
  • You have now completed the full Module 02 workflow: Program → Print → Input → Type Casting → Practice Programs

Quick knowledge check

  1. Which variable temporarily stores a value during swapping?
    k

  2. What is the simple interest formula?
    (P * T * R) / 100

  3. What is the area formula for a rectangle?
    L * B

  4. Is "*" * 50 arithmetic multiplication?
    No, it is string repetition.

Next on the Python path: Data Types (coming soon).