“变量//= 一个值"是什么意思?Python中的语法是什么意思? [英] What does the "variable //= a value" syntax mean in Python?

查看:104
本文介绍了“变量//= 一个值"是什么意思?Python中的语法是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了代码语法 d//= 2 其中 d 是一个变量.这不是任何循环的一部分,我不太明白这个表达.
有人可以请教我吗?

解决方案

// 是一个楼层划分运算符.旁边的 = 表示对变量就地"进行操作.它类似于 +=*= 操作符,如果你以前见过的话,除了这是带除法.

假设我有一个名为 d 的变量.我将它的值设置为 65,就像这样.

<预><代码>>>>d = 65

调用 d//= 2 会将 d 除以 2,然后将该结果分配给 d.由于 d//2 是 32(32.5,但去掉小数部分),d 变成 32:

<预><代码>>>>d//= 2>>>d32

与调用 d = d//2 相同.

I came across with the code syntax d //= 2 where d is a variable. This is not a part of any loop, I don't quite get the expression.
Can anybody enlighten me please?

解决方案

// is a floor division operator. The = beside it means to operate on the variable "in-place". It's similar to the += and *= operators, if you've seen those before, except for this is with division.

Suppose I have a variable called d. I set it's value to 65, like this.

>>> d = 65

Calling d //= 2 will divide d by 2, and then assign that result to d. Since, d // 2 is 32 (32.5, but with the decimal part taken off), d becomes 32:

>>> d //= 2
>>> d
32

It's the same as calling d = d // 2.

这篇关于“变量//= 一个值"是什么意思?Python中的语法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆