Python - Calculate the Sum of Two Numbers
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)
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:
num1 = 5andnum2 = 10: These two lines of code define two variablesnum1andnum2, and assign them the values 5 and 10 respectively.sum = num1 + num2: This line of code calculatesnum1andnum2the sum of , and stores the result in the variablesumin .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:
两个数字的和是: 15Other extensions
Python3 Examples