日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

python基礎(chǔ)之//、/與%的區(qū)別詳解_python

作者:程序媛美美 ? 更新時(shí)間: 2022-08-03 編程語(yǔ)言

“ // ” 表示整數(shù)除法,返回整數(shù) 比如 7/3 結(jié)果為2

“ / ” 表示浮點(diǎn)數(shù)除法,返回浮點(diǎn)數(shù) (即小數(shù)) 比如 8/2 結(jié)果為4.0

“ %” 表示取余數(shù) 比如7/4 結(jié)果為3

示例代碼如下:

pycharm環(huán)境下可直接運(yùn)行使用

a = 321
b = a//100
c = a//10 % 10
d = a % 10

print("百位數(shù)是%d" % b)
print("十位數(shù)是%d" % c)
print("個(gè)位數(shù)是%d" % d)

輸出結(jié)果如下圖:

擴(kuò)展:

使用divmod()函數(shù)會(huì)同時(shí)得到商和余數(shù) IDLE環(huán)境下演練代碼如下:

>>> divmod(13,3)
(4, 1)

divmod()函數(shù)返回的是一個(gè)元組

a = 4321
b = a //1000
c = a //100 %10
d = a //10%10
e = a%10
print("千位數(shù)是",b)
print("百位數(shù)是",c)
print("十位數(shù)是",d)
print("個(gè)位數(shù)是",e)
a = 54321
b = a //10000
c = a //1000 %10
d = a //100%10
e = a//10%10
f = a%10
print("萬(wàn)位數(shù)是",b)
print("千位數(shù)是",c)
print("百位數(shù)是",d)
print("十位數(shù)是",e)
print("個(gè)位數(shù)是",f)

輸出結(jié)果為:

萬(wàn)位數(shù)是 5
千位數(shù)是 4
百位數(shù)是 3
十位數(shù)是 2
個(gè)位數(shù)是 1

附:一分鐘看懂Python中的 // 和 / 和 % 的用法區(qū)別

/ (常規(guī)除)

如:

5 / 2 = 2.5

解釋?zhuān)浩匠3ㄊ鞘裁唇Y(jié)果就是什么結(jié)果。

//(地板除)

如:

5 // 2 = 2 (5 ÷ 2 = 2.5)

5 // 3 = 1 (5 ÷ 3 = 1.6666666666666667)

解釋?zhuān)旱匕宄蝗コ曛蟮恼麛?shù)部分。

% (取余數(shù))

如:

5 % 2 = 1 (5 - 2*2 = 1)

4 % 2 = 0 (4 - 2*2 = 0)

7 % 3 = 1 (7 - 3*2 = 1)

13 % 5 = 3 (13 - 5*2 = 3)

解釋?zhuān)?就是一個(gè)取余數(shù)的操作,除開(kāi)被除數(shù)的倍數(shù),余下幾就得幾。上面紅色的數(shù)字帶表被除數(shù)的倍數(shù)。

總結(jié)

原文鏈接:https://blog.csdn.net/weixin_45222544/article/details/91875456

欄目分類(lèi)
最近更新