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!

# Floating Point Division
231871064940156750 / 5
# => 4.637421298803135e+16
# Turn the result into a integer
# => 46374212988031352
# Floor Division
231871064940156750 // 5
# => 46374212988031350

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

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

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/"
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!

Python Hax: Lambdas

# Python like Javascript is a functional langauge. It allows you to pass methods.
# To filter my_list for even numbers you can:
my_list = range(1,11) # Creates a list with the number 1 – 10
# Filter Syntax is filter(params to filter by, list to filter)
print filter( lambda x: x%2==0, my_list)
# Returns
[2, 4, 6, 8, 10]

Python Hax: Iterating Over a Dictionary

# Iterating over Dictionaries in Pythin is really straight forward
dictionary = {
"a" : "A",
"b" : "B",
"c" : "C",
"d" : "D"
# Get all items in the Dictionary
# Returns
[('a', 'A'), ('c', 'C'), ('b', 'B'), ('d', 'D')]
# Get all keys in the Dictionary
# Returns
['a', 'c', 'b', 'd']
# Get all values in the Dictionary
# Returns
['A', 'C', 'B', 'D']

Python Hax: List Slicing / Reverse

# Python list can be manipulated as follows:
my_list = [1,2,3,4,5,6,7,8,9,10]
# my_list[start_index : end_index : stride]
# Default for start index is 0
# Default for end index is the end of the list
even_num_list = my_list[1::2]
# Returns
[2, 4, 6, 8, 10]
odd_num_list = my_list[::2]
# Returns
[1, 3, 5, 7, 9]
reverse_list = my_list[::1]
# Returns
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Python Hax: Recursive Example for Factorials

Recursion can be difficult for beginners. It still is difficult for me.
The best way to improve is to try "easy" recursion problems such as
factorials in this case.
Create a function that takes an positive interger and returns the factorial of it
Example factorial(3) should return 3*2*1 or 6
Factorial(1) should return 1
Factorial(0) should return 1
#Non Recursion Example
def factorial(x):
total = 1
if x == 0 or x == 1: # Handles Edge Case of 1 or 0
return total
while x > 1:
total = total * x # Multiples variable total defined outside of loop with x
x-=1 # Subtracts one from x so that the while loop will break when x = 1
return total
# Recursion Example
def factorial(x):
if x == 0 or x ==1: # When x is equal to 0 or 1, it returns 1
return 1
return x * factorial(x1) # multiples x with the recursion or calling itself with x – 1
If we do factorial (3)
It first checks if x is == 0 or x == 1 => False
Then it go to x * factorial(x-1) which would be 3 * factorial(2)
Evaluate factorial(2)
Check if x == 0 or x == 1 => False
x * factorial(x-1) or 2 * factorial(1)
Evaluates factorial(1)
Check if x == 0 or x == 1 => True
returns 1
factorial(1) evaluates to 1
returns 1
So 2 * factorial(1) = 2 * 1 => 2
factorial(2) evaluates to 2
returns 2
So factorial(2) = 2
So 3 * factorial(2) = 3 * 2 => 6
This function then returns 6

Install LDA, Beautiful Soup and NLPK for Sublime REPL

Screen Shot 2015-04-04 at 12.40.51 PM

# This guide is for people who have install Sublime REPL and linked their system Python to it.
# If you haven't but want to, refer to my previous post where I show you how.
# If you are using Python 3 and still have Python 2 installed on your system as default
# For NLTK
python3 m pip install U nltk
# For LDA
python3 m pip install lda
#For Beautiful Soup
python3 m pip install beautifulsoup4
# python2 -m pip install PACKAGE NAME will allow you to install packages for Python 2
# If you are only running one version of Python, you can just type pip install PACKAGE NAME

view raw
hosted with ❤ by GitHub