Sum of two numbers in python

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

  • Read
  • Discuss
  • 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;

    How do you sum numbers in python?

    Python provides an inbuilt function sum[] which sums up the numbers in the list. Syntax: sum[iterable, start] iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.

    How do you add 2 numbers in a class python?

    Python program to add two numbers using class.
    In this program, we have the class as add and we have defined the function as def findsum[self, n1, n2].
    sum = n1 + n2 is used for addition and return sum will return the value..
    We have declared the variable as n1 and n2..

    How do you add 2 numbers together?

    When adding, always line up the addends, the two numbers being combined, one on top of each other according to their place values. Add the numbers in the ones column first, then the tens column, and finally the hundreds column, to get the sum, or the total.

    How do you add two numbers in python flask?

    You should build an flask API function: a GET route which accepts the argument from your form and returns a JSON response object. Then in your HTML when you click the button that should perform an AJAX GET quesry accessing your API route and return the values to that page.

    Chủ Đề