Python Practice › Basic Programs › Program 1
Add two integers
Write a Python program to add two integer values and print the result.
Input
num1 = 30, num2 = 40
Output
Sum of 30 and 40 is 70
Approach
- Store the two numbers in variables.
- Add them with the
+operator. - Print the result with a label.
Solution
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: ")).
Learn the concept
New to this topic? Read the lesson, then come back.