python二无误除法

python二和python3除法的最大分别:

Python旅长四个整数相除,暗中同意结果是为整数的。但大家能够经过下边包车型地铁法子,使得四个整数相除的结果为小数。

python2:

图片 1from __future__ import division
图片 2
图片 3print 7/3

print 500/1000

输出结果:

python2结果:取整数部分,小数并从未保存

2.3333333333

0

Process finished with exit code 0

 

python3:

print 500/1000

python3结果:获取真正结果,小数保留

0.5

Process finished with exit code 0

 

那就是说,假如python2想保留小数部分,要怎么办啊?

只须求追加几个导入包.就可以了.并不须要其余操作

from __future__ import division #用于/相除的时候,保留真实结果.小数

 

充实导入包后的,python二操作:

#coding:utf-8
from __future__ import division

print 500/1000

结果:

0.5

Process finished with exit code 0

 

还有另一种格局.将除数或被除数八个别的至少1个调换到float型:

print float(500)/1000

结果:

0.5

Process finished with exit code 0

 

 

 

 

G

M

T

 

Detect language Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bengali Bosnian Bulgarian Catalan Cebuano Chichewa Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Esperanto Estonian Filipino Finnish French Galician Georgian German Greek Gujarati Haitian Creole Hausa Hebrew Hindi Hmong Hungarian Icelandic Igbo Indonesian Irish Italian Japanese Javanese Kannada Kazakh Khmer Korean Lao Latin Latvian Lithuanian Macedonian Malagasy Malay Malayalam Maltese Maori Marathi Mongolian Myanmar (Burmese) Nepali Norwegian Persian Polish Portuguese Punjabi Romanian Russian Serbian Sesotho Sinhala Slovak Slovenian Somali Spanish Sundanese Swahili Swedish Tajik Tamil Telugu Thai Turkish Ukrainian Urdu Uzbek Vietnamese Welsh Yiddish Yoruba Zulu   Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bengali Bosnian Bulgarian Catalan Cebuano Chichewa Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Esperanto Estonian Filipino Finnish French Galician Georgian German Greek Gujarati Haitian Creole Hausa Hebrew Hindi Hmong Hungarian Icelandic Igbo Indonesian Irish Italian Japanese Javanese Kannada Kazakh Khmer Korean Lao Latin Latvian Lithuanian Macedonian Malagasy Malay Malayalam Maltese Maori Marathi Mongolian Myanmar (Burmese) Nepali Norwegian Persian Polish Portuguese Punjabi Romanian Russian Serbian Sesotho Sinhala Slovak Slovenian Somali Spanish Sundanese Swahili Swedish Tajik Tamil Telugu Thai Turkish Ukrainian Urdu Uzbek Vietnamese Welsh Yiddish Yoruba Zulu          

 

 

 

Text-to-speech function is limited to 200 characters

 

  Options : History : Feedback : Donate Close

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图