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.

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

One Response to Difference between / and // division in python

  1. siri says:

    Hi Amal,
    How to read to pdf table data.we want output as ,separated values.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: