Python 中 * 运算符的正确名称是什么?


在本文中,我们将解释 Python 中 * **运算符**的正确名称。

在 Python 中,您会经常遇到 * 和 ** 符号。许多 Python 程序员,尤其是那些处于中等水平的程序员,对 Python 中的星号 (*) 字符感到困惑。

*args 参数称为“**可变位置参数**”,**kwargs 称为“**可变关键字参数**”。* 和 ** 参数只是解包它们各自的数据结构。

在乘法中使用星号 (*) 运算符

算法(步骤)

以下是执行所需任务的算法/步骤:

  • 创建两个变量并将它们存储在两个单独的变量中。

  • 使用 * **运算符**将两个输入数字相乘,并创建一个变量来存储它。

  • 打印两个数字相乘的结果。

示例

以下程序使用 * 运算符返回两个数字的乘积:

# input number 1 inputNumber_1 = 10 # input number 2 inputNumber_2 = 5 # multiplying both the input numbers using the * operator multResult = inputNumber_1 * inputNumber_2 # printing the resultant multiplication of 2 numbers print("Resultant multiplication result using *:", multResult)

输出

执行上述程序后,将生成以下输出:

Resultant multiplication result using *: 50

在幂运算中使用星号 (**) 运算符

  • 创建两个变量并将它们存储在两个单独的变量中。

  • 使用 ** **运算符**获取 inputNumber_1 的 inputNumber_2 次幂(此处为 2^5)的值,并创建一个变量来存储它。

  • 打印结果的指数值。

示例

以下程序使用 ** 运算符返回两个数字的指数结果:

# input number 1 inputNumber_1 = 2 # input number 2 inputNumber_2 = 5 print("number 1 =",inputNumber_1,"\nnumber 2 =",inputNumber_2) # getting the exponential value of inputNumber_1 to the # power of inputNumber_2 (2^5) using ** operator expResult = inputNumber_1 ** inputNumber_2 # printing the resultant exponential value print("The resultant exponential result using **:", expResult)

输出

执行上述程序后,将生成以下输出:

number 1 = 2
number 2 = 5
The resultant exponential result using **: 32

在第一个示例中,我们取了两个数字并将它们存储在两个单独的变量中。当我们在两个变量之间使用 * 运算符时,结果是两个变量的乘积。当我们在两个变量之间使用 ** 运算符时,它充当这两个变量的幂函数,其中第一个数字是底数,第二个数字是指数。

使用 * 运算符重复列表

Python 列表也包含 * 运算符,它允许您创建一个新列表,其中元素重复指定的次数。

算法(步骤)

以下是执行所需任务的算法/步骤:

  • 使用一些随机/虚拟值创建一个列表。

  • 通过将给定列表与 * 运算符相乘,使列表乘以 n 倍(此处为 2)

示例

以下程序使用 * **运算符**重复列表给定的次数:

# input list inputList = [5, 6, 7] print("Input list =",inputList) # Repeating the input list 2 times using the * operator print(inputList * 2)

输出

执行上述程序后,将生成以下输出:

Input list = [5, 6, 7]
[5, 6, 7, 5, 6, 7]

在这里,我们取了一个随机值的列表,并用 * 运算符将其乘以两次,以便输出包含给定列表重复两次。

使用 * 运算符解包函数。

此方法在以原始格式(没有任何逗号和括号)打印数据时非常方便。许多程序员试图通过合并函数来删除逗号和括号,因此这个简单的星号前缀可以解决您在解包它们时遇到的问题。

算法(步骤)

以下是执行所需任务的算法/步骤:

  • 创建一个变量来存储输入列表并赋予它一些随机值。

  • 要打印用空格分隔的列表元素,而不带括号 [],首先我们将列表转换为字符串,将 str 和 list 作为参数传递给 **map()** 函数。它将列表的每个元素转换为字符串类型,并返回结果项列表。**join()** 函数(join() 用于连接用字符串分隔符分隔的序列的元素)用于将结果列表转换为字符串。

  • 除了上述方法之外,我们还可以使用星号运算符 (*) 来打印用空格分隔的列表。

示例

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 5, 'everyone', 10, 5.3] print("Input list =",inputList) # Converting list elements to string using map() # Applying join() function to convert list to string # Without using the asterisk (*) operator print('Without Using * Operator :') print(' '.join(map(str,inputList))) # Using the asterisk (*) operator print('Using * operator : ') print (*inputList)

输出

执行上述程序后,将生成以下输出:

Input list = ['TutorialsPoint', 'Python', 'Codes', 'hello', 5, 'everyone', 10, 5.3]
Without Using * Operator :
TutorialsPoint Python Codes hello 5 everyone 10 5.3
Using * operator :
TutorialsPoint Python Codes hello 5 everyone 10 5.3

两种方式的输出保持不变。

使用任意数量的位置参数传递函数

在此方法中,单个星号 (*) 也用于 ***args** 中。

它用于向函数传递可变数量的参数;它最常用于传递非键参数和可变长度参数列表。

它具有多种应用,其中一种如下所述。我们创建了一个 **addNumbers** 函数,该函数接受任意数量的参数,并可以使用 ***args** 将它们全部加在一起。

示例

# creating a function that accepts any number of arguments to it and # returns the sum of arguments passed to it def addNumbers(*args): # getting the sum of any numbers passed to the function return sum(args) # calling the addNumbers() function by passing some # random numbers as arguments to it to get the sum of those. print("Sum of 4,6,15,5 = ", addNumbers(4, 6, 15, 5))

输出

执行上述程序后,将生成以下输出:

Sum of 4,6,15,5 = 30

两种方式的输出保持不变。

结论

在本文中,我们学习了 * 运算符的正确名称,以及如何在 Python 编程语言中将其用于各种应用。我们还使用一个示例演示了 * 和 ** 运算符之间的区别。

更新于: 2022-11-09

172 次查看

启动您的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.