From 3ef96e3874ab214f009a8b3f21f1e20b015847d6 Mon Sep 17 00:00:00 2001 From: rasbt Date: Tue, 6 May 2014 16:13:21 -0400 Subject: [PATCH] print fixes --- README.md | 2 +- benchmarks/palindrome_timeit.ipynb | 24 +- benchmarks/timeit_tests.ipynb | 2 +- not_so_obvious_python_stuff.ipynb | 30 +- python_true_false.html | 2297 ----------------- .../python_true_false.ipynb | 14 +- 6 files changed, 40 insertions(+), 2329 deletions(-) delete mode 100644 python_true_false.html rename python_true_false.ipynb => tutorials/python_true_false.ipynb (96%) diff --git a/README.md b/README.md index 1305f53..c970e0e 100755 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Useful functions, tutorials, and other Python-related things ###Links to view the IPython Notebooks - [Python benchmarks via `timeit`](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/benchmarks/timeit_tests.ipynb?create=1) -- [Implementing the least squares fit method for linear regression and speeding it up via Cythonook](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/benchmarks/cython_least_squares.ipynb?create=1) +- [Implementing the least squares fit method for linear regression and speeding it up via Cython](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/benchmarks/cython_least_squares.ipynb?create=1) - [Benchmarks of different palindrome functions](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/benchmarks/palindrome_timeit.ipynb?create=1) - [A collection of not so obvious Python stuff you should know!](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/not_so_obvious_python_stuff.ipynb?create=1) - [Python's scope resolution for variable names and the LEGB rule](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/scope_resolution_legb_rule.ipynb?create=1) diff --git a/benchmarks/palindrome_timeit.ipynb b/benchmarks/palindrome_timeit.ipynb index ef0e33f..f1df349 100644 --- a/benchmarks/palindrome_timeit.ipynb +++ b/benchmarks/palindrome_timeit.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:6ea19109869c82ee989c8ea0599ec49401e74246a542ad0b7b05f6ef464bda19" + "signature": "sha256:3d878b64b4503fd987496df562af53903ad85d4cce103ea0a2e6c456519c03c7" }, "nbformat": 3, "nbformat_minor": 0, @@ -12,8 +12,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Sebastian Raschka 04/2014\n", + "[Sebastian Raschka](http://sebastianraschka.com) \n", + "last updated: 05/03/2014\n", "\n", + "- [Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/benchmarks/palindrome_timeit.ipynb) \n", + "- [Link to the GitHub repository](https://github.com/rasbt/python_reference) \n", + "\n", + "
\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "#Timing different Implementations of palindrome functions" ] }, @@ -178,14 +190,6 @@ } ], "prompt_number": 11 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] } ], "metadata": {} diff --git a/benchmarks/timeit_tests.ipynb b/benchmarks/timeit_tests.ipynb index d1bc680..bbd8c17 100644 --- a/benchmarks/timeit_tests.ipynb +++ b/benchmarks/timeit_tests.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:75d807f509bd9f76b2e14a5a048cb44852a3318bcd0d95afc95d1c9b2904c078" + "signature": "sha256:132d5623e27eb721587599809f9870d397887ec873bbbdc95b25a05e710d160e" }, "nbformat": 3, "nbformat_minor": 0, diff --git a/not_so_obvious_python_stuff.ipynb b/not_so_obvious_python_stuff.ipynb index 1e0ef3c..0a9632c 100644 --- a/not_so_obvious_python_stuff.ipynb +++ b/not_so_obvious_python_stuff.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:0e1c6e74b301e23ea4146d660afb3f07765686c6c7fa4752f3a4495da7949787" + "signature": "sha256:7a22f6c91e4aab51a325c721dd7674622d1acc5b4a3a038ff512c736d83bbe4a" }, "nbformat": 3, "nbformat_minor": 0, @@ -12,11 +12,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Sebastian Raschka \n", + "[Sebastian Raschka](http://sebastianraschka.com) \n", "last updated: 05/03/2014 ([Changelog](#changelog))\n", "\n", - "[Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/not_so_obvious_python_stuff.ipynb)\n", - "\n", + "- [Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/not_so_obvious_python_stuff.ipynb) \n", + "- [Link to the GitHub repository](https://github.com/rasbt/python_reference) \n", "\n" ] }, @@ -307,14 +307,6 @@ ], "prompt_number": 6 }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - }, { "cell_type": "markdown", "metadata": {}, @@ -684,13 +676,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "** Logical `or`: ** \n", + "**Logical `or`:** \n", "\n", "`a or b == a if a else b` \n", "- If both values in `or` expressions are `True`, Python will select the first value (e.g., select `\"a\"` in `\"a\" or \"b\"`), and the second one in `and` expressions. \n", "This is also called **short-circuiting** - we already know that the logical `or` must be `True` if the first value is `True` and therefore can omit the evaluation of the second value.\n", "\n", - "** Logical `and`: ** \n", + "**Logical `and`:** \n", "\n", "`a and b == b if a else a` \n", "- If both values in `and` expressions are `True`, Python will select the second value, since for a logical `and`, both values must be true.\n" @@ -3448,8 +3440,14 @@ "metadata": {}, "source": [ "I would claim that the conditional \"else\" is every programmer's daily bread and butter. However, there is a second flavor of \"else\"-clauses in Python, which I will call \"completion else\" (for reason that will become clear later). \n", - "But first, let us take a look at our \"traditional\" conditional else that we all are familiar with. \n", - "### Conditional else:" + "But first, let us take a look at our \"traditional\" conditional else that we all are familiar with. \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "###Conditional else:" ] }, { diff --git a/python_true_false.html b/python_true_false.html deleted file mode 100644 index 079d7cb..0000000 --- a/python_true_false.html +++ /dev/null @@ -1,2297 +0,0 @@ - - - - -[] - - - - - - - - - - - - - - -
-

Sebastian Raschka, 03/2014
Code was executed in Python 3.4.0

-
-
-

True and False in the datetime module

-

Pointed out in a nice article "A false midnight" at http://lwn.net/SubscriberLink/590299/bf73fe823974acea/:

-

"it often comes as a big surprise for programmers to find (sometimes by way of a hard-to-reproduce bug) that,
unlike any other time value, midnight (i.e. datetime.time(0,0,0)) is False.
A long discussion on the python-ideas mailing list shows that, while surprising,
that behavior is desirable—at least in some quarters."

-
-
-
-
-In [17]: -
-
-
import datetime
-
-print('"datetime.time(0,0,0)" (Midnight) evaluates to', bool(datetime.time(0,0,0)))
-
-print('"datetime.time(1,0,0)" (1 am) evaluates to', bool(datetime.time(1,0,0)))
-
- -
-
- -
-
- - -
-
-
-"datetime.time(0,0,0)" (Midnight) evaluates to False
-"datetime.time(1,0,0)" (1 am) evaluates to True
-
-
-
-
- -
-
- -
-
-

Boolean True

-
-
-
-
-In [83]: -
-
-
my_true_val = True
-
-
-print('my_true_val == True:', my_true_val == True)
-print('my_true_val is True:', my_true_val is True)
-
-print('my_true_val == None:', my_true_val == None)
-print('my_true_val is None:', my_true_val is None)
-
-print('my_true_val == False:', my_true_val == False)
-print('my_true_val is False:', my_true_val is False)
-
-print(my_true_val
-if my_true_val:
-    print('"if my_true_val:" is True')
-else:
-    print('"if my_true_val:" is False')
-    
-if not my_true_val:
-    print('"if not my_true_val:" is True')
-else:
-    print('"if not my_true_val:" is False')
-
- -
-
- -
-
- - -
-
-
-my_true_val == True: True
-my_true_val is True: True
-my_true_val == None: False
-my_true_val is None: False
-my_true_val == False: False
-my_true_val is False: False
-"if my_true_val:" is True
-"if not my_true_val:" is False
-
-
-
-
- -
-
- -
-
-

Boolean False

-
-
-
-
-In [76]: -
-
-
my_false_val = False
-
-
-print('my_false_val == True:', my_false_val == True)
-print('my_false_val is True:', my_false_val is True)
-
-print('my_false_val == None:', my_false_val == None)
-print('my_false_val is None:', my_false_val is None)
-
-print('my_false_val == False:', my_false_val == False)
-print('my_false_val is False:', my_false_val is False)
-
-
-if my_false_val:
-    print('"if my_false_val:" is True')
-else:
-    print('"if my_false_val:" is False')
-    
-if not my_false_val:
-    print('"if not my_false_val:" is True')
-else:
-    print('"if not my_false_val:" is False')
-
- -
-
- -
-
- - -
-
-
-my_false_val == True: False
-my_false_val is True: False
-my_false_val == None: False
-my_false_val is None: False
-my_false_val == False: True
-my_false_val is False: True
-"if my_false_val:" is False
-"if not my_false_val:" is True
-
-
-
-
- -
-
- -
-
-

None 'value'

-
-
-
-
-In [62]: -
-
-
my_none_var = None
-
-print('my_none_var == True:', my_none_var == True)
-print('my_none_var is True:', my_none_var is True)
-
-print('my_none_var == None:', my_none_var == None)
-print('my_none_var is None:', my_none_var is None)
-
-print('my_none_var == False:', my_none_var == False)
-print('my_none_var is False:', my_none_var is False)
-
-
-if my_none_var:
-    print('"if my_none_var:" is True')
-else:
-    print('"if my_none_var:" is False')
-
-if not my_none_var:
-    print('"if not my_none_var:" is True')
-else:
-    print('"if not my_none_var:" is False')
-
- -
-
- -
-
- - -
-
-
-my_none_var == True: False
-my_none_var is True: False
-my_none_var == None: True
-my_none_var is None: True
-my_none_var == False: False
-my_none_var is False: False
-"if my_none_var:" is False
-"if not my_none_var:" is True
-
-
-
-
- -
-
- -
-
-

Empty String

-
-
-
-
-In [61]: -
-
-
my_empty_string = ""
-
-print('my_empty_string == True:', my_empty_string == True)
-print('my_empty_string is True:', my_empty_string is True)
-
-print('my_empty_string == None:', my_empty_string == None)
-print('my_empty_string is None:', my_empty_string is None)
-
-print('my_empty_string == False:', my_empty_string == False)
-print('my_empty_string is False:', my_empty_string is False)
-
-
-if my_empty_string:
-    print('"if my_empty_string:" is True')
-else:
-    print('"if my_empty_string:" is False')
-    
-if not my_empty_string:
-    print('"if not my_empty_string:" is True')
-else:
-    print('"if not my_empty_string:" is False')
-
- -
-
- -
-
- - -
-
-
-my_empty_string == True: False
-my_empty_string is True: False
-my_empty_string == None: False
-my_empty_string is None: False
-my_empty_string == False: False
-my_empty_string is False: False
-"if my_empty_string:" is False
-"if my_empty_string:" is True
-
-
-
-
- -
-
- -
-
-

Empty List

-

It is generally not a good idea to use the == to check for empty lists...

-
-
-
-
-In [67]: -
-
-
my_empty_list = []
-
-
-print('my_empty_list == True:', my_empty_list == True)
-print('my_empty_list is True:', my_empty_list is True)
-
-print('my_empty_list == None:', my_empty_list == None)
-print('my_empty_list is None:', my_empty_list is None)
-
-print('my_empty_list == False:', my_empty_list == False)
-print('my_empty_list is False:', my_empty_list is False)
-
-
-if my_empty_list:
-    print('"if my_empty_list:" is True')
-else:
-    print('"if my_empty_list:" is False')
-    
-if not my_empty_list:
-    print('"if not my_empty_list:" is True')
-else:
-    print('"if not my_empty_list:" is False')
-
-
-    
-
- -
-
- -
-
- - -
-
-
-my_empty_list == True: False
-my_empty_list is True: False
-my_empty_list == None: False
-my_empty_list is None: False
-my_empty_list == False: False
-my_empty_list is False: False
-"if my_empty_list:" is False
-"if not my_empty_list:" is True
-
-
-
-
- -
-
- -
-
-

[0]-List

-
-
-
-
-In [70]: -
-
-
my_zero_list = [0]
-
-
-print('my_zero_list == True:', my_zero_list == True)
-print('my_zero_list is True:', my_zero_list is True)
-
-print('my_zero_list == None:', my_zero_list == None)
-print('my_zero_list is None:', my_zero_list is None)
-
-print('my_zero_list == False:', my_zero_list == False)
-print('my_zero_list is False:', my_zero_list is False)
-
-
-if my_zero_list:
-    print('"if my_zero_list:" is True')
-else:
-    print('"if my_zero_list:" is False')
-    
-if not my_zero_list:
-    print('"if not my_zero_list:" is True')
-else:
-    print('"if not my_zero_list:" is False')
-
- -
-
- -
-
- - -
-
-
-my_zero_list == True: False
-my_zero_list is True: False
-my_zero_list == None: False
-my_zero_list is None: False
-my_zero_list == False: False
-my_zero_list is False: False
-"if my_zero_list:" is True
-"if not my_zero_list:" is False
-
-
-
-
- -
-
- -
-
-

List comparison

-

List comparisons are a handy way to show the difference between == and is.
While == is rather evaluating the equality of the value, is is checking if two objects are equal. The examples below show that we can assign a pointer to the same list object by using =, e.g., list1 = list2.
a) If we want to make a shallow copy of the list values, we have to make a little tweak: list1 = list2[:], or
b) a deepcopy via list1 = copy.deepcopy(list2)

-

Possibly the best explanation of shallow vs. deep copies I've read so far:

-

*** "Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements. Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated."***

-

(via S.Lott on StackOverflow)

-
-
-

a) Shallow vs. deep copies for simple elements

-

List modification of the original list doesn't affect
shallow copies or deep copies if the list contains literals.

-
-
-
-
-In [11]: -
-
-
from copy import deepcopy
-
-my_first_list = [1]
-my_second_list = [1]
-print('my_first_list == my_second_list:', my_first_list == my_second_list)
-print('my_first_list is my_second_list:', my_first_list is my_second_list)
-
-my_third_list = my_first_list
-print('my_first_list == my_third_list:', my_first_list == my_third_list)
-print('my_first_list is my_third_list:', my_first_list is my_third_list)
-
-my_shallow_copy = my_first_list[:]
-print('my_first_list == my_shallow_copy:', my_first_list == my_shallow_copy)
-print('my_first_list is my_shallow_copy:', my_first_list is my_shallow_copy)
-
-my_deep_copy = deepcopy(my_first_list)
-print('my_first_list == my_deep_copy:', my_first_list == my_deep_copy)
-print('my_first_list is my_deep_copy:', my_first_list is my_deep_copy)
-
-print('\nmy_third_list:', my_third_list)
-print('my_shallow_copy:', my_shallow_copy)
-print('my_deep_copy:', my_deep_copy)
-
-my_first_list[0] = 2
-print('after setting "my_first_list[0] = 2"')
-print('my_third_list:', my_third_list)
-print('my_shallow_copy:', my_shallow_copy)
-print('my_deep_copy:', my_deep_copy)
-
- -
-
- -
-
- - -
-
-
-my_first_list == my_second_list: True
-my_first_list is my_second_list: False
-my_first_list == my_third_list: True
-my_first_list is my_third_list: True
-my_first_list == my_shallow_copy: True
-my_first_list is my_shallow_copy: False
-my_first_list == my_deep_copy: True
-my_first_list is my_deep_copy: False
-
-my_third_list: [1]
-my_shallow_copy: [1]
-my_deep_copy: [1]
-after setting "my_first_list[0] = 2"
-my_third_list: [2]
-my_shallow_copy: [1]
-my_deep_copy: [1]
-
-
-
-
- -
-
- -
-
-

b) Shallow vs. deep copies if list contains other structures and objects

-

List modification of the original list does affect
shallow copies, but not deep copies if the list contains compound objects.

-
-
-
-
-In [13]: -
-
-
my_first_list = [[1],[2]]
-my_second_list = [[1],[2]]
-print('my_first_list == my_second_list:', my_first_list == my_second_list)
-print('my_first_list is my_second_list:', my_first_list is my_second_list)
-
-my_third_list = my_first_list
-print('my_first_list == my_third_list:', my_first_list == my_third_list)
-print('my_first_list is my_third_list:', my_first_list is my_third_list)
-
-my_shallow_copy = my_first_list[:]
-print('my_first_list == my_shallow_copy:', my_first_list == my_shallow_copy)
-print('my_first_list is my_shallow_copy:', my_first_list is my_shallow_copy)
-
-my_deep_copy = deepcopy(my_first_list)
-print('my_first_list == my_deep_copy:', my_first_list == my_deep_copy)
-print('my_first_list is my_deep_copy:', my_first_list is my_deep_copy)
-
-print('\nmy_third_list:', my_third_list)
-print('my_shallow_copy:', my_shallow_copy)
-print('my_deep_copy:', my_deep_copy)
-
-my_first_list[0][0] = 2
-print('after setting "my_first_list[0][0] = 2"')
-print('my_third_list:', my_third_list)
-print('my_shallow_copy:', my_shallow_copy)
-print('my_deep_copy:', my_deep_copy)
-
- -
-
- -
-
- - -
-
-
-my_first_list == my_second_list: True
-my_first_list is my_second_list: False
-my_first_list == my_third_list: True
-my_first_list is my_third_list: True
-my_first_list == my_shallow_copy: True
-my_first_list is my_shallow_copy: False
-my_first_list == my_deep_copy: True
-my_first_list is my_deep_copy: False
-
-my_third_list: [[1], [2]]
-my_shallow_copy: [[1], [2]]
-my_deep_copy: [[1], [2]]
-after setting "my_first_list[0][0] = 2"
-my_third_list: [[2], [2]]
-my_shallow_copy: [[2], [2]]
-my_deep_copy: [[1], [2]]
-
-
-
-
- -
-
- -
-
-

Some Python oddity:

-
- -
-
-
-In [1]: -
-
-
a = 1
-b = 1
-print('a is b', bool(a is b))
-True
-
-a = 999
-b = 999
-print('a is b', bool(a is b))
-
- -
-
- -
-
- - -
-
-
-a is b True
-a is b False
-
-
-
-
- -
-
- -
- - diff --git a/python_true_false.ipynb b/tutorials/python_true_false.ipynb similarity index 96% rename from python_true_false.ipynb rename to tutorials/python_true_false.ipynb index b2786a2..fa8c446 100644 --- a/python_true_false.ipynb +++ b/tutorials/python_true_false.ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "" + "name": "", + "signature": "sha256:f6d59feb844af096c0581919fa052e8f29f4fa0aaa81752f5e50241ab084a0e9" }, "nbformat": 3, "nbformat_minor": 0, @@ -11,7 +12,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Sebastian Raschka, 03/2014 \n", + "[Sebastian Raschka](http://sebastianraschka.com) \n", + "last updated: 05/03/2014\n", + "\n", + "- [Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/benchmarks/python_true_false.ipynb) \n", + "- [Link to the GitHub repository](https://github.com/rasbt/python_reference) \n", + "\n", "Code was executed in Python 3.4.0" ] }, @@ -392,8 +398,8 @@ "\n", "Possibly the best explanation of shallow vs. deep copies I've read so far:\n", "\n", - "*** \"Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.\n", - "Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.\"***\n", + "***\"Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.\n", + "Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.\"*** \n", "\n", "(via [S.Lott](http://stackoverflow.com/users/10661/s-lott) on [StackOverflow](http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy))" ]