Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Python Language issue/question

It seems that when I have

a = 1  
b = 5  
c = a/b  
print c  

The console prints '0' instead of '0.2'. A cursory google search told me that in this case, Python defaults to integer division (This is changed in Python 3). The documentation says that '//' implements floating point division, but doing so for the above example still results in '0' being printed.

Is there a method specific to here that I should be using?

2 responses

Python treats the number as integer if you don't specify the format. Please try this

a = 1.0  
b = 5.0  
c = a/b  

You might also try:

a = 1  
b = 5.0  
c = a/b  

and

a = 1.0  
b = 5  
c = a/b  

See also:

https://www.quora.com/What-are-the-semantics-of-the-division-operator-in-Python