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

Lesson 10

Adding Two Numbers

Three batch-mode programs: hard-coded add, labeled print(), then dynamic float input.

Beginner15 min

What you will learn

By the end of this lesson, you'll connect three concepts into one complete program:

  • What is Batch Mode (.py file)?
  • How do you use print() for displaying results?
  • How do you use input() with float() 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.

Version 1 hard-coded, Version 2 labeled output, Version 3 dynamic input

Each version solves the same problem, but becomes more practical.

Version 1 — hard-coded values (addex1.py)

This is the simplest Batch Mode program.

python
# addex1.py
a = 10
b = 20
c = a + b
print(a)
print(b)
print(c)
text
10
20
30
a=10 plus b=20 becomes c=30

Use 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().

python
# 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)
text
**************************************************
Val of a= 10
Val of b= 20
sum= 30
**************************************************

What does "*" * 50 mean?

This is string repetition, not multiplication.

python
print("*" * 10)
text
**********
 "*" times 10 becomes a line of ten asterisks

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.

python
# 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)
text
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:

text
python addex3.py

Step-by-step execution

Suppose the user enters 3 and 6.7.

input 3 and 6.7, float conversion, sum 9.7

The program performs three stages:

  1. Read strings
  2. Convert to floats
  3. 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)

python
# 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)
text
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 "=" * 50 creates clean console separators

Quick knowledge check

  1. Which version accepts user input?
    Version 3

  2. What does "*" * 50 do?
    Prints 50 asterisks.

  3. Why do we use float(input())?
    To convert keyboard strings into numeric values.

  4. 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.