Python - Calculate the Sum of Two Numbers

Document 对象参考手册Python3 Examples

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

Example

# Define two numbers
num1 = 5
num2 = 10

# Calculate the sum of two numbers
sum = num1 + num2

# Output the result
print("The sum of the two numbers is:", sum)

Code analysis:

  1. num1 = 5andnum2 = 10: These two lines of code define two variablesnum1andnum2, and assign them the values 5 and 10 respectively.
  2. sum = num1 + num2: This line of code calculatesnum1andnum2the sum of , and stores the result in the variablesumin .
  3. print("两个数字的和是:", sum): This line of code outputs the calculation result.printThe function is used to display information on the screen. Here it will display the string "The sum of the two numbers is:" and the variablesumthe value of .

Output result:

两个数字的和是: 15

Document 对象参考手册Python3 Examples

Other extensions