How to add number in python

 

Add number in Python

To  understand how to add the two number in the python ,first of all you have know how to take input 

in python and display output in python.


Add the two number

Following is the example code for add the two in python:


# the following code  adds two numbers by assigning value to variables

 

number1 = 7

number2 = 4.7

 

# Add two numbers

sum = number1 + number2

 

# Now Display the sum

print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))

 

 

Now we will see how add numbers with the help of taking input from the user.

Example code:Add the two number by user input 


 

# the following code  taking two number from the user then 

add into the sum variable.

 

number1 = input('Enter first number: ')

number2 = input('Enter second number: ')

 

# Add two numbers

sum = float(number1) + float(number2)

 

# Now Display the sum

print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))

 

 

In the above example,we take input with the help of input function input( ) which takes input as string 

and later we convert it into float for performing addition operation. At the last we display with the help of a 

print function .

 

Hopefully,it we will helpful for your coding comment below if any problem in understanding. 

Comments

Hire a programmer

Popular posts from this blog

input by cin in c++

Python program to print Hello wolrd

pre increment or post increment in c++