Skip to content

Instantly share code, notes, and snippets.

@bastibe
Created March 27, 2013 13:18
Show Gist options
  • Save bastibe/5254089 to your computer and use it in GitHub Desktop.
Save bastibe/5254089 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Python Basics"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to Python\n",
"\n",
"Why Python?\n",
"\n",
"- Python is free\n",
"- Python is Open Source\n",
"- Python is more powerful than Matlab"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Typing\n",
"\n",
"Like Matlab, Python is *dynamically typed*. \n",
"Unlinke Matlab, Python is *strongly typed*.\n",
"\n",
"### Dynamic Typing\n",
"\n",
"(Like Matlab, unlike C)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Python:\n",
"a = 5.0\n",
"a = 'text'\n",
"a = [1, 2, 3]"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"# C:\n",
"float a = 5.0;\n",
"char b[] = \"text\";\n",
"int c[3] = {1, 2, 3};"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Strongly Typed\n",
"\n",
"(unlike Matlab or C)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Matlab:\n",
"a = 'text';\n",
"b = a + 5;"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Python:\n",
"a = 'text'\n",
"b = a + 5"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Syntax\n",
"\n",
"Many small differences to Matlab\n",
"\n",
"### No Semikolons\n",
"\n",
"$\\Rightarrow$ use `print()`-function instead"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# does not print:\n",
"a = 5\n",
"# instead:\n",
"print(a)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Branches, Loops\n",
"\n",
"*Whitespace is important!*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Python:\n",
"if a == 5:\n",
" print('A is five')\n",
"elif a == 6:\n",
" print('A is six')\n",
"else:\n",
" print('A is neither five nor six')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Matlab:\n",
"if a == 5\n",
" disp('A is five')\n",
"elseif a == 6\n",
" disp('A is six')\n",
"else\n",
" disp('A is neither five nor six')\n",
"end"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Write for-loops with `in` instead of `=`*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Python:\n",
"for n in [1, 2, 3, 4, 5, 6]: # These commas are important!\n",
" print(n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Matlab:\n",
"for n = [1 2 3 4 5 6]\n",
" disp(n)\n",
"end"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*While-Loop:*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Fibonacci Series: 1 1 2 3 5 8 13...\n",
"a = 1\n",
"b = 1\n",
"while b < 100:\n",
" print(b)\n",
" tmp = b\n",
" b = a + b\n",
" a = tmp"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n",
"5\n",
"8\n",
"13\n",
"21\n",
"34\n",
"55\n",
"89\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Multiple Assignment:*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# shorter:\n",
"a = 1\n",
"b = 1\n",
"while b < 100:\n",
" print(b)\n",
" a, b = b, a + b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n",
"5\n",
"8\n",
"13\n",
"21\n",
"34\n",
"55\n",
"89\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Types"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# float\n",
"a = 5.0\n",
"print(type(a))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<class 'float'>\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# integer\n",
"a = 4\n",
"print(type(a))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<class 'int'>\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# bool\n",
"a = True # or False\n",
"print(type(a))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<class 'bool'>\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# string\n",
"a = 'text'\n",
"print(type(a))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<class 'str'>\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# list\n",
"a = [1, 'text', True]\n",
"print(type(a))\n",
"print(a[0], a[1], a[2]) # counting from 0!"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<class 'list'>\n",
"1 text True\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# tuple\n",
"a = (1, 'text', True)\n",
"print(type(a))\n",
"print(a[0], a[1], a[2]) # counting from 0!"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<class 'tuple'>\n",
"1 text True\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# dictionary\n",
"a = {'first': 1,\n",
" 'second': 'text',\n",
" 'third': True}\n",
"print(type(a))\n",
"print(a['first'], a['second'], a['third'])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<class 'dict'>\n",
"1 text True\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# function (can be defined anywhere, not just in special files)\n",
"def func(a):\n",
" print('a ist ' + str(a))\n",
"\n",
"print(type(func))\n",
"func(a)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<class 'function'>\n",
"a ist {'second': 'text', 'third': True, 'first': 1}\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# class (can be defined anywhere, not just in special files)\n",
"class MyClass:\n",
" def __init__(self, num):\n",
" self.number = num\n",
" def func(self, num):\n",
" print(self.number + num)\n",
"\n",
"print(type(MyClass))\n",
"c = MyClass(5)\n",
"c.func(6)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<class 'type'>\n",
"11\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment