How do you add two numbers using input 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.

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2022 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.

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 add two numbers with input in Python?

    How to Add Two Numbers in Python.
    ❮ Previous Next ❯.
    Example. x = 5. y = 10. print[x + y] Try it Yourself ».
    Example. x = input["Type a number: "] y = input["Type another number: "] sum = int[x] + int[y] print["The sum is: ", sum] Try it Yourself ».
    ❮ Previous Next ❯.

    How do you add sum of inputs in Python?

    First Read the input number asking for the length of the list using the input[] function and store these values into a list. After that, you can use the sum[] function or Iterate each element in the list using for loop and sump up all.

    What is the code to add two numbers in Python?

    num1 = input['Enter first number: '] num2 = input['Enter second number: '] # Add two numbers. sum = float[num1] + float[num2]

    Chủ Đề