Skip to content

Instantly share code, notes, and snippets.

@shunghsiyu
Last active August 26, 2015 13:51
Show Gist options
  • Save shunghsiyu/668bdd8c37cf6d74f6a3 to your computer and use it in GitHub Desktop.
Save shunghsiyu/668bdd8c37cf6d74f6a3 to your computer and use it in GitHub Desktop.
An Introduction to Python for Developers
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# An Introduction to the Python language v0.1\n",
"\n",
"and demo of the CMTS Configuration application\n",
"\n",
"[Download the IPython Notebook](https://gist.github.com/shunghsiyu/668bdd8c37cf6d74f6a3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Presentation Powered by [Jupyter/IPython Notebook](https://jupyter.org/), [RISE](https://github.com/damianavila/RISE) and [reveal.js](https://github.com/hakimel/reveal.js/)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"```\n",
"Running Python 2.7.10\n",
"\n",
"2015/08/25\n",
"Shung-Hsi Yu\n",
"Email: First Name + Last Name (No hyphen, no spaces) at Gmail\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Easy to Learn"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Built-in basic data types that mimic how do work in everday life"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Number\n",
"day = 25\n",
"price = 1.01"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# String\n",
"s1 = 'this is a String'\n",
"s2 = \"This works too\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# List\n",
"fruits = ['apple', 'orange', 'banana']"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# Dictionary\n",
"capital_of = {\n",
" 'Taiwan': 'Taipei',\n",
" 'United States': 'Washington D.C.',\n",
" 'Brazil': 'Brasília',\n",
" 'French': 'Paris'\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Clear Syntax"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Have a coherent syntax across different data structures"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"apple\n"
]
}
],
"source": [
"fruits = ['apple', 'orange', 'banana']\n",
"\n",
"# Access the first item\n",
"print fruits[0]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Taipei\n"
]
}
],
"source": [
"capital_of = {\n",
" 'Taiwan': 'Taipei',\n",
" 'United States': 'Washington D.C.',\n",
" 'Brazil': 'Brasília',\n",
" 'French': 'Paris'\n",
"}\n",
"\n",
"# Access the value\n",
"print capital_of['Taiwan']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# Add the Capital City of Germany\n",
"capital_of['Germany'] = 'Berlin'"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'United States': 'Washington D.C.', 'Brazil': 'Bras\\xc3\\xadlia', 'Taiwan': 'Taipei', 'Germany': 'Berlin', 'French': 'Paris'}\n"
]
}
],
"source": [
"print capital_of"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Variable Unpacking"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Seperates assignment from business logic"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"capital_of = {\n",
" 'Taiwan': 'Taipei',\n",
" 'United States': 'Washington D.C.',\n",
" 'Germany': 'Berlin',\n",
" 'Brazil': 'Brasília',\n",
" 'French': 'Paris'\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The capital city of United States is Washington D.C.\n",
"The capital city of Brazil is Brasília\n",
"The capital city of Taiwan is Taipei\n",
"The capital city of Germany is Berlin\n",
"The capital city of French is Paris\n"
]
}
],
"source": [
"for country_capital in capital_of.items():\n",
" print 'The capital city of', country_capital[0], 'is', country_capital[1]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The capital city of United States is Washington D.C.\n",
"The capital city of Brazil is Brasília\n",
"The capital city of Taiwan is Taipei\n",
"The capital city of Germany is Berlin\n",
"The capital city of French is Paris\n"
]
}
],
"source": [
"for country, capital in capital_of.items():\n",
" print 'The capital city of', country, 'is', capital"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Assignments doesn't just work in for-loops"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"scrolled": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x = 1\n",
"y = 2\n"
]
}
],
"source": [
"x, y = (1, 2)\n",
"\n",
"print 'x =', x\n",
"print 'y =', y"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am Shung-Hsi Yu , intern\n"
]
}
],
"source": [
"# Works with nested lists too\n",
"(first_name, last_name), position = [('Shung-Hsi', 'Yu'), 'intern']\n",
"\n",
"print 'I am', first_name, last_name, ',', position"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Explicit"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Ask for the data explicitly"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"colors = ['red', 'green', 'blue', 'yellow']\n",
"body_parts = ['nose', 'paw', 'teeth', 'neck']\n",
"animals = ['dog', 'cat', 'monkey', 'giraffe']"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Got a dog nose red\n",
"Got a cat paw green\n",
"Got a monkey teeth blue\n",
"Got a giraffe neck yellow\n"
]
}
],
"source": [
"for i in range(len(colors)):\n",
" print 'Got a', animals[i], body_parts[i], colors[i]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Got a red nose dog\n",
"Got a green paw cat\n",
"Got a blue teeth monkey\n",
"Got a yellow neck giraffe\n"
]
}
],
"source": [
"# Ask for colors, body_parts, animals explicitly and assign the values\n",
"for color, body_part, animal in zip(colors, body_parts, animals):\n",
" print 'Got a', color, body_part, animal"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Write like psuedo-cdoe"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"heights = [180, 157, 169, 172, 177]\n",
"min_height = 160"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Someone is under 160 cm!\n"
]
}
],
"source": [
"# If all the height in heights are greater than min_height\n",
"if all(height > min_height for height in heights):\n",
" print 'Everyone is over', min_height, 'cm!'\n",
"else:\n",
" print 'Someone is under', min_height, 'cm!'"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Someone is over 160 cm!\n"
]
}
],
"source": [
"# If any of the height are greater than min_height\n",
"if any(height > min_height for height in heights):\n",
" print 'Someone is over', min_height, 'cm!'\n",
"else:\n",
" print 'Noboday is over', min_height, 'cm!'"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[663, 669, 675, 681, 687, 693, 699, 705, 711, 717, 723, 729, 735, 741, 747, 753]\n"
]
}
],
"source": [
"data = '657,663,669,675,681,687,693,699,705,711,717,723,729,735,741,747'\n",
"\n",
"# Split the string at commas, and increment each by 6\n",
"print [int(freq) + 6 for freq in data.split(',')]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Differentiate between being equal and being identical"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"a = (1, 2)\n",
"b = (1, 2)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"# Equal in value\n",
"print a == b"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"# The same object, where some language have ===\n",
"print a is b"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Check for presence of an element with the `in` operator"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"months = [\n",
" 'January',\n",
" 'February',\n",
" 'March',\n",
" 'April',\n",
" 'May',\n",
" 'June', # Exclude this\n",
" 'July', # and this\n",
" 'August',\n",
" 'September',\n",
" 'October',\n",
" 'November',\n",
" 'December'\n",
"]\n",
"\n",
"excludes = ['June', 'July']"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"January\n",
"February\n",
"March\n",
"April\n",
"May\n",
"August\n",
"September\n",
"October\n",
"November\n",
"December\n"
]
}
],
"source": [
"for month in months:\n",
" if month not in excludes:\n",
" print month"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Reduce Keystroke"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Differnet ways to write a string literal"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Escaping manually\n",
"manual_escape = '\\\\r\\\\n'\n",
"\n",
"# Much simpler\n",
"regex_str = r'\\r\\n'"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print regex_str == manual_escape"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the first line\n",
"Need to escape single quote '\n"
]
}
],
"source": [
"# Manually breaking and appending '\\n'\n",
"manual_long_str = 'This is the first line\\n' \\\n",
" 'Need to escape single quote \\''\n",
"print manual_long_str"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the first line\n",
"Don't need to escape single quote '\n",
"\n"
]
}
],
"source": [
"# The \\n is taken literally\n",
"long_str = '''\\\n",
"This is the first line\n",
"Don't need to escape single quote '\n",
"'''\n",
"print long_str"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Context manager handles the trivial routines"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"f = open('intro_to_python.txt', 'w')\n",
"f.write('''\\\n",
"This is\n",
"an\n",
"introduction to\n",
"Python\n",
"''')\n",
"# Bad things can happen when you forget this line\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is\n",
"an\n",
"introduction to\n",
"Python\n",
"\n"
]
}
],
"source": [
"# f.close() is called automatically when leaving the idented block\n",
"# whether the code exit the block because of an exception or exiting normally\n",
"with open('intro_to_python.txt') as f:\n",
" print f.read()"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 This is\n",
"1 an\n",
"2 introduction to\n",
"3 Python\n"
]
}
],
"source": [
"# Print the content of the line along with the line number\n",
"with open('intro_to_python.txt') as f:\n",
" # enumerate(f) returns an index and the content of a single element in f each time\n",
" for idx, line in enumerate(f):\n",
" print idx, line,"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"def fib(n):\n",
" \"\"\"Calculate the n-th integer in the Fibonacci Sequence\n",
" 1, 1, 2, 3, 5, 8, 13, 21, ...\n",
" \"\"\"\n",
" # No curly brackets \n",
" if n < 1:\n",
" return 0 \n",
" # elif instead of else if\n",
" elif 1 < n <= 2: # the equivalent of 1 < n and n <= 2\n",
" return 1\n",
" else:\n",
" return fib(n-1)+fib(n-2)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3524578"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fib(34)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Fast to Program"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"from functools import wraps\n",
"\n",
"store = {}\n",
"def memorize(func, storage=store):\n",
" \n",
" # A function will be called instead of the original\n",
" @wraps(func)\n",
" def wrapper(arg):\n",
" \"\"\"This will only work when the target function takes a single parameter\"\"\"\n",
" if arg not in storage:\n",
" print 'Running for', arg\n",
" # Call the original function and put it in cache\n",
" storage[arg] = func(arg)\n",
" else:\n",
" print 'Using cached value for', arg\n",
" return storage[arg]\n",
" \n",
" # Replace the original function\n",
" return wrapper"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# I talked about @lru_cache in the presentation, but didn't realize that was new in Python 3.2\n",
"# So I'm keeping the memorize decorator I wrote\n",
"@memorize\n",
"def fib(n):\n",
" \"\"\"Calculate the n-th integer in the Fibonacci Sequence\n",
" 1, 1, 2, 3, 5, 8, 13, 21, ...\n",
" \"\"\"\n",
" # No curly brackets \n",
" if n < 1:\n",
" return 0 \n",
" # elif instead of else if\n",
" elif 1 < n <= 2: # the equivalent of 1 < n and n <= 2\n",
" return 1\n",
" else:\n",
" return fib(n-1)+fib(n-2)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false,
"scrolled": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running for 34\n",
"Running for 33\n",
"Running for 32\n",
"Running for 31\n",
"Running for 30\n",
"Running for 29\n",
"Running for 28\n",
"Running for 27\n",
"Running for 26\n",
"Running for 25\n",
"Running for 24\n",
"Running for 23\n",
"Running for 22\n",
"Running for 21\n",
"Running for 20\n",
"Running for 19\n",
"Running for 18\n",
"Running for 17\n",
"Running for 16\n",
"Running for 15\n",
"Running for 14\n",
"Running for 13\n",
"Running for 12\n",
"Running for 11\n",
"Running for 10\n",
"Running for 9\n",
"Running for 8\n",
"Running for 7\n",
"Running for 6\n",
"Running for 5\n",
"Running for 4\n",
"Running for 3\n",
"Running for 2\n",
"Running for 1\n",
"Running for 0\n",
"Running for -1\n",
"Using cached value for 2\n",
"Using cached value for 3\n",
"Using cached value for 4\n",
"Using cached value for 5\n",
"Using cached value for 6\n",
"Using cached value for 7\n",
"Using cached value for 8\n",
"Using cached value for 9\n",
"Using cached value for 10\n",
"Using cached value for 11\n",
"Using cached value for 12\n",
"Using cached value for 13\n",
"Using cached value for 14\n",
"Using cached value for 15\n",
"Using cached value for 16\n",
"Using cached value for 17\n",
"Using cached value for 18\n",
"Using cached value for 19\n",
"Using cached value for 20\n",
"Using cached value for 21\n",
"Using cached value for 22\n",
"Using cached value for 23\n",
"Using cached value for 24\n",
"Using cached value for 25\n",
"Using cached value for 26\n",
"Using cached value for 27\n",
"Using cached value for 28\n",
"Using cached value for 29\n",
"Using cached value for 30\n",
"Using cached value for 31\n",
"Using cached value for 32\n"
]
},
{
"data": {
"text/plain": [
"3524578"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Try calling it more than once\n",
"# The result will be cached\n",
"fib(34)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Batteries Included"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Both syntax return a set object\n",
"fruits = set(['orange', 'mango', 'apple', 'banana'])\n",
"companies = {'comcast', 'microsoft', 'apple', 'facebook', 'google'}"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set(['apple'])\n"
]
}
],
"source": [
"# Find the common elements between the two\n",
"print companies & fruits"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set(['google', 'apple', 'banana', 'mango', 'facebook', 'comcast', 'orange', 'microsoft'])\n"
]
}
],
"source": [
"# Combine the two\n",
"print companies | fruits"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set(['google', 'facebook', 'comcast', 'microsoft'])\n"
]
}
],
"source": [
"# Elements in one but not the other\n",
"print companies - fruits"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"import subprocess\n",
"\n",
"# Run shell command `ls -ld` \n",
"p = subprocess.Popen(['ls', '-ld'], stdout=subprocess.PIPE)\n",
"stdout, stderr = p.communicate()\n",
"print stdout"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Large Set of Libraries"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"For the following code to run, please install the **Flask** library using command line prompt/shell with the following command:\n",
"\n",
"```\n",
"pip install flask\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Import will fail if Flask is not installed\n",
"from flask import Flask, request"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"app = Flask('AppName')\n",
"\n",
"# Direct HTTP GET Request for the root to this function\n",
"@app.route('/')\n",
"def index():\n",
" return '''\n",
" <h1>Hello World</h1>\n",
" '''\n",
"\n",
"# Direct HTTP GET or POST Request for /receive_post to this function\n",
"@app.route('/receive_post', methods=['GET', 'POST'])\n",
"def receive_post():\n",
" # Checks the Request Method\n",
" if request.method == 'POST':\n",
" # Retrieve value from the form submitted\n",
" some_input = request.form['someinput']\n",
" return 'You typed ' + some_input\n",
" return '''\n",
" <form action=\"\" method=\"post\">\n",
" <input type=text name=someinput><br/>\n",
" <input type=submit>\n",
" </form>\n",
" '''"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# A work-around that starts the web server in a different thread\n",
"# or else this notebook will continue to run the web server indefinitely\n",
"import threading\n",
"t = threading.Thread(target=app.run)\n",
"t.setDaemon(True)\n",
"t.start()\n",
"\n",
"# DO NOT run this block more than once (without restarting the Ipython Kernel)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Now navigate to http://localhost:5000/ and you'll see 'Hello World'\n",
"\n",
"Navigate to http://localhost:5000/receive_post and you'll see a form with a textbox that echos your input"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## More Syntax Sugar"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Ternary Operator"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"assign_one = True\n",
"will_be_one = 1 if assign_one else 2\n",
"\n",
"print will_be_one"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### For-else statement"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Eating apple\n",
"Eating orange\n",
"I hate banana\n"
]
}
],
"source": [
"fruits = ['apple', 'orange', 'banana', 'mango']\n",
"\n",
"# To check if break have been called at all\n",
"# Need to have a boolean with a initial value\n",
"ate_all = True\n",
"for fruit in fruits:\n",
" if fruit == 'banana':\n",
" print 'I hate banana'\n",
" # And negate the boolean when going to call break\n",
" ate_all = False\n",
" break\n",
" print 'Eating', fruit\n",
"\n",
"# Checks the value of the boolean\n",
"if ate_all:\n",
" print 'I ate all the fruits'"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Eating apple\n",
"Eating orange\n",
"I hate banana\n"
]
}
],
"source": [
"# Try removing 'banana'\n",
"fruits = ['apple', 'orange', 'banana', 'mango']\n",
"\n",
"for fruit in fruits:\n",
" if fruit == 'banana':\n",
" print 'I hate banana'\n",
" break\n",
" print 'Eating', fruit\n",
"else: # else == non-break\n",
" # This block runs when break is not called\n",
" # and every single element have been 'used' in the for-block\n",
" print 'I ate all the fruits'"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Slicing"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"acronyms = ['CDCB', 'CMTS', 'CRAN', 'QUM']"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['CDCD', 'CMTS', 'CRAN', 'QUM']\n"
]
}
],
"source": [
"# Correct the typo in the first element\n",
"acronyms[0] = 'CDCD'\n",
"print acronyms"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['CDCD', 'I-CMTS', 'M-CMTS', 'CRAN', 'QUM']\n"
]
}
],
"source": [
"# Insert another list at index 1\n",
"# And remove the original elements between index 1 (including) and index 2 (excluding)\n",
"acronyms[1:2] = ['I-CMTS', 'M-CMTS']\n",
"print acronyms"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['CDCD', 'I-CMTS', 'M-CMTS', 'CRAN', 'QAM']\n"
]
}
],
"source": [
"# Correcting the type in the last element\n",
"acronyms[-1] = 'QAM'\n",
"print acronyms"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"o\n",
"c\n",
" \n",
"e\n",
"t\n",
"o\n",
"r\n",
" \n",
"r\n",
"g\n"
]
}
],
"source": [
"# A string is also a sequence, just like list\n",
"# Take one from every three characters, starting from index 1\n",
"for char in 'You can iterate over a string'[1::3]:\n",
" print char"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Try it\n",
"\n",
"[Online Python Interpretor](http://www.skulpt.org/)\n",
"\n",
"[Python Official Website](https://www.python.org/)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# CMTS Configuration Tool Demo\n",
"\n",
"http://76.96.120.214/cmts/cmts_tools"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment