Python Program To Add Two Numbers

1. Introduction

Adding two numbers is one of the most basic arithmetic operations and serves as an introductory concept in programming. This operation can be performed in Python using a simple program that reads two numbers from the user, adds them together, and then prints the result. This blog post will guide you through writing a Python program to add two numbers, demonstrating the simplicity and elegance of performing arithmetic operations in Python.

2. Program Steps

1. Prompt the user to input the first number.

2. Prompt the user to input the second number.

3. Convert the input from strings to numbers that can be added together.

4. Add the two numbers.

5. Display the sum to the user.

3. Code Program

# Step 1: Prompt the user for the first number
first_number = input("Enter the first number: ")

# Step 2: Prompt the user for the second number
second_number = input("Enter the second number: ")

# Step 3: Convert the input strings to integers
first_number = int(first_number)
second_number = int(second_number)

# Step 4: Add the two numbers
sum = first_number + second_number

# Step 5: Display the sum
print("The sum of the two numbers is:", sum)

Output:

Enter the first number: 5
Enter the second number: 7
The sum of the two numbers is: 12

Explanation:

1. The program starts by asking the user to input two numbers. These inputs are captured as strings by the input function.

2. To perform arithmetic operations, these strings must be converted into integers. The program converts the input strings to integers using the int function.

3. After conversion, the two numbers are added together using the + operator, and the result is stored in the variable sum.

4. Finally, the program prints the result, displaying the sum of the two input numbers. This simple program illustrates the basic structure of a Python program and how to perform arithmetic operations and handle user input.


Comments