Write Python. Understand every error.

Open the editor

Python programs · Basics

Python program to add two numbers

Adding two numbers is the smallest complete program: read input, calculate, show the result. It also shows why input() must be converted to a number first.

Approach

  1. Read the first number and convert it with int().
  2. Read the second number and convert it too.
  3. Add them and print the sum.

The program

a = int(input())
b = int(input())
print("Sum:", a + b)

Sample run

Input
12
30
Output
Sum: 42

Run this program in Py-CRAFT

How the code works

input() returns text, so int() turns each line into a whole number. If you skipped the conversion, + would join the two pieces of text instead of adding them: "12" + "30" gives 1230. Use float() instead of int() for numbers with decimals.

Try changing it

Common mistakes

Learn the ideas

More programs

Share this page: