How to add Two Numbers in Python

Hello Coders, In this blog post, you will learn to add Two Numbers in Python. It is very easy to add Numbers in Python. We will add numbers with three different methods in Python. Let us learn these steps in detail:-

Add Two number in Python

1. Simple Addition Program

In this type, Both numbers are given. We simply need to add these numbers. Forex:- Num1 = 3 , Num2 = 5 and the sum of num1 and num2 is num1 + num2 = 8. The Code and result of this problem are as follows:-

Simple.py

# CodeBlockx
# Program To Add Two Number
Num1 = 3
Num2 = 5
Sum = Num1 + Num2
print(” Sum of both Number is = “ ,Sum)

OUTPUT

Sum of both Number is = 8

2. Adding Number with User Input

In this type of Problem, We will take the value of both numbers from the user. The user will input the value of the number then the numbers will be scanned by the python after which the operation will be performed on them and the result will be stored and then displayed on the output screen. The Code and result of this problem are as follows:-

input.py

#codeblockx
Num1 = int (input (” Enter the value of 1st number : “) )
Num2 = int (input (” Enter the value of 2nd number : “) )
# We can also use float instead of int.
sum = Num1 + Num2
print (” The Addition of both number is” , sum)

OUTPUT

Enter the value of 1st number : 41
Enter the value of 2nd number : 87
The Addition of both number is 128

3. Adding Number with Defining a Function

In this type, We will define a function sum which will add two numbers and whenever we will write sum and then numbers automatically the sum of these numbers will be displayed on the output screen. The Code and result of this problem are as follows:-

funcation.py

# codeblockx
def add(a,b):
    return a+b
# Function defined and output also given in return
 
num2 = 4
num1 = 8
# To get the value of return add the function in print
print(“The sum of these two numbers is”, add( num1, num2))

OUTPUT

The sum of these two numbers is 12

Hope this blog post was helpful to you and you have got the answer to your question. Thank you for visiting our blog. If you have any doubts about any coding questions then let us know in the comments section we will answer them as soon as possible. Till then check out our more blog posts.

Leave a Comment

Your email address will not be published. Required fields are marked *