Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Compute the Sum of Two Numbers

Python Compute the Sum of Two Numbers

Document Object Reference Manual Python3 Examples

This simple Python program demonstrates how to compute the sum of two numbers. We define two variables to store the numbers and then use the addition operator to compute their sum.

Example

Define two numbers

num1

=

5

num2

=

10

Compute the sum of the two numbers

sum

=

num1 + num2

Output result

print

(

"The sum of the two numbers is:"

,

sum

)

Code analysis:

  1. num1 = 5 and num2 = 10: These two lines define two variables num1 and num2, and assign them the values 5 and 10 respectively.
  2. sum = num1 + num2: This line computes the num1 and num2 sum of and stores the result in the variable sum it.
  3. print("两个数字的和是:", sum): This line prints the computed result.print The function displays information on the screen; here it displays the string "The sum of the two numbers is:" and the variable sum value.

Output result:

两个数字的和是: 15

Document Object Reference Manual Python3 Examples