• 科普

SCIENCE

英语

ENGLISH

科技

TECHNOLOGY

MOVIE

FOOD

励志

INSPIRATIONS

社会

SOCIETY

TRAVEL

动物

ANIMALS

KIDS

卡通

CARTOON

计算机

COMPUTER

心理

PSYCHOLOGY

教育

EDUCATION

手工

HANDCRAFTS

趣闻

MYSTERIES

CAREER

GEEKS

时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

Python的除法及其他数学运算

Division with Python, and other math

So if you started with python, you found that you can do basic math within python.

It’s really not too difficult.

You can do things like 7 times 8. You put it out, it’s 56.

You can do 7 minus 8. You get negative one.

You can do 7 plus 8. You get 15.

And then of course 7 divided by 8, and you get 0.

Obviously this is a problem, so how to do division get decimals within python

It’s quite simply. There’s a couple of major ways to do this.

The first one is to just simply use decimals in your numbers.

This kind of notifies python to say “hey, I’d like to go to decimals”

So you can say 7.0 divided by,

you can do it this way 8 and it will still give you decimal numbers.

But the best way to do it is probably to give decimals on both sides.

Now what if you don’t want to have to add the decimal point to it

or like say your point from another source that’s not having the decimal point.

You still want to do division.

The easiest way to do that is to inform python that you want all the division to be this way.

And to do that, at the very top of your script, I’ll actually show you guys within IDLE.

So let me pause this really quick and I’ll open up IDLE to show you what you’ll type in there.

Right, here we are in the typical python IDE called IDLE.

What you are going to have to do is use the from “__future__” import division.

And to do that you just literally type that out “from __future__ import division”

Now what this is doing is telling python that you want to use this module.

To use any from __future__ import like this, you have to put it at the top of your script.

Even if you have other imports, they must go below any from __future__ imports.

So continuing right along.

All you have to do is just to tell it to print 7 divided by 8.

Here, save it.

Run it.

And as you can see it printed out, and we didn’t use any decimal points.

So that’s division within python.

译制信息

Python教学系列，除法及其他数学运算。

[B]hugue

B11101001