{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Sebastian Raschka, 03/2014 \n", "Code was executed in Python 3.4.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###`True` and `False` in the `datetime` module\n", "\n", "Pointed out in a nice article **\"A false midnight\"** at [http://lwn.net/SubscriberLink/590299/bf73fe823974acea/](http://lwn.net/SubscriberLink/590299/bf73fe823974acea/):\n", "\n", "*\"it often comes as a big surprise for programmers to find (sometimes by way of a hard-to-reproduce bug) that, \n", "unlike any other time value, midnight (i.e. datetime.time(0,0,0)) is False. \n", "A long discussion on the python-ideas mailing list shows that, while surprising, \n", "that behavior is desirable\u2014at least in some quarters.\"*" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import datetime\n", "\n", "print(datetime.time(0,0,0), '(Midnight) evaluates to ', end=\"\")\n", "if not datetime.time(0,0,0):\n", " print(False)\n", "else:\n", " print(True)\n", " \n", "print(datetime.time(1,0,0), '(1 am) evaluates to ', end=\"\")\n", "if not datetime.time(1,0,0):\n", " print(False)\n", "else:\n", " print(True)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "00:00:00 (Midnight) evaluates to False\n", "01:00:00 (1 am) evaluates to True\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean `True`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_true_val = True\n", "\n", "\n", "print('my_true_val == True:', my_true_val == True)\n", "print('my_true_val is True:', my_true_val is True)\n", "\n", "print('my_true_val == None:', my_true_val == None)\n", "print('my_true_val is None:', my_true_val is None)\n", "\n", "print('my_true_val == False:', my_true_val == False)\n", "print('my_true_val is False:', my_true_val is False)\n", "\n", "\n", "if my_true_val:\n", " print('\"if my_true_val:\" is True')\n", "else:\n", " print('\"if my_true_val:\" is False')\n", " \n", "if not my_true_val:\n", " print('\"if not my_true_val:\" is True')\n", "else:\n", " print('\"if not my_true_val:\" is False')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean `False`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_false_val = False\n", "\n", "\n", "print('my_false_val == True:', my_false_val == True)\n", "print('my_false_val is True:', my_false_val is True)\n", "\n", "print('my_false_val == None:', my_false_val == None)\n", "print('my_false_val is None:', my_false_val is None)\n", "\n", "print('my_false_val == False:', my_false_val == False)\n", "print('my_false_val is False:', my_false_val is False)\n", "\n", "\n", "if my_false_val:\n", " print('\"if my_false_val:\" is True')\n", "else:\n", " print('\"if my_false_val:\" is False')\n", " \n", "if not my_false_val:\n", " print('\"if not my_false_val:\" is True')\n", "else:\n", " print('\"if not my_false_val:\" is False')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "my_false_val == True: False\n", "my_false_val is True: False\n", "my_false_val == None: False\n", "my_false_val is None: False\n", "my_false_val == False: True\n", "my_false_val is False: True\n", "\"if my_false_val:\" is False\n", "\"if not my_false_val:\" is True\n" ] } ], "prompt_number": 76 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `None` 'value'" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_none_var = None\n", "\n", "print('my_none_var == True:', my_none_var == True)\n", "print('my_none_var is True:', my_none_var is True)\n", "\n", "print('my_none_var == None:', my_none_var == None)\n", "print('my_none_var is None:', my_none_var is None)\n", "\n", "print('my_none_var == False:', my_none_var == False)\n", "print('my_none_var is False:', my_none_var is False)\n", "\n", "\n", "if my_none_var:\n", " print('\"if my_none_var:\" is True')\n", "else:\n", " print('\"if my_none_var:\" is False')\n", "\n", "if not my_none_var:\n", " print('\"if not my_none_var:\" is True')\n", "else:\n", " print('\"if not my_none_var:\" is False')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "my_none_var == True: False\n", "my_none_var is True: False\n", "my_none_var == None: True\n", "my_none_var is None: True\n", "my_none_var == False: False\n", "my_none_var is False: False\n", "\"if my_none_var:\" is False\n", "\"if not my_none_var:\" is True\n" ] } ], "prompt_number": 62 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Empty String" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_empty_string = \"\"\n", "\n", "print('my_empty_string == True:', my_empty_string == True)\n", "print('my_empty_string is True:', my_empty_string is True)\n", "\n", "print('my_empty_string == None:', my_empty_string == None)\n", "print('my_empty_string is None:', my_empty_string is None)\n", "\n", "print('my_empty_string == False:', my_empty_string == False)\n", "print('my_empty_string is False:', my_empty_string is False)\n", "\n", "\n", "if my_empty_string:\n", " print('\"if my_empty_string:\" is True')\n", "else:\n", " print('\"if my_empty_string:\" is False')\n", " \n", "if not my_empty_string:\n", " print('\"if not my_empty_string:\" is True')\n", "else:\n", " print('\"if not my_empty_string:\" is False')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "my_empty_string == True: False\n", "my_empty_string is True: False\n", "my_empty_string == None: False\n", "my_empty_string is None: False\n", "my_empty_string == False: False\n", "my_empty_string is False: False\n", "\"if my_empty_string:\" is False\n", "\"if my_empty_string:\" is True\n" ] } ], "prompt_number": 61 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Empty List\n", "It is generally not a good idea to use the `==` to check for empty lists. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_empty_list = []\n", "\n", "\n", "print('my_empty_list == True:', my_empty_list == True)\n", "print('my_empty_list is True:', my_empty_list is True)\n", "\n", "print('my_empty_list == None:', my_empty_list == None)\n", "print('my_empty_list is None:', my_empty_list is None)\n", "\n", "print('my_empty_list == False:', my_empty_list == False)\n", "print('my_empty_list is False:', my_empty_list is False)\n", "\n", "\n", "if my_empty_list:\n", " print('\"if my_empty_list:\" is True')\n", "else:\n", " print('\"if my_empty_list:\" is False')\n", " \n", "if not my_empty_list:\n", " print('\"if not my_empty_list:\" is True')\n", "else:\n", " print('\"if not my_empty_list:\" is False')\n", "\n", "\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "my_empty_list == True: False\n", "my_empty_list is True: False\n", "my_empty_list == None: False\n", "my_empty_list is None: False\n", "my_empty_list == False: False\n", "my_empty_list is False: False\n", "\"if my_empty_list:\" is False\n", "\"if not my_empty_list:\" is True\n" ] } ], "prompt_number": 67 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [0]-List" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_zero_list = [0]\n", "\n", "\n", "print('my_zero_list == True:', my_zero_list == True)\n", "print('my_zero_list is True:', my_zero_list is True)\n", "\n", "print('my_zero_list == None:', my_zero_list == None)\n", "print('my_zero_list is None:', my_zero_list is None)\n", "\n", "print('my_zero_list == False:', my_zero_list == False)\n", "print('my_zero_list is False:', my_zero_list is False)\n", "\n", "\n", "if my_zero_list:\n", " print('\"if my_zero_list:\" is True')\n", "else:\n", " print('\"if my_zero_list:\" is False')\n", " \n", "if not my_zero_list:\n", " print('\"if not my_zero_list:\" is True')\n", "else:\n", " print('\"if not my_zero_list:\" is False')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "my_zero_list == True: False\n", "my_zero_list is True: False\n", "my_zero_list == None: False\n", "my_zero_list is None: False\n", "my_zero_list == False: False\n", "my_zero_list is False: False\n", "\"if my_zero_list:\" is True\n", "\"if not my_zero_list:\" is False\n" ] } ], "prompt_number": 70 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List comparison \n", "List comparisons are a handy way to show the difference between `==` and `is`. \n", "While `==` is rather evaluating the equality of the value, `is` is checking if two objects are equal.\n", "The examples below show that we can assign a pointer to the same list object by using `=`, e.g., `list1 = list2`. If we want to make a copy of the list values, we have to make a little " ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_first_list = [1]\n", "my_second_list = [1]\n", "print('my_first_list == my_second_list:', my_first_list == my_second_list)\n", "print('my_first_list is my_second_list:', my_first_list is my_second_list)\n", "\n", "\n", "my_third_list = my_first_list\n", "print('my_first_list == my_third_list:', my_first_list == my_third_list)\n", "print('my_first_list is my_third_list:', my_first_list is my_third_list)\n", "\n", "my_fourth_list = my_first_list[:]\n", "print('my_first_list == my_fourth_list:', my_first_list == my_fourth_list)\n", "print('my_first_list is my_fourth_list:', my_first_list is my_fourth_list)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "my_first_list == my_second_list: True\n", "my_first_list is my_second_list: False\n", "my_first_list == my_third_list: True\n", "my_first_list is my_third_list: True\n", "my_first_list == my_fourth_list: True\n", "my_first_list is my_fourth_list: False\n" ] } ], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }