Skip to content

Instantly share code, notes, and snippets.

@ripleyaffect
Last active January 30, 2017 15:51
Show Gist options
  • Save ripleyaffect/d63cb81a3a30da84df924bbe2c57f09f to your computer and use it in GitHub Desktop.
Save ripleyaffect/d63cb81a3a30da84df924bbe2c57f09f to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inclusion in a `list`\n",
"---------------------\n",
"True\n",
"False\n",
"\n",
"Inclusion in a `tuple`\n",
"----------------------\n",
"Error: 'tuple' object does not support item assignment\n",
"True\n",
"Error: argument of type 'int' is not iterable\n",
"True\n",
"False\n",
"\n",
"Inclusion in a `dict`\n",
"---------------------\n",
"True\n",
"False\n",
"\n",
"Inclusion in a `set`\n",
"--------------------\n",
"True\n",
"True\n",
"True\n",
"False\n"
]
}
],
"source": [
"# Inclusion and comparisons\n",
"# =========================\n",
"\n",
"# Inclusion\n",
"# ~~~~~~~~~\n",
"# Checking for \"inclusion\" means you're checking\n",
"# if a value is included in a collection. There\n",
"# are several different types of collections in\n",
"# python, including `list`s, `tuple`s, `dict`s,\n",
"# and `set`s.\n",
"\n",
"# You check for inclusion using the \"in\" keyword\n",
"# as in \"x in y\"\n",
"\n",
"print \"Inclusion in a `list`\"\n",
"print \"---------------------\"\n",
"print 1 in [1, 2, 3] # True\n",
"print 1 in [2, 3, 4] # False\n",
"\n",
"\n",
"print \"\\nInclusion in a `tuple`\"\n",
"print \"----------------------\"\n",
"\n",
"# A tuple is a lot like a list, but is \"immutable\",\n",
"# so you can't change it once it's been created\n",
"my_tuple = (1, 2, 3)\n",
"try:\n",
" my_tuple[0] = 4 # Error\n",
"except Exception as e:\n",
" print 'Error: {}'.format(e)\n",
"\n",
"print 1 in (1, 2, 3) # True\n",
"try:\n",
" print 1 in (1) # Error, parens don't make the tuple, commas do\n",
"except Exception as e:\n",
" print 'Error: {}'.format(e)\n",
"print 1 in (1,) # True, notice the comma after the 1\n",
"print 1 in (2, 3, 4) # False\n",
"\n",
"\n",
"print \"\\nInclusion in a `dict`\"\n",
"print \"---------------------\"\n",
"\n",
"# We're looking at the _keys_ here, not the values\n",
"print 1 in {\n",
" 1: 'one',\n",
" 2: 'two',\n",
" 3: 'three'} # True\n",
"print 1 in {\n",
" 'one': 1,\n",
" 'two': 2,\n",
" 'three': 3} # False, 1 is a value but not a key\n",
"\n",
"\n",
"print \"\\nInclusion in a `set`\"\n",
"print \"--------------------\"\n",
"\n",
"# `set`s look like `dict`s with just the keys and no\n",
"# values. Under the hood, looking up a value in a set\n",
"# is faster than looking up a value in a list\n",
"\n",
"print 1 in { 1 } # True\n",
"print 1 in { 1, 2, 3 } # True\n",
"print 1 in { 'one', 1, 1, 1, 'one' } # True\n",
"print 1 in { 'one', 2, 3 } # False"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Comparing `list`s\n",
"-----------------\n",
"False\n",
"True\n",
"False\n",
"\n",
"Comparing `list`s\n",
"-----------------\n",
"False\n",
"True\n",
"False\n",
"False\n",
"\n",
"Comparing `dict`s\n",
"-----------------\n",
"False\n",
"True\n",
"False\n",
"False\n",
"\n",
"Comparing `set`s\n",
"----------------\n",
"False\n",
"True\n",
"False\n",
"1\n",
"1\n",
"True\n"
]
}
],
"source": [
"# Comparison\n",
"# ~~~~~~~~~~\n",
"\n",
"print \"Comparing `list`s\"\n",
"print \"-----------------\"\n",
"\n",
"# For two `list`s to be equal, they must\n",
"# have the same values in the same order\n",
"print 1 == [1, 2, 3] # False, different types\n",
"print [1, 2, 3] == [1, 2, 3] # True\n",
"print [4, 2, 3] == [2, 3, 4] # False, different order\n",
"\n",
"\n",
"print \"\\nComparing `list`s\"\n",
"print \"-----------------\"\n",
"\n",
"# For two `tuple`s to be equal, they must\n",
"# also have the same values in the same order.\n",
"# `list`s and `tuple`s are not equivalent, even\n",
"# if they have the same values in the same order.\n",
"print 1 == (1, 2, 3) # False\n",
"print (1, 2, 3) == (1, 2, 3) # True\n",
"print (4, 2, 3) == (2, 3, 4) # False, different order\n",
"print (1, 2, 3) == [1, 2, 3] # False, different types\n",
"\n",
"\n",
"print \"\\nComparing `dict`s\"\n",
"print \"-----------------\"\n",
"\n",
"# For two `dict`s to be equal, they must have the\n",
"# same keys _and_ values. Order of the keys doesn't\n",
"# matter.\n",
"print 1 == {\n",
" 1: 'one',\n",
" 2: 'two',\n",
" 3: 'three'} # False, different types\n",
"print {\n",
" 1: 'one',\n",
" 2: 'two',\n",
" 3: 'three'\n",
"} == {\n",
" 3: 'three',\n",
" 2: 'two',\n",
" 1: 'one'} # True, order doesn't matter\n",
"print {\n",
" 1: 'one',\n",
" 2: 'two',\n",
" 3: 'three'\n",
"} == {\n",
" 1: 'uno',\n",
" 2: 'dos',\n",
" 3: 'tres'} # False, values are not the same\n",
"print {\n",
" 1: 'one',\n",
" 2: 'two',\n",
" 3: 'three'\n",
"} == {\n",
" 1: 'uno',\n",
" 2: 'dos'} # False, different keys\n",
"\n",
"\n",
"print \"\\nComparing `set`s\"\n",
"print \"----------------\"\n",
"\n",
"# For two `set`s to be equal, they must have the\n",
"# same values. Order doesn't matter.\n",
"print 1 == {1, 2, 3} # False, different types\n",
"print { 1, 2, 3 } == { 3, 2, 1 } # True, order doesn't matter\n",
"print { 1, 2, 3 } == { 1, 2 } # False, different values\n",
"\n",
"# Duplicate values are removed in a set, a value\n",
"# can only be present once.\n",
"print len({ 1 }) # 1\n",
"print len({ 1, 1, 1, 1, 1, 1, }) # Also 1\n",
"print { 1 } == { 1, 1, 1, 1, 1, 1, } # True, duplicates are removed\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment