How to add two numbers in Python

Published on Aug. 22, 2023, 12:16 p.m.

As seen in the search results, adding two numbers in Python can be done using the + operator as shown in this example code:

num1 = 10
num2 = 5
result = num1 + num2
print(result)

This will output 15, which is the result of adding num1 and num2.

Alternatively, if you want to add two numbers entered by the user, you can use the input() function to prompt the user to enter the two numbers, convert the input to integers using the int() function, and then add them using the + operator. Here is an example code that does this:

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
result = num1 + num2
print(result)

This will prompt the user to enter the two numbers, add num1 and num2, and print the result.

Tags:

related content