Python Hax: True Division vs Floor Division

I have been recently doing an algorithms course via Python 3 and I was doing what I thought was a simple division.

I was trying to do 231871064940156750 / 5. When I did it in Python I would get 
46374212988031352. But it would then fail tests telling me the answer should be 46374212988031350.

At first, I thought maybe it was a bit error and that the number wasn’t being stored correctly. But after reading the docs for int in Python 3, there doesn’t seem to be any limit to int vs long like there was in Python 2.

I then thought maybe converting the result from scientific notation to int was causing a rounding error somewhere along the line.

It turns out that I can fix this problem by dividing with the // operator rather than the / operator. Take a look at the code below!

In Python 3, all division returns as a float, not an integer (unlike Python 2, Ruby 2 or JavaScript).

Advertisements

Sublime Hax: Turning off the Error Checking for Anaconda in SublimeREPL

Screen Shot 2015-04-18 at 12.00.10 PM

So I been using the Anaconda Package in Sublime via Sublime REPL. If you are trying to do this too, I have a few posts on it that you should refer to.

Now if you are already doing this, you might see something like this when you load a file from someone else.Screen Shot 2015-04-18 at 12.03.03 PM

If you these error alerts drive you crazy and you want to turn some of them off, this is the post for you. At first, I use to go back and fix these errors but most of them deal with spacing around certain things like comments, equal signs, ( ) etc.

While it might be good practice to code in accordance to these conventions, going back to fix some one else’s code or even when you are writing some yourself, these errors can be extremely distracting. If you hover over the line, at the bottom of the Sublime window, it will tell you what the error is.

For example, the first error in this picture is “[W] PEP 8 (E501): line too long (89 > 79 characters), Line 2, Column 78” So what if you didn’t care that the line is “too long”. Lets change it so it stops showing us this error.

Step 1
Go to Sublime Text > Preferences > Package Settings > Anaconda  and Open: Setting – Defaults and Setting -User.
Screen Shot 2015-04-18 at 12.13.44 PM

Step 2
Copy all of settings JSON in the Defaults and paste it into the User Settings Window.

Step 3
Find the “pep8_ignore”: in the JSON (Mine was around line: 338)

Screen Shot 2015-04-18 at 12.16.42 PM

Step 4
Type "E501" so the JSON files looks like this:
Screen Shot 2015-04-18 at 1.30.10 PM

Step 5
Save the JSON file and you should see the error disappear!
Screen Shot 2015-04-18 at 1.38.16 PM

Bonus!
If you want to remove multiple error checking codes, connect new error codes with a , and make sure you put them in a ” ” like this: Screen Shot 2015-04-18 at 12.00.10 PM

See all the errors disappear! Beautiful.
Screen Shot 2015-04-18 at 1.41.59 PM

Sublime Hax: Setting System iPython to SublimeREPL

If you haven’t setup Sublime REPL or hacked it to use your system version of Python, please read my blog post on it.

I been fiddling with the Python console in Sublime for some time but everyone in the data science community uses iPython as it has some additional features and syntax highlighting (not too important since Sublime already does it for me)

Anywho its really easy to set iPython on Sublime REPL

1. Install iPython

I didn’t have a system version of iPython so I need to download it.
In your terminal type
pip install "ipython[notebook]"

This will install iPython and all of its dependencies.

2. Open up the SublimeREPL settings JSON
Screen Shot 2015-03-25 at 11.09.04 AM

Screen Shot 2015-03-25 at 11.13.38 AM

Screen Shot 2015-04-10 at 2.45.14 PM

Line 70 you want to type within the brackets [ ]
"/usr/local/bin/python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"
This is assuming that your Python is installed at that location.

If you don’t know for sure, type into the terminal which python and it will tell you.

Go DatSci go!