In this article, we will look at how to write a Python program to add two numbers. This is a very simple Python program that is suitable for even a beginner in Python programming to work on it for getting hands on practice with Python.
In order to add two numbers in Python program, we need to first break down the problem into the following steps:
Breakdown of the problem of Adding two numbers
- Receive the first input number from the user and store it in a Python variable.
- Receive the second input number from the user and store it in another Python variable.
- Add the two numbers by adding the two Python variables using a Python statement (Learn about Python statement here)
- Store the final result in another Python variable called “result”
- Print the value of the “result” variable.
In the above breakdown of the problem, you will notice that the Python program we will be written in such a way that the two numbers that need to be added will not be hard coded directly into the program itself but instead is written in a generic way such that we prompt the user to enter these two input values every time the Python program to add two numbers is run. This type of programming approach is often called general programming as the program is generic enough to receive any two different values each time it is run.
Pseudo-code To Add Two Numbers Using Python Programming Language
num1 = Receive First Input Number From The User num1 = Receive Second Input Number From The User result = (num1) Added to (num2) Print the (result) on the screen
We can see from the above psuedo code that this is a simple program that receives two numbers from the user, adds them and print their results back on the screen. The above pseudo code gives us a nice little framework on how to write our program. The same pseudo-code can now be used to write a program to add two numbers using any programming language and not just Python!
Now that we have our problem broken down and pseudo code written, it is time for us to replace the pseudo code with actual Python programming code instructions.
Python Program To Add Two Numbers And Print Its Result
Fire up your Python IDLE interpreter and start typing in the code along with me for you to be able to understand this program better:
Python 3.5.2 (default, Oct 8 2019, 13:06:37) [GCC 5.4.0 20160609] on linux Type "copyright", "credits" or "license()" for more information. >>>
Once in our python interpreter, let us start typing in our Python program commands. The first thing we need to do according to the pseudo code written above is the receive the first input number from our user. In Python program, the instruction code to be used to receive a value from its user is by calling the input() function. Input function will accept a string as its parameter that will be displayed to the program user when the program is run. So, with this knowledge, our first line of the Python program to add two numbers will be:
>>> num1 = input('Enter the first number\n') Enter the first number 10 >>>
As you can see from the above code block, we have used the input() function to prompt our program user with the string “Enter the first number“. We are also saving the value entered by the user to a Python variable called num1.
The input function will then prompt with the above string and wait until the user enters a number. In the above code snippet, I had entered a value of 10 which is now stored in the variable num1.
In a similar way, we will write the next line of code which will prompt the user to enter a second input number that is to be added to the first number. This is achieved with the following piece of code:
>>> num2 = input('Enter the second number\n') Enter the second number 20 >>>
Again over here, I have entered my second number as 20 when prompted.
Now that we have the two numbers in our Python variables num1 and num2, it is time add them to get the final result that we are going to store in our third Python variable called result. This is achieved using the following Python statement:
>>> result = int(num1) + int(num2) >>>
So from the above python code, we have now added the numbers num1 and num2 using the Python arithmetic operator “+” and the typecast operator called “int()“. We had to use the typecast operator int because by default all inputs from the user into a python program will be interpreted and stored as a string. We can check this by issuing the following command in the interpreter:
>>> print(num1) 10 >>> type(num1) <class 'str'> >>>
So, by calling the typecast operator int, we are converting this string value to an integer value.
>>> type(int(num1)) <class 'int'> >>>
finally stored the end result in a new variable called result. Since we have not issued a Python command to print the value of result, nothing gets printed on the IDLE prompt yet. So, the final step is exactly that. To print the value of the result variable onto the screen. This is achieved using another Python function called the print function.
>>> print (result) 30 >>>
Here is the full program that we can store in a file called add2num.py and run it using the command python3 add2num.py everytime we want to add any two numbers!
num1 = input('Enter the first number\n') num2 = input('Enter the second number\n') result = int(num1) + int(num2) print (result)
This concludes our Python program to add two numbers.
Additional Side Notes On Python Programming Language:
Python is an interpreted programming language that gets interpreted and executed on the fly and hence the program written in Python do not need to be compiled like in the case of a C or Java programming language.