Add Two Numbers in Python

Python - Add Two Numbers: Given numbers, we have to add them using Python program. By IncludeHelp Last updated : April 09, 2023

Given two integer numbers and we have to find their sum in Python.

Logic to Add Two Numbers

We have two variables num1 and num2 and assigning them with the value 10 and 20, finding and printing the sum of the numbers. Later we are inputting two numbers from the input and assigning them num1 and num2.

Note: While inputting numbers from using input() function, we get string value, to convert string value to an integer value – we need to convert them into integer. To convert string to an integer, use int() function.

Sample Input/Output

Input:
num1 = 10
num2 = 20

Finding sum:
sum = num1 + num2

Output:
30

Python Code to Add Two Numbers

# python program to find sum of 
# two numbers

num1 = 10
num2 = 20

# finding sum 
sum = num1 + num2

# printing sum
print("sum of ", num1, " and ", num2, " is = ", sum)

# taking input from user
num1 = input("Enter first  number: ")
num2 = input("Enter second number: ")

# finding sum 
sum = int(num1) + int(num2)

# printing sum
print("sum of ", num1, " and ", num2, " is = ", sum)

Output

sum of  10  and  20  is =  30
Enter first  number: 100
Enter second number: 200
sum of  100  and  200  is =  300

Python Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.