Skip to content

Instantly share code, notes, and snippets.

@keitheis
Last active December 16, 2015 11:49
Show Gist options
  • Save keitheis/5429819 to your computer and use it in GitHub Desktop.
Save keitheis/5429819 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "play3"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# play3 - Play Python 3 on IPython Notebook\n",
"\n",
"Lighting Talk @ [Taipei.py April 2013](http://www.meetup.com/Taipei-py/events/102668252/)\n",
"\n",
"* Author: *keitheis*\n",
"* Licence: [CC0 1.0](http://creativecommons.org/publicdomain/zero/1.0/)\n",
"\n",
"Reference the presentation of [Programming with Python - Basic by Mosky](http://www.slideshare.net/moskytw/programming-with-python-basic)\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def hello(name=None):\n",
" if name:\n",
" return 'Hello, %s!' % name\n",
" else:\n",
" return 'Hello, Python!'"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 108
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"if __name__ == '__main__':\n",
" print(hello(\"Main\"))\n",
"else:\n",
" print(hello())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Hello, Main!\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('End with a new line char.')\n",
"print('Print', 'multiple', 'strings.')\n",
"print('End with a space.',)\n",
"print() # print a new line char \"\\n\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"End with a new line char.\n",
"Print multiple strings.\n",
"End with a space.\n",
"\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('The world is', end=' never end. ')\n",
"print('a', 'b', 'c', sep=',')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The world is never end. a,b,c\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"### Data types and basic operations on Strings, Numbers and Booleans"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 105
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"3 + 3, 3 - 3, 3 * 3, 3 / 3, 3 % 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 21,
"text": [
"(6, 0, 9, 1.0, 0)"
]
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(_) # _ means last output of last input"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 16,
"text": [
"builtins.tuple"
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(3), type(\"3\"), type(3.0), type((0, 1, 2)), type([0, 1, 2])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 19,
"text": [
"(builtins.int, builtins.str, builtins.float, builtins.tuple, builtins.list)"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def get_bmi(weight, height):\n",
" bmi = weight / (height / 100) ** 2\n",
" return bmi\n",
"\n",
"get_bmi(49, 163)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 25,
"text": [
"18.442545824080696"
]
}
],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(_) # practice again"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 26,
"text": [
"builtins.float"
]
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(\"3\"), \"->\", int(\"3\"), \"->\", type(int(\"3\"))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 31,
"text": [
"(builtins.str, '->', 3, '->', builtins.int)"
]
}
],
"prompt_number": 31
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(\"3.3\"), \"->\", float(\"3.3\"), \"->\", type(float(\"3.3\"))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 32,
"text": [
"(builtins.str, '->', 3.3, '->', builtins.float)"
]
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"None, False, not True, True, not False, not None"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 36,
"text": [
"(None, False, False, True, True, True)"
]
}
],
"prompt_number": 36
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bool(None), bool(0), bool(False), bool([]), bool({}), bool(1), bool(True)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 112,
"text": [
"(False, False, False, False, False, True, True)"
]
}
],
"prompt_number": 112
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"True and True, True or False, False and True, False and False"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 113,
"text": [
"(True, True, False, False)"
]
}
],
"prompt_number": 113
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"### List and Dictionary"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 104
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[1, 2, 3, 4, 5, 6, 7]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 40,
"text": [
"[1, 2, 3, 4, 5, 6, 7]"
]
}
],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(_)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 41,
"text": [
"builtins.list"
]
}
],
"prompt_number": 41
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[i for i in range(7)] # List Comprehension"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 44,
"text": [
"[0, 1, 2, 3, 4, 5, 6]"
]
}
],
"prompt_number": 44
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"7 in range(7), 6 in range(7)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 49,
"text": [
"(False, True)"
]
}
],
"prompt_number": 49
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seven_range = range(7)\n",
"seven_range"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 83,
"text": [
"range(0, 7)"
]
}
],
"prompt_number": 83
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seven_range[0], seven_range[1], seven_range[1:3], seven_range[:3]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 84,
"text": [
"(0, 1, range(1, 3), range(0, 3))"
]
}
],
"prompt_number": 84
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for i in seven_range[2:7]:\n",
" print(i)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2\n",
"3\n",
"4\n",
"5\n",
"6\n"
]
}
],
"prompt_number": 85
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seven_list = [i for i in range(7)] # [0, 1, 2, 3, 4, 5, 6], not (0, 1, ..., 6) or\n",
"\n",
"seven_list.append('x') # append to list\n",
"del seven_list[1] # remove from list\n",
"\n",
"print(seven_list)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[0, 2, 3, 4, 5, 6, 'x']\n"
]
}
],
"prompt_number": 103
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(seven_list), len(seven_list[:3]), len(\"abc\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 88,
"text": [
"(7, 3, 3)"
]
}
],
"prompt_number": 88
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d = {\"d\": 1, \"e\": 2}\n",
"print(d)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'d': 1, 'e': 2}\n"
]
}
],
"prompt_number": 101
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(d)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 98,
"text": [
"2"
]
}
],
"prompt_number": 98
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d.keys()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 99,
"text": [
"dict_keys(['d', 'e'])"
]
}
],
"prompt_number": 99
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"e = dict(d=1, e=2)\n",
"print(e)\n",
"\n",
"del e['d']\n",
"print(e)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'d': 1, 'e': 2}\n",
"{'e': 2}\n"
]
}
],
"prompt_number": 102
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"### Flow Control"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 111
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"if d['d'] == 2:\n",
" print(\"(d['d'] == 2) is True\")\n",
"elif d['d'] < 3:\n",
" print(\"(d['d'] == 2) is not True\")\n",
"else:\n",
" print(\"whatever\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(d['d'] == 2) is not True\n"
]
}
],
"prompt_number": 121
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"(d['d'] == 2) is not True"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 122,
"text": [
"True"
]
}
],
"prompt_number": 122
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for item in seven_list:\n",
" print(item)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"x\n"
]
}
],
"prompt_number": 134
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# enumerate: get index iterated over the list\n",
"for index, item in enumerate(seven_list):\n",
" print(index, item)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0 0\n",
"1 2\n",
"2 3\n",
"3 4\n",
"4 5\n",
"5 6\n",
"6 x\n"
]
}
],
"prompt_number": 139
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for key in d:\n",
" print(key, ': ', d[key]) # value"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"d : 1\n",
"e : 2\n"
]
}
],
"prompt_number": 133
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# loop control: continue, break\n",
"for i, item in enumerate(seven_range):\n",
" if i < 2:\n",
" continue\n",
" if i > 4:\n",
" break\n",
" print(item)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2\n",
"3\n",
"4\n"
]
}
],
"prompt_number": 145
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# How to do nothing but fill the line?\n",
"if True:\n",
" pass"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 147
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"### Error handling"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 148
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"int(\"1.1\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: '1.1'",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-149-957f95e57e2e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"1.1\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '1.1'"
]
}
],
"prompt_number": 149
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"try:\n",
" int(\"1.1\")\n",
"except ValueError as value_error:\n",
" print(value_error)\n",
" print(float(\"1.1\"))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"invalid literal for int() with base 10: '1.1'\n",
"1.1\n"
]
}
],
"prompt_number": 157
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"### function definition"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 158
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def lovely_print(a, b, *args, **kwargs):\n",
" print(a, b)\n",
" print(*args)\n",
" print(kwargs)\n",
" return a + b"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 188
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# function keyword expansion\n",
"lovely_print(1, 2, 3, 4, 5, 6, 7, 8, c='C', d='D', e='E', f='F')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 2\n",
"3 4 5 6 7 8\n",
"{'d': 'D', 'e': 'E', 'f': 'F', 'c': 'C'}\n"
]
},
{
"output_type": "pyout",
"prompt_number": 189,
"text": [
"3"
]
}
],
"prompt_number": 189
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"my_list = [5, 4, 3]\n",
"my_dict = {'c': 'C', 'd': 'D'}\n",
"print(my_list, my_dict)\n",
"\n",
"lovely_print(9, 10, *my_list, **my_dict)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[5, 4, 3] {'d': 'D', 'c': 'C'}\n",
"9 10\n",
"5 4 3\n",
"{'d': 'D', 'c': 'C'}\n"
]
},
{
"output_type": "pyout",
"prompt_number": 192,
"text": [
"19"
]
}
],
"prompt_number": 192
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"### Help tools"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 193
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# dir the attributes of an object\n",
"print(dir(seven_range))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index', 'start', 'step', 'stop']\n"
]
}
],
"prompt_number": 216
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# see document string of an object\n",
"help(dir)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Help on built-in function dir in module builtins:\n",
"\n",
"dir(...)\n",
" dir([object]) -> list of strings\n",
" \n",
" If called without an argument, return the names in the current scope.\n",
" Else, return an alphabetized list of names comprising (some of) the attributes\n",
" of the given object, and of attributes reachable from it.\n",
" If the object supplies a method named __dir__, it will be used; otherwise\n",
" the default dir() logic is used and returns:\n",
" for a module object: the module's attributes.\n",
" for a class object: its attributes, and recursively the attributes\n",
" of its bases.\n",
" for any other object: its attributes, its class's attributes, and\n",
" recursively the attributes of its class's base classes.\n",
"\n"
]
}
],
"prompt_number": 217
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# \u65b0\u805e\u8207\u516c\u544a\n",
"\n",
"## [COSCUP@TICC Taipei.py \u793e\u7fa4\u8b70\u7a0b\u975e\u5e38\u5f37\u529b\u5fb5\u7a3f\u4e2d\uff01](http://www.meetup.com/Taipei-py/messages/boards/thread/33574662/)\n",
"\n",
"## [Taipei.py Workshops Pool](http://www.meetup.com/Taipei-py/polls/) - We're having Django Workshop!\n",
"\n",
"## [PyCon Taiwan 2013 \u7968\u623f\u71b1](http://registrano.com/events/pycon-taiwan-2013)\uff01\u5834\u5730\u7d44\u3001\u8b70\u7a0b\u7d44\u5fb5\u6c42\u5fd7\u5de5\u4e2d\uff0c\u610f\u8005\u8acb Email\uff1a[[email protected]](mailto://[email protected])"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment