You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>There are 2 major versions of Python in widespread use: <a href="https://docs.python.org/2/">Python 2</a> and <a href="https://docs.python.org/3/">Python 3</a>. Python 3 has some features that are not backward compatible with Python 2, and some Python 2 libraries have not been updated to work with Python 3. I have been using Python 2, primarily because I use some of those Python 2[-only] libraries, but an increasing proportion of them are migrating to Python 3, and I anticipate shifting to Python 3 in the near future.</p>
2040
-
<p>For more on the topic, I recommend a very well documented IPython Notebook, which includes numerous helpful examples and links, by <a href="http://sebastianraschka.com/">Sebastian Raschka</a>, <a href="http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.ipynb">Key differences between Python 2.7.x and Python 3.x</a> ... or <a href="https://www.google.com/q=python%202%20vs%203">googling Python 2 vs 3</a>.</p>
2041
-
<p>I received an email request from a Python 3 programmer who suggested that a relatively minor change in this notebook would enable it to run with Python 2 or Python 3: importing the <code>print_function</code> from <a href="https://docs.python.org/2/library/__future__.html"><code>__future__</code></a>, and changing my <a href="https://docs.python.org/2/reference/simple_stmts.html#print"><code>print</code> statements (Python 2)</a> to <a href="https://docs.python.org/3/library/functions.html#print"><code>print</code> function calls (Python 3)</a>. Although a relatively minor conceptual change, it necessitates the changing of many cells to reflect the Python 3 <code>print</code> syntax.</p>
2042
-
<p>I find the arguments for <a href="https://www.python.org/dev/peps/pep-3105/">making <code>print</code> a function rather than statement</a> compelling - especially as it is more consistent with printing functionality in many other programming langugages - and so while I do not want to convert this notebook to a Python 3 notebook, I have implemented this change so that it can be used in either Python 2 or Python 3. However, while I have verified that it still works in Python 2, I have not tested it in Python 3.</p>
2043
-
<p>I also find the arguments for <a href="https://www.python.org/dev/peps/pep-0238/">changing the division operator</a> compelling, so will import that as well. Without this import in Python 2, <code>1 / 2</code> returns <code>0</code> (the integer portion of the quotient); with this import, <code>1 / 2</code> returns <code>0.5</code>, and if you want only the integer portion of the quotient (<em>floor division</em>), you can use <code>1 // 2</code> (which works the same way in Python 2 and Python 3).</p>
2044
-
<p>Note that if you don't understand some/any of the above discussion about Python 2 and Python 3, it should not affect your ability to understand the rest of this notebook.</p>
2040
+
<p>For more on the topic, I recommend a very well documented IPython Notebook, which includes numerous helpful examples and links, by <a href="http://sebastianraschka.com/">Sebastian Raschka</a>, <a href="http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.ipynb">Key differences between Python 2.7.x and Python 3.x</a>, the <a href="http://python-future.org/compatible_idioms.html">Cheat Sheet: Writing Python 2-3 compatible code</a> by Ed Schofield ... or <a href="https://www.google.com/q=python%202%20vs%203">googling Python 2 vs 3</a>.</p>
2041
+
<p><a href="https://twitter.com/ncoghlan_dev">Nick Coghlan</a>, a CPython core developer, sent me an email suggesting that relatively minor changes in this notebook would enable it to run with Python 2 <em>or</em> Python 3: importing the <code>print_function</code> from the <a href="https://docs.python.org/2/library/__future__.html"><strong><code>__future__</code></strong></a> module, and changing my <a href="https://docs.python.org/2/reference/simple_stmts.html#print"><code>print</code> <em>statements</em> (Python 2)</a> to <a href="https://docs.python.org/3/library/functions.html#print"><code>print</code> <em>function calls</em> (Python 3)</a>. Although a relatively minor conceptual change, it necessitated the changing of many individual cells to reflect the Python 3 <code>print</code> syntax. I also needed to replace a few functions that are no longer available in Python 3 with related functions that are available in both versions; I've added notes in nearby cells where the incompatible functions were removed explaining why they are related ... and no longer available.</p>
"There are 2 major versions of Python in widespread use: [Python 2](https://docs.python.org/2/) and [Python 3](https://docs.python.org/3/). Python 3 has some features that are not backward compatible with Python 2, and some Python 2 libraries have not been updated to work with Python 3. I have been using Python 2, primarily because I use some of those Python 2[-only] libraries, but an increasing proportion of them are migrating to Python 3, and I anticipate shifting to Python 3 in the near future.\n",
352
352
"\n",
353
-
"For more on the topic, I recommend a very well documented IPython Notebook, which includes numerous helpful examples and links, by [Sebastian Raschka](http://sebastianraschka.com/), [Key differences between Python 2.7.x and Python 3.x](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.ipynb) ... or [googling Python 2 vs 3](https://www.google.com/q=python%202%20vs%203).\n",
353
+
"For more on the topic, I recommend a very well documented IPython Notebook, which includes numerous helpful examples and links, by [Sebastian Raschka](http://sebastianraschka.com/), [Key differences between Python 2.7.x and Python 3.x](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.ipynb), the [Cheat Sheet: Writing Python 2-3 compatible code](http://python-future.org/compatible_idioms.html) by Ed Schofield ... or [googling Python 2 vs 3](https://www.google.com/q=python%202%20vs%203).\n",
354
354
"\n",
355
-
"I received an email request from a Python 3 programmer who suggested that a relatively minor change in this notebook would enable it to run with Python 2 or Python 3: importing the `print_function` from [`__future__`](https://docs.python.org/2/library/__future__.html), and changing my [`print` statements (Python 2)](https://docs.python.org/2/reference/simple_stmts.html#print) to [`print` function calls (Python 3)](https://docs.python.org/3/library/functions.html#print). Although a relatively minor conceptual change, it necessitates the changing of many cells to reflect the Python 3 `print` syntax.\n",
356
-
"\n",
357
-
"I find the arguments for [making `print` a function rather than statement](https://www.python.org/dev/peps/pep-3105/) compelling - especially as it is more consistent with printing functionality in many other programming langugages - and so while I do not want to convert this notebook to a Python 3 notebook, I have implemented this change so that it can be used in either Python 2 or Python 3. However, while I have verified that it still works in Python 2, I have not tested it in Python 3.\n",
358
-
"\n",
359
-
"I also find the arguments for [changing the division operator](https://www.python.org/dev/peps/pep-0238/) compelling, so will import that as well. Without this import in Python 2, `1 / 2` returns `0` (the integer portion of the quotient); with this import, `1 / 2` returns `0.5`, and if you want only the integer portion of the quotient (*floor division*), you can use `1 // 2` (which works the same way in Python 2 and Python 3).\n",
360
-
"\n",
361
-
"Note that if you don't understand some/any of the above discussion about Python 2 and Python 3, it should not affect your ability to understand the rest of this notebook."
355
+
"[Nick Coghlan](https://twitter.com/ncoghlan_dev), a CPython core developer, sent me an email suggesting that relatively minor changes in this notebook would enable it to run with Python 2 *or* Python 3: importing the `print_function` from the [**`__future__`**](https://docs.python.org/2/library/__future__.html) module, and changing my [`print` *statements* (Python 2)](https://docs.python.org/2/reference/simple_stmts.html#print) to [`print` *function calls* (Python 3)](https://docs.python.org/3/library/functions.html#print). Although a relatively minor conceptual change, it necessitated the changing of many individual cells to reflect the Python 3 `print` syntax. I also needed to replace a few functions that are no longer available in Python 3 with related functions that are available in both versions; I've added notes in nearby cells where the incompatible functions were removed explaining why they are related ... and no longer available."
Copy file name to clipboardExpand all lines: README.md
+10-4
Original file line number
Diff line number
Diff line change
@@ -4,12 +4,12 @@ This short primer on [Python](http://www.python.org/) is designed to provide a r
4
4
5
5
The primer is spread across a collection of [IPython Notebooks](http://ipython.org/notebook.html), and the easiest way to use the primer is to [install IPython Notebook](http://ipython.org/install.html) on your computer. You can also [install Python](https://www.python.org/downloads/), and manually copy and paste the pieces of sample code into the Python interpreter, as the primer only makes use of the Python standard libraries.
6
6
7
-
There are three versions of the primer. Two versions contain the entire primer in a single notebook:
7
+
There are three versions of the primer. Two versions are compatible with Python 2 or Python 3), and contain the entire primer in a single notebook:
8
8
9
9
* Single IPython Notebook: [Python_for_Data_Science_all.ipynb](Python_for_Data_Science_all.ipynb)
10
10
* Single web page (HTML): [Python_for_Data_Science_all.html](Python_for_Data_Science_all.html)
11
11
12
-
The other version divides the primer into 5 separate notebooks:
12
+
The other version divides the primer into 5 separate notebooks (Python 2 only):
* Updated `simple_ml.py` to also use Python 3 `print_function` and `division`
36
-
* Changed "call by reference" to "call by sharing"
39
+
* Updated `simple_ml.py` and `SimpleDecisionTree.py` to also use Python 3 `print_function` and `division`
40
+
* Replaced `xrange()` (Python 2 only) with `range()` (Python 2 or 3)
41
+
* Replaced `dict.iteritems()` (Python 2 only) with `dict.items()` (Python 2 or 3)
42
+
* Changed ["call by reference"](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference) to ["call by sharing"](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing)
37
43
* Added `isinstance()` (and reference to duck typing) to section on `type()`
38
44
* Added variable for `delimiter` rather than hard-coding `'|'` character
0 commit comments