Python中添加两个数字的6种不同方法

Python中有六种将两个数字相加的方法,如下所示。
  1. 使用算术运算符“+”
  2. 使用“+=”
  3. 使用函数reduce()和operator.add
  4. 使用添加功能
  5. 使用用户输入
  6. 使用 sum() 方法

我们将讨论在 Python 中将两个数字相加的所有 6 种方法/让我们首先从算术运算符开始。

方法 1:如何在 Python 中使用算术运算符“+”将两个数字相加
这是在 Python 中将两个数字相加的最基本、最直接的方法。只需在要相加的两个数字之间使用“+”运算符,Python 就会返回总和。

# Calculating the sum of two numbers

num1 = 5 # First number
num2 = 10 # Second number

# Adding the two numbers
sum = num1 + num2

# Printing the result
print("The sum of", num1, "and", num2, "is", sum)

上面的代码计算两个数字num1和num2的总和,并将结果存储在名为sum的变量中。然后将结果与一条消息一起打印到控制台。num1的值被分配为 5,num2的值被分配为 10。

方法 2:如何在 Python 中使用“+=”将两个数字相加
“+=”运算符是 Python 中的简写运算符,可用于向变量添加值。该运算符将右侧的值添加到左侧的变量中,并将结果存储在左侧的变量中。

# Addition of two variables
num1 = 5
num2 = 10

# Perform addition using '+=' operator
num1 += num2

# Print the result
print("The sum of", num1, "and", num2, "is", num1)

上面的代码使用“+=”运算符将两个数字num1和num2相加。加法的结果存储在num1变量中,然后打印为“The sum of 5 and 10 is 15”。

方法3:如何在Python中使用函数reduce()和operator.add将两个数字相加
reduce ()函数是 Python 中的内置函数,可用于将给定函数应用于列表的所有元素。要添加两个数字,您可以使用reduce()函数和operator.add函数。

# 从函数库导入运算符模块和 reduce 函数
import operator
from functools import reduce

# Initializing two numbers
num1 = 5
num2 = 10

# Calculating the sum of the two numbers using reduce() and operator.add
sum = reduce(operator.add, [num1, num2])

# Printing the result
print("The sum of", num1, "and", num2, "is", sum)

上面的代码从functools模块导入了operator模块和reduce函数。
  • 它定义了两个变量num1和num2,其值分别为5和10。
  • 然后,它使用以operator.addreduce作为运算符的函数计算两个变量的总和,以应用于列表[num1, num2]的元素。
  • 结果打印出一条消息,指出“5 和 10 的总和是 15”。

方法 4:如何在 Python 中使用 add 函数将两个数字相加

# 定义一个接收两个参数 num1 和 num2 的函数
def add(num1, num2):
    # Return the result of adding the two numbers
    return num1 + num2

# Store the result of calling the add function with arguments 5 and 10
sum = add(5, 10)

# Print the sum of 5 and 10
print("The sum of 5 and 10 is", sum)

上面的代码定义了一个函数add(),它接受两个参数num1和num2。
  • 该函数使用“+”运算符返回两个参数的总和。
  • 然后使用参数 5 和 10 调用该函数,并将结果存储在sum变量中。最后,打印总和。

方法 5:如何使用用户输入在 Python 中添加两个数字
要将用户提供的两个数字相加,您可以使用input()函数获取用户的输入,然后使用上述任何方法将两个数字相加。

# 这段代码从用户输入的两个数字中
# 计算出它们的总和并打印结果

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

# Sum of the two input numbers
sum = num1 + num2

# Output the result
print(
"The sum of", num1, "and", num2, "is", sum)

上面的代码从用户处获取两个整数输入并将它们存储为num1和num2。
  • 然后,它计算这两个数字的总和并将其存储在变量sum中。最后,它通过在输出字符串中包含num1、num2和sum的值来打印总和的结果。

方法 6:如何在 Python 中使用 sum() 方法将两个数字相加
sum ()函数是 Python 中的内置函数,可用于将数字相加。您可以将数字列表传递给此函数,它将返回列表中所有数字的总和。

# 使用 sum() 函数计算两个数字之和的程序
# Define the two numbers
num1 = 5
num2 = 10

# Calculate the sum of the two numbers
# by passing them as a list to the sum() function
sum = sum([num1, num2])

# Print the sum of the two numbers
print("The sum of", num1, "and", num2, "is", sum)

上面的代码计算两个数字num1和num2的和。这些数字的值分别为 5 和 10。

然后使用模块内置函数中的 sum函数来查找两个数字的总和并将其存储在sum变量中。


Python 中使用函数将两个数字相加
Python 还允许您定义自己的函数。我们可以创建一个函数来添加两个数字:

def add_two_numbers(num1, num2):
    return num1 + num2

print('The sum is', add_two_numbers(3, 5))


在此脚本中,我们首先定义了一个函数add_two_numbers,该函数接受两个参数并返回它们的总和。然后我们用数字 3 和 5 调用这个函数,并打印结果。

使用 Lambda 函数在 Python 中计算两个数字的总和
Lambda 函数是一种在 Python 中创建小型匿名函数的方法。它们可以用在需要函数但又不想用完整def语句定义它的地方。

以下是如何在 Python 中使用 lambda 函数将两个数字相加:

# define the lambda function
add_two_numbers = lambda num1, num2: num1 + num2

# use the lambda function
print('The sum is', add_two_numbers(3, 5))