You are currently viewing How to add two numbers in Python?

How to add two numbers in Python?

In this artilcle, we will see a very simple program in Python. We will do the addition of two number in python. Simple right? Yes and even simpler to write the code in Python.

Example 1: With fixed values. A simpler form of addition where we will have fixed values of variables. See the code below.

a=5

b=10

c=a+b

print(c)

Output: 15

In the above program, the variable data is fixed,  now we will take input from the user and then perform the addition.

Example 2: Take input from the user and then print the sum of the inputs taken.

Syntax to take data from the user is:

input("Enter first Number")

Code:

a=input("Enter First Number")  //entered number is10

b=input("Enter Second Number")  //Entered number is 20

c=a+b

In the above code, the output will come: 1020, because by default Python will treat the input as string formate, so it will simply concatenate the two values we have taken as input from the user. In order to do a mathematical sum, we have to convert the entered values into an integer. Below is the code to convert the values in integer

  a=int(input('Enter The First Number')) //Enter value is 10

     b=int(input('Enter The Second Number'))///Enter value is 20

     c=a+b 

    print(c)

   Output: 30

So, this was a very basic program to do the sum of 2 numbers in Python.

That’s it Folks on this topic. Let us know your comments on this article in the comment box below. Kindly like our Facebook page, follows us on Twitter and subscribe to our YouTube channel for latest updates.

Leave a Reply