{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditionals and their caveats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# `==` and `is`\n", "[Condensed from this SO thread](http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python) \n", "\n", "The rule of thumb to when to use `==` or `is`.\n", "- `==` is for value equality. Use to know if two objects *have the same value*.\n", "- `is` is for reference equality. Use to know if two references *refer to the same object*.\n", "\n", "OR more compactly;\n", "- `is` - identity testing,\n", "- `==` - equality testing. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3]\n", "b = a\n", "b is a " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b == a" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a[:]\n", "b is a" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b == a" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "# Also and most annoyingly;\n", "print([] is [])\n", "print([] == []) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The `while` statement" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def funzo():\n", " contin = input(\"Are you having fun yet?(Y/N) \")\n", " while contin == \"N\":\n", " contin = input(\"I'm sorry! :( \\n Are you having fun now? (Y/N) \")\n", " while contin == \"Y\":\n", " return \"Good! I hope your happy now.\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Are you having fun yet?(Y/N) N\n", "I'm sorry! :( \n", " Are you having fun now? (Y/N) N\n", "I'm sorry! :( \n", " Are you having fun now? (Y/N) N\n", "I'm sorry! :( \n", " Are you having fun now? (Y/N) Y\n" ] }, { "data": { "text/plain": [ "'Good! I hope your happy now.'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "funzo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }