Bookmark and Share

RSS:
English journal:http://meng6.net/pages/journal/index.rss, feedburner, subscribe by email
Chinese journalhttp://meng6.net/pages/zh/journal/index.rss

RSS Atom Add a new post titled:

In Python 2, dividing two integers will yield a rounded integer result:

$ ipython2
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
Type "copyright", "credits" or "license" for more information.

IPython 4.1.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: 1/2
Out[1]: 0

To get a real-number result:

In [2]: 1./2
Out[2]: 0.5

In [3]: float(1)/2
Out[3]: 0.5

In Python 3, the default is the real-number result:

$ ipython
Python 3.5.1 (default, Dec  7 2015, 21:59:10)
Type "copyright", "credits" or "license" for more information.

IPython 4.1.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [2]: 1/2
Out[2]: 0.5

In [3]: exit
>>> elapsed time 15s

In a Python script, to use Python 3 division behavior for Python 2:

import from __future__ division;
print 1/2

Posted Tue May 16 16:59:39 2017 Tags:

Older journal entries:

Add a new post titled:

How to use matplotlib in Python virtual envrionments
Posted Tue May 16 16:59:39 2017

Git core.ignoreCase setting
Posted Tue May 16 16:59:39 2017

Wolfram Language (Mathematica) package structure
Posted Tue May 16 16:59:39 2017

mushroom note
Posted Tue May 16 16:59:39 2017

Jupyter notebook basics
Posted Tue May 16 16:59:39 2017

mean squared error
Posted Tue May 16 16:59:39 2017

how to play a sound after a command is finished
Posted Tue May 16 16:59:39 2017

category theory note
Posted Tue Apr 11 00:18:07 2017

Manufacturing Consent
Posted Sat Apr 8 15:21:45 2017

The Big Short
Posted Sat Apr 8 15:21:45 2017

Oil
Posted Sat Apr 8 15:21:45 2017

how to select items to work on from a large to-do list
Posted Sat Apr 8 15:21:45 2017

blog comments powered by Disqus