Python 3 Program To Add Two Numbers

0
3446
python 3 program to add two numbers

This post was last Updated on by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.

Today, we will cover a Python 3 program to add two numbers. Before proceeding, make sure you have set up a Python development environment. You can download Python from here.

I will recommend you install the PyCharm Community Edition IDE. It is a free Python IDE that you can use to build Python projects. I can also share a different tutorial for configuring the Python development environment for Linux and Windows if you want.

If you love to code on the go, check out the Online Compiler Android app. Using this app, you can quickly write Python code snippets and execute them to check the output.

Python 3 Program To Add Two Numbers

1. Adding two numbers in Python by taking input from users.

Now, there are two scenarios here that you should consider. First, do you want to add predefined numbers, or do you want users to enter those numbers?

Here is the Python code for both scenarios.

Let’s first see how to allow users to specify numbers to be added. In Python, the input() function takes input from users. In this program, we will utilize the input() function to receive two numbers as input from users and then add them using the arithmetic operation”+.”

# Store input numbers
firstNumber = input('Enter first number: ')
secondNumber = input('Enter second number: ')
# Add two numbers
addition = float(firstNumber) + float(secondNumber)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(firstNumber, secondNumber, addition))

Also, note that I stored the sum in a third variable in the code below. You can also display the amount of these two numbers directly using the print() function. You can save memory by showing the sum using the print() function, but the optimization has an unnoticeable performance boost.

But, in the case of more extensive Python programs, it can matter a lot and affect their execution time. However, this topic requires an in-depth discussion, and we will cover it some other day.

I have also used type conversion in the program, as a user can enter float values as input. The program will not work correctly if a user enters a float value and you haven’t used type conversion as data is lost while converting float datatype to integer datatype. I plan to cover this topic in greater detail, so make sure you subscribe to our newsletter to get all the latest updates.

Output:

python program to add two numbers without functions

2: Adding two predefined numbers in Python.

This program will directly define two numbers we want to add. However, I don’t recommend this method as this is not the right way to code and is also known as hard coding. I have mentioned it for the sake of knowledge.

# Store input numbers
firstNumber = 3.4
secondNumber = 4.5
# Add two numbers
addition = float(firstNumber) + float(secondNumber)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(firstNumber, secondNumber, addition))

Output:

output of python program to add two numbers

Get code files from GitHub [Add Two Numbers.py]

Other Python 3 Programs:

  1. Python 3 Program To Add Two Matrices.
  2. Python 3 Program to Check Armstrong Number.
  3. Python 3 Program to Find the Sum of Natural Numbers.
  4. Python 3 Program To Find The Factorial Of A Number.
  5. Python 3 Program to Find Largest Among Three Numbers.