Learnitweb

Python program to add two numbers

We’ll write Python program to add two numbers. We’ll do it in two ways. First, we’ll find the sum of two numbers without user input i.e. assuming we already have the numbers. Second, we’ll find the sum of two numbers based on the user input i.e. user will provide two numbers to be added.

Python program to add two numbers

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Python3 program to add two numbers
firstNumber = 10
secondNumber = 20
# Calculating sum of two numbers
sum = firstNumber + secondNumber
# print sum of given two numbers
print(f"Sum of {firstNumber} and {secondNumber} is {sum}")
# Python3 program to add two numbers firstNumber = 10 secondNumber = 20 # Calculating sum of two numbers sum = firstNumber + secondNumber # print sum of given two numbers print(f"Sum of {firstNumber} and {secondNumber} is {sum}")
# Python3 program to add two numbers
firstNumber = 10
secondNumber = 20

# Calculating sum of two numbers
sum = firstNumber + secondNumber

# print sum of given two numbers
print(f"Sum of {firstNumber} and {secondNumber} is {sum}")

Output

Sum of 10 and 20 is 30

Python program to add two numbers based on user input

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Input two numbers from user
# We have assumed the input is int. You can use float if the provided input numbers are of type float
firstNumber = int(input('Enter first number: '))
secondNumber = int(input('Enter Second number: '))
# Calculating sum of two numbers
sum = firstNumber + secondNumber
# print sum of given two numbers
print(f"Sum of {firstNumber} and {secondNumber} is {sum}")
# Input two numbers from user # We have assumed the input is int. You can use float if the provided input numbers are of type float firstNumber = int(input('Enter first number: ')) secondNumber = int(input('Enter Second number: ')) # Calculating sum of two numbers sum = firstNumber + secondNumber # print sum of given two numbers print(f"Sum of {firstNumber} and {secondNumber} is {sum}")
# Input two numbers from user
# We have assumed the input is int. You can use float if the provided input numbers are of type float

firstNumber = int(input('Enter first number: '))
secondNumber = int(input('Enter Second number: '))

# Calculating sum of two numbers
sum = firstNumber + secondNumber

# print sum of given two numbers
print(f"Sum of {firstNumber} and {secondNumber} is {sum}")

Output

Enter first number: 10
Enter Second number: 20
Sum of 10 and 20 is 30