In this program, you will learn to add two numbers and display it using print[] function.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python Input, Output and Import
- Python Data Types
- Python Operators
In the program below, we've used the +
operator to add two numbers.
Example 1: Add Two Numbers
# This program adds two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
Output
The sum of 1.5 and 6.3 is 7.8
The program below calculates the sum of two numbers entered by the user..
Example 2: Add Two Numbers With User Input
# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
Output
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8
In this program, we asked the user to enter two numbers and this program displays the sum of two numbers entered by user.
We use the built-in function input[]
to take the input. Since, input[]
returns a string, we convert the string into number using the float[]
function. Then, the numbers are added.
Alternative to this, we can perform this addition in a single statement without using any variables as follows.
print['The sum is %.1f' %[float[input['Enter first number: ']] + float[input['Enter second number: ']]]]
Output
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8
Although this program uses no variable [memory efficient], it is harder to read.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given two numbers num1 and num2. The task is to write a Python program to find the addition of these two numbers. Examples:
Input: num1 = 5, num2 = 3 Output: 8 Input: num1 = 13, num2 = 6 Output: 19
In the below program to add two numbers, the user is first asked to enter two numbers and the input is scanned using the input[] function and stored in the variables number1 and number2. Then, the variables number1 and number2 are added using the arithmetic operator + and the result is stored in the variable sum. Below is the Python program to add two numbers:
Example 1:
Python3
num1
=
15
num2
=
12
sum
=
num1
+
num2
print
["
Sum
of {
0
}
and
{
1
}
is
{
2
}" .
format
[num1, num2,
sum
]]
Output:
Sum of 15 and 12 is 27
Example 2: Adding two number provided by user input
Python3
number1
=
input
["First number: "]
number2
=
input
["\nSecond number: "]
sum
=
float
[number1]
+
float
[number2]
print
["The
sum
of {
0
}
and
{
1
}
is
{
2
}" .
format
[number1, number2,
sum
]]
Output:
First number: 13.5 Second number: 1.54 The sum of 13.5 and 1.54 is 15.04
Example 3:
Python3
if
__name__
=
=
"__main__"
:
num1
=
15
num2
=
12
sum_twoNum
=
lambda
num1, num2 : num1
+
num2
print
[
"Sum of {0} and {1} is {2};"
.
format
[num1, num2, sum_twoNum[num1, num2]]]
Output:
Sum of 15 and 12 is 27;