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 to install the PyCharm Community Edition IDE. It is a free Python IDE that you can use to build Python projects. In case, you want I can also share a different tutorial for configuring Python development environment for Linux and Windows.
In case you love to code on the go, do check out Online Compiler Android app. Using this app, you can quickly write Python code snippets and execute them to check the output.
If you are just getting started, then I will recommend you to grab any of these best books to learn Python. These books will help you to learn various concepts from fundamentals to advanced topics.
Python 3 Program To Add Two Numbers
1. Adding two numbers in Python by taking input from users.
Now, there are two scenarios here which you should consider. First, do you want to add predefined numbers or you want users to enter those numbers?
Here is the Python code for both scenarios.
Let’s first see how you can allow users to specify numbers to be added. In Python, the input() function is used to take input from users. In this program, we will utilize 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 in the below code, I stored the sum in a third variable. If you want, you can also display the amount of these two numbers directly using the print() function. By showing the sum using the print() function, you can save memory, 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 is a topic that 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 also 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:
2: Adding two predefined numbers in Python.
In this program, we will directly define two numbers that we want to add. However, I don’t recommend this method as this 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:
Get code files from GitHub [Add Two Numbers.py]
Other Python 3 Programs:
- Python 3 Program To Add Two Matrices.
- Python 3 Program to Check Armstrong Number.
- Python 3 Program to Find the Sum of Natural Numbers.
- Python 3 Program To Find The Factorial Of A Number.
- Python 3 Program to Find Largest Among Three Numbers.
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator.
With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!