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.
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()andformat()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:
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:
After swapping:
# 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).
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
==================================================Because input() returns str, this swaps text values. Use float() or int() if numeric calculations are needed.
2. Simple interest (SimpleInt.py)
SI = (P * T * R) / 100
Total Amount = P + SI| Symbol | Meaning |
|---|---|
P |
Principal amount |
T |
Time |
R |
Rate of interest |
SI |
Simple interest |
# 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.
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
**************************************************3. Area and perimeter of a circle (CircleEx.py)
Area = 3.14 * r * r
Perimeter = 2 * 3.14 * rWhere r is the radius.
# 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).
Enter Radius:7
**************************************************
Radius=7.0
Area of Circle=153.86
Perimter if Circle=43.96
**************************************************4. Area and perimeter of a rectangle (RectEx.py)
Area = L * B
Perimeter = 2 * (L + B)Where L is length and B is breadth.
# 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)Enter Length:10
Enter Breadth:5
==================================================
Length=10.0
Breadth=5.0
Area of Rect=50.0
Perimeter of Rect=30.0
**************************************************String repetition reminder
Your source also demonstrates that * behaves differently for strings (RectEx.py notes).
print("3" * 4)3333print("2+3" * 2)2+32+3print(str(2 + 3) * 4)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:
- Write a Batch Mode program (
.py) - Read values using
input() - Convert with
float() - Perform calculations
- Display formatted results with
print()andformat()
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:
========================================
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) inSwapLogic1.py - Financial and geometry formulas require
float()for numeric calculations print()withformat()produces clean, labeled console output"*" * 50and"=" * 50are examples of string repetition- You have now completed the full Module 02 workflow: Program → Print → Input → Type Casting → Practice Programs
Quick knowledge check
-
Which variable temporarily stores a value during swapping?
k -
What is the simple interest formula?
(P * T * R) / 100 -
What is the area formula for a rectangle?
L * B -
Is
"*" * 50arithmetic multiplication?
No, it is string repetition.
Next on the Python path: Data Types (coming soon).