A very warm welcome to all my readers in our articles. As you already find our last two articles in Python namely What is python, why python is so popular and how to install the python? and Write First Python Program. Now in this article we will start a series of Python examples for the beginners who are just stepping into this wonderful and powerful programming language or those who already use python for a long time can also refresh their mind by have a look to this tiny programs. I can tell you one thing that it will be a fun learning to all of you.
We have already seen how to write the first program in Python. Now it’s time to roll the ball. So jump into coding with Codinghub…
To add two numbers in Python we assumed that you should have clear understanding of Input/Output things, Datatype and Operators in Python. If not, our recommendation is to have a look at the Python Documentations.
Moving next, there are different ways you can write this program or your requirement vary with each other. For example, let’s write a program where we will add two numbers inside the program. Meaning we know the numbers and now we will write the code to add those numbers.
EXAMPLE 1: ADD TWO NUMBERS
For this we have to see + operator
# This program add two numbers a = 10 b = 20 # add two numbers c = a + b # print the result print("Sum = " . c)
EXAMPLE 2: ADD TWO NUMBERS BY USER INPUT
# take user input and store in variables a = input('Enter the 1st Number: ') b = input('Enter the 2nd Number: ') # add two numbers c = float(a) + float(b) # print the result print("Sum = " . c)
Enter the 1st Number: 10 Enter the 2nd Number: 20 Sum = 30
Now see what have we done in this program, we simply asked the user to enter two numbers and boom its displaying the sum of two numbers. So, adding two numbers is pretty simple isn’t it. To capture user input its actually use the python’s built-in function input().
In this program, we simply set the program in this way so that user can enter two numbers and this program displays the sum of two numbers.
One interesting thing you may noticed that we have used float() after taking user input. The reason is very simple. It actually takes the user input as string now to perform a mathematical calculation we must have to convert the string into numbers. Here float is better option as you can add any floating number as well as integer.
This is not the end, we will come back with more such important notes. If comfortable with this article you are ready to go to our next article Python Examples For Beginners — Check Prime Number
Till then connect with us on Facebook, Twitter, Linked In, and other social channels.
Thank You Very Much…
Originally published at https://www.codinghub.net.