Add Two Numbers

Read two numbers from the user, add them, and display the result.

BeginnerTopic: Basic Python Programs
Back

What You'll Learn

  • Reading input from the user using input()
  • Converting strings to numbers using float()
  • Performing basic arithmetic in Python
  • Printing formatted output

Python Add Two Numbers Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to add two numbers provided by the user

# taking input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# adding the two numbers
result = num1 + num2

# displaying the result
print("The sum is:", result)
Output
Enter first number: 10
Enter second number: 20
The sum is: 30.0

Step-by-Step Breakdown

  1. 1Ask the user to enter the first number using input().
  2. 2Ask the user to enter the second number.
  3. 3Convert both inputs to float so arithmetic is possible.
  4. 4Add the two numbers and store the result in a variable.
  5. 5Use print() to display the final sum to the user.

Understanding Add Two Numbers

This program shows how to take input from the user, convert it to a numeric type, perform an arithmetic operation, and print the result.

Key concepts used

1.input() to read values from the user as strings.
2.float() to convert the string input into floating-point numbers so we can add them.
3.Basic arithmetic operator + to add two numbers.
4.print() to show the final result to the user.

This pattern (read → convert → compute → display) appears in many beginner-level programs and is a good template for I/O-based problems.

Let us now understand every line and the components of the above program.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.