Skip to main content

How to calculate the sum of two numbers in Python

How to calculate the sum of two numbers in Python.

Here's a step-by-step tutorial on how to calculate the sum of two numbers in Python, along with multiple code examples.

Step 1: Declare the two numbers

  • Start by declaring two variables to hold the numbers you want to add. For example, let's say you want to add the numbers 5 and 3. You can declare two variables like this:
num1 = 5
num2 = 3

Step 2: Calculate the sum using the + operator

  • In Python, you can calculate the sum of two numbers by using the + operator. Simply add the two variables together and store the result in a new variable. Here's an example:
sum = num1 + num2

Step 3: Print the result

  • Finally, you can print the result using the print() function. Pass the variable holding the sum as an argument to the print() function. Here's the complete code:
num1 = 5
num2 = 3

sum = num1 + num2
print("The sum is:", sum)

Output:

The sum is: 8

You can also directly print the sum without storing it in a separate variable. Here's an example:

num1 = 5
num2 = 3

print("The sum is:", num1 + num2)

Output:

The sum is: 8

That's it! You have successfully calculated the sum of two numbers in Python. Feel free to modify the values of num1 and num2 to calculate the sum of different numbers.