深度阅读

How to add two numbers in Python

作者
作者
2023年08月22日
更新时间
7.91 分钟
阅读时间
0
阅读量

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.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。