Difference between / and // division in python

I have seen people using / and // for division in python. So for checking the difference I checked both of this in my system. I used python 2..7 for testing. I got same output for both the divisions. The reason for the same output is explained below.

Eg: 7 / 2   and 7 // 2  will give the same answer 3 in python 2.x

In Python 3.0, 7 / 2  will return 3.5 and 7 // 2  will return 3. The former is floating point division, and the latter is floor division, sometimes also called integer division.

In Python 2.2 or later in the 2.x line, there is no difference for integers. ie we will get 3 for both the divisions. We can enable the 3.x behavior by doing simple import.

from __future__ import division,

This causes Python 2.x to adopt the behavior of 3.0. The future import should be placed at the top of your program.

Advertisements

About amalgjose
I am an Electrical Engineer by qualification, now I am working as a Software Engineer. I am very much interested in Electrical, Electronics, Mechanical and now in Software fields. I like exploring things in these fields. I like travelling, long drives and very much addicted to music.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: