首页 > 编程语言 >What do these operators mean (** , ^ , %, //)? [closed] Python

What do these operators mean (** , ^ , %, //)? [closed] Python

时间:2022-09-24 01:11:05浏览次数:90  
标签:do What division these Python arguments operator integer modulus

What do these operators mean (** , ^ , %, //)? [closed]

 

回答1

  • **: exponentiation   指数
  • ^: exclusive-or (bitwise)  异或
  • %: modulus
  • //: divide with integral result (discard remainder)

python中“//”是一个算术运算符,表示整数除法,它可以返回商的整数部分(向下取整)。具体用法如:【a = 10 b = 5 c = a//b 】,结果输出整数2。

 

回答2

You can find all of those operators in the Python language reference, though you'll have to scroll around a bit to find them all. As other answers have said:

  • The ** operator does exponentiation. a ** b is a raised to the b power. The same ** symbol is also used in function argument and calling notations, with a different meaning (passing and receiving arbitrary keyword arguments).
  • The ^ operator does a binary xor. a ^ b will return a value with only the bits set in a or in b but not both. This one is simple!
  • The % operator is mostly to find the modulus of two integers. a % b returns the remainder after dividing a by b. Unlike the modulus operators in some other programming languages (such as C), in Python a modulus it will have the same sign as b, rather than the same sign as a. The same operator is also used for the "old" style of string formatting, so a % b can return a string if a is a format string and b is a value (or tuple of values) which can be inserted into a.
  • The // operator does Python's version of integer division. Python's integer division is not exactly the same as the integer division offered by some other languages (like C), since it rounds towards negative infinity, rather than towards zero. Together with the modulus operator, you can say that a == (a // b)*b + (a % b). In Python 2, floor division is the default behavior when you divide two integers (using the normal division operator /). Since this can be unexpected (especially when you're not picky about what types of numbers you get as arguments to a function), Python 3 has changed to make "true" (floating point) division the norm for division that would be rounded off otherwise, and it will do "floor" division only when explicitly requested. (You can also get the new behavior in Python 2 by putting from __future__ import division at the top of your files. I strongly recommend it!)

 

回答3

You are correct that ** is the power function.

^ is bitwise XOR.

% is indeed the modulus operation, but note that for positive numbers, x % m = x whenever m > x. This follows from the definition of modulus. (Additionally, Python specifies x % m to have the sign of m.)

// is a division operation that returns an integer by discarding the remainder. This is the standard form of division using the / in most programming languages. However, Python 3 changed the behavior of / to perform floating-point division even if the arguments are integers. The // operator was introduced in Python 2.6 and Python 3 to provide an integer-division operator that would behave consistently between Python 2 and Python 3. This means:

| context                                | `/` behavior   | `//` behavior |
---------------------------------------------------------------------------
| floating-point arguments, Python 2 & 3 | float division | int divison   |
---------------------------------------------------------------------------
| integer arguments, python 2            | int division   | int division  |
---------------------------------------------------------------------------
| integer arguments, python 3            | float division | int division  |

For more details, see this question: Division in Python 2.7. and 3.3

 

标签:do,What,division,these,Python,arguments,operator,integer,modulus
From: https://www.cnblogs.com/chucklu/p/16724805.html

相关文章

  • do...while循环体
    基本语法 不要忘记while()后还需要加分号!!!例(输出五句hello):inti=1;//循环变量初始化intmax=5;//循环的最大次数do{printf("hello!");//循环体......
  • win10: Windows Audio无法启动 错误 0x80070005:拒绝访问
    进入目录C:\Windows\System32,找到cmd.exe,右键->以管理员身份运行在cmd窗口中输入:netlocalgroupAdministrators/addnetworkservice,回车在cmd窗口中输入:netlocalg......
  • Markdown语法笔记
    Markdown学习网站:https://commonmark.org/help/Markdown语法笔记Markdown目录(自动生成):[TOC]Markdown标题:#这是H1##这是H2###这是H3Markdown列表:-列表......
  • window 启用telnet 方法 和基本使用
    1找到启用获关闭window功能,点击    2勾选telnet   3telnet的基本使用 在 CMD 命令行键入telnet ......
  • Windows10 操作系统里数量众多的 svchost.exe
    如果各位的计算机使用的是Windows10操作系统,请尝试以下操作:使用Ctrl+Shift+Esc热键或右键单击任务栏并选择“任务管理器”选项打开任务管理器。现在单击“详细信息......
  • day2-doc指令的练习
    常用的dos指令 #盘符切换 #查看当前目录下的文件dir #切换目录cdchangedirectory  cd/d跨盘切换  cd..切换上级目录 #cls清理屏幕clearscreen ......
  • [UTCTF2020]docx
    [UTCTF2020]docx下载下来是一个word文件,里面直接搜flag搜不到,用010打开发现是压缩文件改后缀为rar解压在word\media的图片里找到flag......
  • windows 软链接的建立及删除
    1.mklink用法  2.创建软链接##建立d:develop链接目录,指向远程的目标服务器上的e盘的对应目录。mklink/Dd:\develop\\138.20.1.141\e$\develop##建立d:devel......
  • Linux安装Docker
    一、设置dockerrepositorysudoyuminstall-yyum-utilssudoyum-config-manager--add-repohttps://download.docker.com/linux/centos/docker-ce.repo二、安......
  • windows清理桌面右键菜单中新增选项中的多余选项
    在卸载坚果云时一开始没注意,最近在使用的时候发现右键新增的里面出现了.nol等坚果云的文件,于是找了一下能删除多余选项的方法。感觉自己以后还能用到所以记下来方便自己查......