Skip to content

Instantly share code, notes, and snippets.

@jabooth
Created March 8, 2015 17:06
Show Gist options
  • Select an option

  • Save jabooth/8256a49327873c342299 to your computer and use it in GitHub Desktop.

Select an option

Save jabooth/8256a49327873c342299 to your computer and use it in GitHub Desktop.
Advanced Python - 1. Basics
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Advanced Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Getting set up"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To use this notebook, you'll first need to install miniconda. To get set up you can follow the start of the [instructions given here for Windows, OS X, and Linux](http://www.menpo.org/installation/). Only go as far as 'Setting Up A Fresh Environment' then jump back here to install the stuff we need (not menpo).\n",
"\n",
"- Create a Python environment with all the code we need in it\n",
"\n",
"```\n",
"> conda create -n advpython python=3 pip matplotlib ipython-notebook\n",
"```\n",
"- Activate the environment. On OS X/Linux thats:\n",
"```\n",
"> source activate advpython\n",
"```\n",
"and on Windows...\n",
"```\n",
"> activate advpython\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {
"slide_type": "subslide"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Basics"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"#### Outputting state"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world\n"
]
}
],
"source": [
"print('hello world')"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"#### Variable definitions"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"# dynamic typing\n",
"x = 1\n",
"y = 2\n",
"z = x + y\n",
"print(z)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'ello world!\n"
]
}
],
"source": [
"# string literals\n",
"hello = \"'ello\"\n",
"world = 'world!'\n",
"print(hello + \" \" + world)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"\"1. 'ello\""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# strong typing\n",
"#x + hello # TypeError!\n",
"\n",
"# explictly cast to show our intent\n",
"str(x) + \". \" + hello # no complaining"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"#### Formatting strings"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1): 'ello world!\n"
]
}
],
"source": [
"# for building strings though we normally use format\n",
"# (simple template system - replaces '{}' with input)\n",
"s = \"{}): {} {}\".format(x, hello, world)\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"### Flow of control"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"#### `for` loop"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"# use range(n) to iterate from 0 -> n-1\n",
"for i in range(3):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n"
]
}
],
"source": [
"# we can also set the start and step size\n",
"for i in range(1, 3):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"25\n",
"50\n",
"75\n"
]
}
],
"source": [
"# jump by 25\n",
"for i in range(0, 76, +25):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"75\n",
"50\n",
"25\n",
"0\n"
]
}
],
"source": [
"# go backwards\n",
"for i in range(75, -1, -25):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"#### Side note - use ? to find out what a function does"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"# note this is IPython specific!\n",
"range?"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"#### Side note - use shift + tab to see this inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"range(# hit shift + tab immediately after '('"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"#### if else elif"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"internals": {},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hmm i is 3..\n"
]
}
],
"source": [
"i = 3\n",
"\n",
"if i == 2:\n",
" print('wahoo i is 2!')\n",
"elif i == 3:\n",
" print('hmm i is 3..')\n",
"else:\n",
" print('sadly, i is not 2...')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### basic functions"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def print_name(name):\n",
" print('hello {}'.format(name))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello james\n"
]
}
],
"source": [
"print_name('james')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment