Add Two Numbers

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

PythonBeginner

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
# 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

Add Two Numbers in Python

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.

Understanding the Code

1. Taking Input from User

num1 = float(input("Enter first number: "))

  • input() function displays a prompt and waits for the user to type something.
  • Whatever the user types is returned as a ## string (text).
  • float() converts that string into a floating-point number (decimal number).
  • The converted number is stored in the variable num1.

Example:

  • User types: "10" (as a string)
  • float("10") converts it to: 10.0 (as a number)

2. Performing Addition

result = num1 + num2

  • The + operator adds the two numbers together.
  • The result is stored in the variable result.

3. Displaying the Result

print("The sum is:", result)

  • print() displays text and values on the screen.
  • You can print multiple items by separating them with commas.
  • Python automatically adds a space between items.

Common Variations

Using int() for Whole Numbers

If you only need whole numbers (integers), use int() instead of float():

python
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 + num2
print("The sum is:", result)

Direct Calculation in print()

You can also calculate directly in the print statement:

python
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("The sum is:", num1 + num2)

Using f-strings for Formatting

For better formatting, use f-strings:

python
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 + num2
print(f"The sum of {num1} and {num2} is {result}")

Key Takeaways

1
input() reads user input as a string
2
float() converts strings to numbers
3

Arithmetic operators work on numeric types

4
print() can display multiple values

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.