在Python中,**是指数运算符

WBOY
发布: 2023-08-20 23:10:30
转载
2424人浏览过

在python中,**是指数运算符

在本文中,我们将学习Python中的**运算符。

Double Star (**)是Python中的算术运算符(如+,-,*,**,/,//,%)。幂运算符是它的另一个名称。

What Order/Precedence Do Arithmetic Operators Take?

The rules for both Arithmetic operators and Mathematical operators are same, which are as follows: exponential is run first, followed by multiplication and division, and then addition and subtraction.

Following are the priority orders of arithmetic operators used in decreasing mode −

立即学习Python免费学习笔记(深入)”;

() >> ** >> * >> / >> // >> % >> + >> -
登录后复制

双星(**)运算符的用途

Using ** As Exponentiation Operator:

它也以在数值数据中执行指数运算而闻名

Example

以下程序使用 ** 运算符作为表达式中的幂运算符 −

# using the double asterisk operator as an exponential operator
x = 2
y = 4

# getting exponential value of x raised to the power y
result_1 = x**y

# printing the value of x raised to the power y
print("result_1: ", result_1)

# getting the resultant value according to the
# Precedence of Arithmetic Operators
result_2 = 4 * (3 ** 2) + 6 * (2 ** 2 - 5)

print("result_2: ", result_2)
登录后复制

Output

On executing, the above program will generate the following output −

PHP 网络编程技术与实例(曹衍龙)
PHP 网络编程技术与实例(曹衍龙)

PHP网络编程技术详解由浅入深,全面、系统地介绍了PHP开发技术,并提供了大量实例,供读者实战演练。另外,笔者专门为本书录制了相应的配套教学视频,以帮助读者更好地学习本书内容。这些视频和书中的实例源代码一起收录于配书光盘中。本书共分4篇。第1篇是PHP准备篇,介绍了PHP的优势、开发环境及安装;第2篇是PHP基础篇,介绍了PHP中的常量与变量、运算符与表达式、流程控制以及函数;第3篇是进阶篇,介绍

PHP 网络编程技术与实例(曹衍龙) 386
查看详情 PHP 网络编程技术与实例(曹衍龙)
<font face="Liberation Mono, Consolas, Menlo, Courier, monospace"><span style="font-size: 14px;">result_1:  16
result_2:  30</span></font>
登录后复制

Using **As Arguments in Functions and Methods:

双星号在函数定义中也被称为**kwargs。它用于将可变长度的关键字字典传递给函数

我们可以使用下面示例中显示的小函数打印**kwargs参数:

Example

下面的程序展示了在用户定义的函数中使用kwargs的方法 -

# creating a function that prints the dictionary of names.
def newfunction(**kwargs):
   # traversing through the key-value pairs if the dictionary
      for key, value in kwargs.items():
   # formatting the key, values of a dictionary
   # using format() and printing it
      print("My favorite {} is {}".format(key, value))
# calling the function by passing the any number of arguments
newfunction(language_1="Python", language_2="Java", language_3="C++")
登录后复制

Output

On executing, the above program will generate the following output −

My favorite language_1 is Python
My favorite language_2 is Java
My favorite language_3 is C++
登录后复制

我们可以通过**kwargs在我们的代码中轻松使用关键字参数。最好的部分是,当我们将**kwargs作为参数使用时,可以向函数传递大量的参数。当预计参数列表中的输入数量相对较少时,创建接受**kwargs的函数是最好的选择。

结论

This article taught us about Python's ** operator. We learned about the precedence of operators in the Python compiler, as well as how to utilize the ** operator, which functions like a kwargs and may accept any amount of arguments for a function and is also used to calculate the power.

以上就是在Python中,**是指数运算符的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号