Add two integers

EasyBasic Programs · 1 of 40

Write a Python program to add two integer values and print the result.

Advertisement
Input
num1 = 30, num2 = 40
Output
Sum of 30 and 40 is 70

Approach

  1. Store the two numbers in variables.
  2. Add them with the + operator.
  3. Print the result with a label.

Solution

Python
num1 = 30num2 = 40 total = num1 + num2print("Sum of", num1, "and", num2, "is", total)
Output
Sum of 30 and 40 is 70

Line by line

Lines 1-2

Two integer variables hold the input values.

Line 4

+ adds the numbers and the result is stored in total, so it has a meaningful name.

Line 5

print() with several values separated by commas puts a space between each one.

Tip

To add numbers typed by the user, convert the text first: num1 = int(input("First: ")).

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs