In this tutorial, we will learn to write a program that add two numbers in Python in a basic way. You must know basic Python before writing a basic program.
First, we will learn to write a Python program to add two numbers simply. In the second program, we will write a program to take input from the user and then sum up those numbers. In the third program, we will write a program to sum two numbers by using a function and taking input from the user.
Adding two numbers in the paper is very easy and every person can add two numbers in a very quick time but if the largest numbers are there then it will take time to add those numbers. Today, we will learn to give instructions to the computer to add two numbers by writing our code.
Program 1: Add Two Numbers
# This program adds two numbers in simple way
number1 = 10
number2 = 40.5
# Add two numbers and store result in sum variable
sum = number1 + number2
# Display the sum
print('The sum of {} + {} = {}'.format(number1, number2, sum))
Output
The sum of 10 + 40.5 = 50.5
In the above program, we simply took two variables and assigned a value to each variable. In the next statement, we simply add those two variables by using the add (+) operator and store their value in the sum variable. In the last statement, we print the value to the output screen in a formatted way.
Program 2: Add two numbers using user input
# This program adds two numbers by taking input from the user
number1 = input("Enter first number:")
number2 = input("Enter second number:")
# Add two numbers and store result in sum variable
sum = float(number1) + float(number2)
# Display the sum
print('The sum of {} + {} = {}'.format(number1, number2, sum))
Output
Enter first number:5
Enter second number:6
The sum of 5 + 6 = 11.0
In the above program, we took input from the user in two different variables. After that, we add those two inputs by using the add (+) operator but before adding we convert those variables’ value into float type because whenever we take input from the user in Python then it is considered as string type and if we add those input variable without converting to number type then those numbers will be concatenated instead of addition.
Here, we have converted to float type because the user can input any number like integer or float. In the last statement, we have displayed the output on the screen by using a print statement.
Program 3: Add two numbers in Python by using the function
In the above program, we learned how to take input from the user in Python and add those numbers. Now, we will learn to add two numbers by creating a function in Python.
# Function to add two numbers
def add_numbers(num1, num2):
result = num1 + num2
return result
# Input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Call the add_numbers function and store the result
sum_result = add_numbers(num1, num2)
# Display the result
print(f"The sum of {num1} + {num2} = {sum_result}")
Output
Enter the first number: 36
Enter the second number: 56.4
The sum of 36.0 + 56.4 = 92.4
In the above program, we created a function add_numbers and took two parameters num1 and num2, and then the next statement inside the function is performing addition operation and return result. In the next statement, we took input from the user, converted it to float type, and called the above function by passing the value of the parameter dynamically. At last, we are displaying results in a formatted way.