Python 除法运算符 '/' 与 '//' 的区别

本文详细解释了Python中两种除法运算符'/ '和'//'的区别。'/'执行真除法并返回浮点数结果,而'//'执行地板除法,总是向下取整到最接近的整数。此外,文章还探讨了不同类型的操作数如何影响这两种运算符的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 除法运算符 '/' 与 '//' 的区别

>>> 10 / 3
3
>>> 10 // 3
3
>>> 10 / 3.0
3.3333333333333335
>>> 10 // 3.0
3.0
>>> type(10 // 3.0)
<type 'float'>
也就是当除数和被除数都是整数的时候,返回结果都是整数。
当除数和被除数至少有一个是浮点数的时候,返回结果都是浮点数。但是,
// 的结果的小数部分永远是 0.

`Floor division'' is what Python's / operator currently does when
given integer operands; the result is the floor of the value returned
by true division. ``Classic division'' is the current mixed behaviour
of /; it returns the result of floor division when the operands are
integers, and returns the result of true division when one of the
operands is a floating-point number.

Here are the changes 2.2 introduces:

A new operator, //, is the floor division operator. (Yes, we know it
looks like C++'s comment symbol.) // always performs floor division no
matter what the types of its operands are, so 1 // 2 is 0 and 1.0 //
2.0 is also 0.0.

参考:http://docs.python.org/release/2.2.3/whatsnew/node7.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值