Lesson 10
Adding Two Numbers
Three batch-mode programs: hard-coded add, labeled print(), then dynamic float input.
What you will learn
By the end of this lesson, you'll connect three concepts into one complete program:
- What is Batch Mode (
.pyfile)? - How do you use
print()for displaying results? - How do you use
input()withfloat()for dynamic input?
The goal is to add two numbers using three different approaches.
The evolution of one program
The same addition problem can be written in three versions.
Each version solves the same problem, but becomes more practical.
Version 1 — hard-coded values (addex1.py)
This is the simplest Batch Mode program.
# addex1.py
a = 10
b = 20
c = a + b
print(a)
print(b)
print(c)10
20
30Use it for testing the + operator, learning variables, and understanding arithmetic.
Limitation: to add different numbers, you must edit the source code.
Version 2 — messages cum values (addex2.py)
Now we improve the output using print().
# addex2.py
a = 10
b = 20
c = a + b
print("*" * 50)
print("Val of a=", a)
print("Val of b=", b)
print("sum=", c)
print("*" * 50)**************************************************
Val of a= 10
Val of b= 20
sum= 30
**************************************************What does "*" * 50 mean?
This is string repetition, not multiplication.
print("*" * 10)**********This is commonly used to create separators in console programs.
Version 3 — dynamic input (addex3.py)
This is the real Batch Mode pattern from your notes.
# addex3.py
a = float(input("Enter First Value:"))
b = float(input("Enter Second Value:"))
c = a + b
print("=" * 50)
print("Val of a={}".format(a))
print("Val of b={}".format(b))
print("sum({},{})={}".format(a, b, c))
print("=" * 50)Enter First Value:3
Enter Second Value:6.7
==================================================
Val of a=3.0
Val of b=6.7
sum(3.0,6.7)=9.7
==================================================Classroom notes sometimes showed this run as Addopex3.py. In this zip the file is addex3.py — use that name.
Run from the command prompt:
python addex3.pyStep-by-step execution
Suppose the user enters 3 and 6.7.
The program performs three stages:
- Read strings
- Convert to floats
- Add the numbers
Comparing all three versions
| Version | Input | Output style | Reusable? |
|---|---|---|---|
| 1 | Hard-coded | Values only | No |
| 2 | Hard-coded | Messages + values | No |
| 3 | Keyboard | Formatted output | Yes |
Which version should you use?
- Version 1 — checking that
+works correctly - Version 2 — practicing
print()with labels and formatted console output - Version 3 (recommended) — real programs that accept keyboard input dynamically
Complete practical program (AddTwoPracticalEx.py)
# AddTwoPracticalEx.py
a = float(input("Enter First Value: "))
b = float(input("Enter Second Value: "))
c = a + b
print("=" * 40)
print("First Value :", a)
print("Second Value:", b)
print("Sum :", c)
print("=" * 40)Enter First Value: 12.5
Enter Second Value: 7.5
========================================
First Value : 12.5
Second Value: 7.5
Sum : 20.0
========================================This combines Batch Mode + input() + float() + print() into one complete application.
Key terms
| Term | Meaning |
|---|---|
| Hard-coded | Values written directly in the source code |
| Dynamic input | Values entered by the user during execution |
| Batch Mode | A saved .py program |
| String repetition | "*" * 50 creates repeated characters |
| Formatted output | Displaying readable messages with values |
Summary
- The same addition operation can be written in three versions
- Version 1 is useful for testing arithmetic (
addex1.py) - Version 2 improves readability with labeled
print()statements (addex2.py) - Version 3 uses
float(input(...)), making the program reusable and interactive (addex3.py) - String repetition like
"=" * 50creates clean console separators
Quick knowledge check
-
Which version accepts user input?
Version 3 -
What does
"*" * 50do?
Prints 50 asterisks. -
Why do we use
float(input())?
To convert keyboard strings into numeric values. -
Which version is closest to real Batch Mode applications?
Version 3
Next lesson: build four practical programs — Swap Two Numbers, Simple Interest, Area of Circle, and Area of Rectangle.