Created
April 15, 2016 08:04
-
-
Save chehab/34613e4cd8306fc8368f828a855761e9 to your computer and use it in GitHub Desktop.
Based on LearnPython.org
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Learn Python, Crash Course" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Hello, World!" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "This line will be printed.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(\"This line will be printed.\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Indentation" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "x is true.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "x = True\n", | |
| "if x is True:\n", | |
| " # indented four spaces NOT TABs\n", | |
| " print(\"x is true.\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Variables and Types" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Numbers" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "7\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "my_int = 7\n", | |
| "print(my_int)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 18, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "7.0\t7.0\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "my_float = 7.0\n", | |
| "my_float = float(7)\n", | |
| "\n", | |
| "print(my_float, my_float, sep=\"\\t\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "one = 1; two = 2\n", | |
| "three = one + two\n", | |
| "print(three)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Strings" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 20, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "hello world!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "hello = 'hello'\n", | |
| "print(hello, end=' ')\n", | |
| "world = \"world!\"\n", | |
| "print(world)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 21, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Don't worry about apostrophes\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "my_string = \"Don't worry about apostrophes\"\n", | |
| "print(my_string)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 22, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "hello world!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "hello_world = hello + \" \" + world\n", | |
| "print(hello_world)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 23, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "hello world!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "a, b = 'hello', 'world!'\n", | |
| "print(a,b, sep=' ')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "ename": "TypeError", | |
| "evalue": "unsupported operand type(s) for +: 'int' and 'str'", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[0;32m<ipython-input-24-1bc13855b99c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# This will not work!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mone\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mtwo\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mhello\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
| "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# This will not work!\n", | |
| "print(one + two + hello)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Lists\n", | |
| "\n", | |
| "Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner. Here is an example of how to build a list." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 25, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1\n", | |
| "1\t|\t2\t|\t3\t|\t" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "mylist = []\n", | |
| "mylist.append(1)\n", | |
| "mylist.append(2)\n", | |
| "mylist.append(3)\n", | |
| "print(mylist[0]) # prints 1\n", | |
| "\n", | |
| "# prints out 1,2,3\n", | |
| "for x in mylist:\n", | |
| " print(x, end=\"\\t|\\t\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 26, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "ename": "IndexError", | |
| "evalue": "list index out of range", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[0;32m<ipython-input-26-e08774710845>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Accessing an index which does not exist generates an exception (an error).\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmylist\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmylist\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
| "\u001b[0;31mIndexError\u001b[0m: list index out of range" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Accessing an index which does not exist generates an exception (an error).\n", | |
| "mylist = [1,2,3]\n", | |
| "print(mylist[10])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Basic Operators" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Arithmetic Operators" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 27, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2.5" | |
| ] | |
| }, | |
| "execution_count": 27, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "1 + 2 * 3 / 4.0" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 28, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2" | |
| ] | |
| }, | |
| "execution_count": 28, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "11 % 3 # remainder" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 29, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "49" | |
| ] | |
| }, | |
| "execution_count": 29, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "7 ** 2 # power" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 30, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2.5" | |
| ] | |
| }, | |
| "execution_count": 30, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "5 / 2 # division python3" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 31, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2" | |
| ] | |
| }, | |
| "execution_count": 31, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "5 // 2 # division python3" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Using Operators with Strings" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 32, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'hello world'" | |
| ] | |
| }, | |
| "execution_count": 32, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"hello\" + \" \" + \"world\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 33, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'--------------------'" | |
| ] | |
| }, | |
| "execution_count": 33, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"--\" * 10" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Using Operators with Lists" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 34, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[2, 4, 6, 8, 1, 3, 5, 7]" | |
| ] | |
| }, | |
| "execution_count": 34, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "[2,4,6,8] + [1,3,5,7]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 35, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1, 2, 3, 1, 2, 3, 1, 2, 3]" | |
| ] | |
| }, | |
| "execution_count": 35, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "[1,2,3] * 3" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# String Formatting\n", | |
| "\n", | |
| "Python uses C-style string formatting to create new, formatted strings. The \"%\" operator is used to format a set of variables enclosed in a \"tuple\" (a fixed size list), together with a format string, which contains normal text together with \"argument specifiers\", special symbols like \"%s\" and \"%d\".\n", | |
| "\n", | |
| "Let's say you have a variable called \"name\" with your user name in it, and you would then like to print out a greeting to that user." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 36, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Hello, John!'" | |
| ] | |
| }, | |
| "execution_count": 36, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "name = 'John'\n", | |
| "\"Hello, %s!\" % name" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 37, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'John is 23 years old.'" | |
| ] | |
| }, | |
| "execution_count": 37, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "name = \"John\"\n", | |
| "age = 23\n", | |
| "\"%s is %d years old.\" % (name, age)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 38, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'A list: [1, 2, 3]'" | |
| ] | |
| }, | |
| "execution_count": 38, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "mylist = [1,2,3]\n", | |
| "\"A list: %s\" % mylist" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "| Conversion | Meaning Notes |\n", | |
| "|---|---|\n", | |
| "|%s | String (or any object with a string representation, like numbers) |\n", | |
| "|%d | Integers|\n", | |
| "|%f | Floating point numbers|\n", | |
| "|%.`PRC`f | Floating point numbers with a fixed dicimal precision.|\n", | |
| "|%x/%X | Integers in hex representation (lowercase/uppercase)|" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "#### New Style String _'`format`'_ function" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 39, | |
| "metadata": { | |
| "collapsed": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'John is 25 years old.'" | |
| ] | |
| }, | |
| "execution_count": 39, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"{name} is {age} years old.\".format(name=\"John\", age=25)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 40, | |
| "metadata": { | |
| "collapsed": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'A list: [1, 2, 3]'" | |
| ] | |
| }, | |
| "execution_count": 40, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "mylist = [1,2,3]\n", | |
| "\"A list: {}\".format(mylist)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 41, | |
| "metadata": { | |
| "collapsed": true, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "skip" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from math import pi" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 42, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Pi= 3.142'" | |
| ] | |
| }, | |
| "execution_count": 42, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"Pi= %.3f\" % pi" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Basic String Operations" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 43, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "4" | |
| ] | |
| }, | |
| "execution_count": 43, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"Hello World!\".index(\"o\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 44, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "3" | |
| ] | |
| }, | |
| "execution_count": 44, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"Hello World!\".count(\"l\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 45, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'lo W'" | |
| ] | |
| }, | |
| "execution_count": 45, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"Hello World!\"[3:7]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 46, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'loW'" | |
| ] | |
| }, | |
| "execution_count": 46, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"Hello World!\"[2:7:2]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 47, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'!dlroW olleH'" | |
| ] | |
| }, | |
| "execution_count": 47, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"Hello World!\"[::-1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 48, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "HELLO WORLD! hello world!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(\"Hello World!\".upper(), \"Hello World!\".lower() )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 49, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "True False\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(\"Hello World!\".startswith(\"Hello\"), \"Hello World!\".endswith(\"Hello\"),)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 50, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['Hello', 'World!']" | |
| ] | |
| }, | |
| "execution_count": 50, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"Hello World!\".split(\" \")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Conditions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 51, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "True\n", | |
| "False\n", | |
| "True\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "x = 2\n", | |
| "print(x == 2)\n", | |
| "print(x == 3)\n", | |
| "print(x < 3)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Boolean operators" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 52, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Your name is John, and you are also 23 years old.\n", | |
| "Your name is either John or Rick.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "name = \"John\"\n", | |
| "age = 23\n", | |
| "if name == \"John\" and age == 23:\n", | |
| " print(\"Your name is John, and you are also 23 years old.\")\n", | |
| "\n", | |
| "if name == \"John\" or name == \"Rick\":\n", | |
| " print(\"Your name is either John or Rick.\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### The _'`in`'_ Operator\n", | |
| "\n", | |
| "The \"in\" operator could be used to check if a specified object exists within an iterable object container, such as a list:\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 53, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Your name is either John or Rick.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "if name in [\"John\", \"Rick\"]:\n", | |
| " print(\"Your name is either John or Rick.\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "```python\n", | |
| "if <statement is true>:\n", | |
| " <do something>\n", | |
| " ....\n", | |
| " ....\n", | |
| "elif <another statement is true>: # else if\n", | |
| " <do something else>\n", | |
| " ....\n", | |
| " ....\n", | |
| "else:\n", | |
| " <do another thing>\n", | |
| " ....\n", | |
| " ....\n", | |
| "```" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 54, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "x equals two!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "x = 2\n", | |
| "if x == 2:\n", | |
| " print(\"x equals two!\")\n", | |
| "else:\n", | |
| " print(\"x does not equal to two.\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### The _'`is`'_ Operator\n", | |
| "\n", | |
| "Unlike the double equals operator \"==\", the \"is\" operator does not match the values of the variables, but the instances themselves. For example:\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 55, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "True\n", | |
| "False\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "x = [1,2,3]\n", | |
| "y = [1,2,3]\n", | |
| "print(x == y)\n", | |
| "print(x is y)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### The _'not'_ Operator\n", | |
| "\n", | |
| "Using \"not\" before a boolean expression inverts it:\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 56, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "True\n", | |
| "False\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(not False)\n", | |
| "print((not False) == (False))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Loops\n", | |
| "\n", | |
| "There are two types of loops in Python, _'`for`'_ and _'`while`'_." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### The _'`for`'_ loop" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 57, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "2\t3\t5\t7\t" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "primes = [2, 3, 5, 7]\n", | |
| "for prime in primes:\n", | |
| " print(prime, end=\"\\t\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 58, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0\t1\t2\t3\t4\t" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for x in range(5):\n", | |
| " print(x, end=\"\\t\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 59, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "3\t5\t7\t" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for x in range(3, 8, 2):\n", | |
| " print(x, end=\"\\t\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### The _'`while`'_ loop\n", | |
| "\n", | |
| "While loops repeat as long as a certain boolean condition is met. For example:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 60, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0 | 1 | 2 | 3 | 4 | " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "count = 0\n", | |
| "while count < 5:\n", | |
| " print(count, end=\" | \")\n", | |
| " count += 1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### _'`break`'_ and _'`continue`'_ Statements" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 61, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0 1 2 3 4 " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "count = 0\n", | |
| "while True:\n", | |
| " print(count, end=' ')\n", | |
| " count += 1\n", | |
| " if count >= 5:\n", | |
| " break" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 62, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 3 5 7 9 " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for x in range(10):\n", | |
| " # Check if x is even\n", | |
| " if x % 2 == 0:\n", | |
| " continue\n", | |
| " print(x, end=\" \")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### can we use \"else\" clause for loops?\n", | |
| "\n", | |
| "unlike languages like C, CPP.. we can use else for loops. When the loop condition of \"`for`\" or \"`while`\" statement fails then code part in \"else\" is executed. \n", | |
| "\n", | |
| "If `break` statement is executed inside for loop then the \"`else`\" part is skipped. \n", | |
| "\n", | |
| "Note that \"`else`\" part is executed even if there is a `continue` statement." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "count=0\n", | |
| "while(count<5):\n", | |
| " print(count, end=\" \")\n", | |
| " count +=1\n", | |
| "else:\n", | |
| " print(\"\\ncount value reached %d\" %(count))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 63, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 2 3 4 " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Prints out 1,2,3,4\n", | |
| "for i in range(1,10):\n", | |
| " if(i%5==0):\n", | |
| " break\n", | |
| " print(i, end=\" \")\n", | |
| "else:\n", | |
| " print(\"this is not printed because for loop is terminated because of break but not due to fail in condition\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Functions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "How do you write functions in Python?\n", | |
| "As we have seen on previous tutorials, Python makes use of blocks.\n", | |
| "\n", | |
| "A block is a area of code of written in the format of:\n", | |
| "\n", | |
| "```python\n", | |
| "block_head: \n", | |
| " 1st block line \n", | |
| " 2nd block line \n", | |
| " ...\n", | |
| "```" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 64, | |
| "metadata": { | |
| "collapsed": true, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def my_function():\n", | |
| " print(\"Hello From My Function!\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 65, | |
| "metadata": { | |
| "collapsed": true, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def my_function_with_args(name=\"There\", greeting=\"a good day!\"):\n", | |
| " print(\"Hello, %s , From My Function!, I wish you %s\"%(name, greeting))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 66, | |
| "metadata": { | |
| "collapsed": true, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def sum_two_numbers(a, b):\n", | |
| " return a + b" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### call functions in Python" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 67, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Hello From My Function!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "my_function()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 68, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "3" | |
| ] | |
| }, | |
| "execution_count": 68, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sum_two_numbers(1,2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 69, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Hello, John Doe , From My Function!, I wish you a great year!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "my_function_with_args(\"John Doe\", \"a great year!\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 70, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Hello, There , From My Function!, I wish you a great year!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "my_function_with_args(greeting=\"a great year!\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 71, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Hello, John Doe , From My Function!, I wish you a good day!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "my_function_with_args(name=\"John Doe\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Classes and Objects" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 72, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "class MyClass:\n", | |
| " variable = \"blah\"\n", | |
| "\n", | |
| " def function(self):\n", | |
| " print(\"This is a message inside the class. My var is %r\" % self.variable)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 73, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'blah'" | |
| ] | |
| }, | |
| "execution_count": 73, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "myobjectx = MyClass()\n", | |
| "myobjectx.variable" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 74, | |
| "metadata": { | |
| "collapsed": true, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "myobjectx.variable = 'Foo'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 75, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "This is a message inside the class. My var is 'Foo'\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "myobjectx.function()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Dictionaries\n", | |
| "\n", | |
| "A dictionary is a data type similar to arrays, but works with keys and values instead of indexes. Each value stored in a dictionary can be accessed using a key, which is any type of object (a string, a number, a list, etc.) instead of using its index to address it." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 76, | |
| "metadata": { | |
| "collapsed": true, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "phonebook = {}\n", | |
| "phonebook[\"John\"] = 938477566\n", | |
| "phonebook[\"Jack\"] = 938377264\n", | |
| "phonebook[\"Jill\"] = 947662781" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 77, | |
| "metadata": { | |
| "collapsed": true, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "phonebook = {\n", | |
| " \"John\" : 938477566,\n", | |
| " \"Jack\" : 938377264,\n", | |
| " \"Jill\" : 947662781\n", | |
| "}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Iterating over dictionaries\n", | |
| "\n", | |
| "Dictionaries can be iterated over, just like a list. However, a dictionary, unlike a list, does not keep the order of the values stored in it. To iterate over key value pairs, use the following syntax:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 78, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Phone number of Jack is 938377264\n", | |
| "Phone number of John is 938477566\n", | |
| "Phone number of Jill is 947662781\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for name, number in phonebook.items():\n", | |
| " print(\"Phone number of %s is %d\" % (name, number))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Removing a value" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 79, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "del phonebook[\"John\"]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 80, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "947662781" | |
| ] | |
| }, | |
| "execution_count": 80, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "phonebook.pop(\"Jill\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Modules and Packages" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "Modules in Python are simply Python files with the .py extension, which implement a set of functions. Modules are imported from other modules using the import command.\n", | |
| "\n", | |
| "To import a module, we use the import command. Check out the full list of built-in modules in the Python standard library here.\n", | |
| "\n", | |
| "The first time a module is loaded into a running Python script, it is initialized by executing the code in the module once. If another module in your code imports the same module again, it will not be loaded twice but once only - so local variables inside the module act as a \"singleton\" - they are initialized only once." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "#### import the library" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 81, | |
| "metadata": { | |
| "collapsed": true, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import math" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "source": [ | |
| "#### use it" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 82, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "120" | |
| ] | |
| }, | |
| "execution_count": 82, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "math.factorial(5)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Exploring built-in modules" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 83, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['__doc__',\n", | |
| " '__file__',\n", | |
| " '__loader__',\n", | |
| " '__name__',\n", | |
| " '__package__',\n", | |
| " '__spec__',\n", | |
| " 'acos',\n", | |
| " 'acosh',\n", | |
| " 'asin',\n", | |
| " 'asinh',\n", | |
| " 'atan',\n", | |
| " 'atan2',\n", | |
| " 'atanh',\n", | |
| " 'ceil',\n", | |
| " 'copysign',\n", | |
| " 'cos',\n", | |
| " 'cosh',\n", | |
| " 'degrees',\n", | |
| " 'e',\n", | |
| " 'erf',\n", | |
| " 'erfc',\n", | |
| " 'exp',\n", | |
| " 'expm1',\n", | |
| " 'fabs',\n", | |
| " 'factorial',\n", | |
| " 'floor',\n", | |
| " 'fmod',\n", | |
| " 'frexp',\n", | |
| " 'fsum',\n", | |
| " 'gamma',\n", | |
| " 'gcd',\n", | |
| " 'hypot',\n", | |
| " 'inf',\n", | |
| " 'isclose',\n", | |
| " 'isfinite',\n", | |
| " 'isinf',\n", | |
| " 'isnan',\n", | |
| " 'ldexp',\n", | |
| " 'lgamma',\n", | |
| " 'log',\n", | |
| " 'log10',\n", | |
| " 'log1p',\n", | |
| " 'log2',\n", | |
| " 'modf',\n", | |
| " 'nan',\n", | |
| " 'pi',\n", | |
| " 'pow',\n", | |
| " 'radians',\n", | |
| " 'sin',\n", | |
| " 'sinh',\n", | |
| " 'sqrt',\n", | |
| " 'tan',\n", | |
| " 'tanh',\n", | |
| " 'trunc']" | |
| ] | |
| }, | |
| "execution_count": 83, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import math\n", | |
| "dir(math)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 84, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": true, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Help on module math:\n", | |
| "\n", | |
| "NAME\n", | |
| " math\n", | |
| "\n", | |
| "MODULE REFERENCE\n", | |
| " http://docs.python.org/3.5/library/math\n", | |
| " \n", | |
| " The following documentation is automatically generated from the Python\n", | |
| " source files. It may be incomplete, incorrect or include features that\n", | |
| " are considered implementation detail and may vary between Python\n", | |
| " implementations. When in doubt, consult the module reference at the\n", | |
| " location listed above.\n", | |
| "\n", | |
| "DESCRIPTION\n", | |
| " This module is always available. It provides access to the\n", | |
| " mathematical functions defined by the C standard.\n", | |
| "\n", | |
| "FUNCTIONS\n", | |
| " acos(...)\n", | |
| " acos(x)\n", | |
| " \n", | |
| " Return the arc cosine (measured in radians) of x.\n", | |
| " \n", | |
| " acosh(...)\n", | |
| " acosh(x)\n", | |
| " \n", | |
| " Return the inverse hyperbolic cosine of x.\n", | |
| " \n", | |
| " asin(...)\n", | |
| " asin(x)\n", | |
| " \n", | |
| " Return the arc sine (measured in radians) of x.\n", | |
| " \n", | |
| " asinh(...)\n", | |
| " asinh(x)\n", | |
| " \n", | |
| " Return the inverse hyperbolic sine of x.\n", | |
| " \n", | |
| " atan(...)\n", | |
| " atan(x)\n", | |
| " \n", | |
| " Return the arc tangent (measured in radians) of x.\n", | |
| " \n", | |
| " atan2(...)\n", | |
| " atan2(y, x)\n", | |
| " \n", | |
| " Return the arc tangent (measured in radians) of y/x.\n", | |
| " Unlike atan(y/x), the signs of both x and y are considered.\n", | |
| " \n", | |
| " atanh(...)\n", | |
| " atanh(x)\n", | |
| " \n", | |
| " Return the inverse hyperbolic tangent of x.\n", | |
| " \n", | |
| " ceil(...)\n", | |
| " ceil(x)\n", | |
| " \n", | |
| " Return the ceiling of x as an int.\n", | |
| " This is the smallest integral value >= x.\n", | |
| " \n", | |
| " copysign(...)\n", | |
| " copysign(x, y)\n", | |
| " \n", | |
| " Return a float with the magnitude (absolute value) of x but the sign \n", | |
| " of y. On platforms that support signed zeros, copysign(1.0, -0.0) \n", | |
| " returns -1.0.\n", | |
| " \n", | |
| " cos(...)\n", | |
| " cos(x)\n", | |
| " \n", | |
| " Return the cosine of x (measured in radians).\n", | |
| " \n", | |
| " cosh(...)\n", | |
| " cosh(x)\n", | |
| " \n", | |
| " Return the hyperbolic cosine of x.\n", | |
| " \n", | |
| " degrees(...)\n", | |
| " degrees(x)\n", | |
| " \n", | |
| " Convert angle x from radians to degrees.\n", | |
| " \n", | |
| " erf(...)\n", | |
| " erf(x)\n", | |
| " \n", | |
| " Error function at x.\n", | |
| " \n", | |
| " erfc(...)\n", | |
| " erfc(x)\n", | |
| " \n", | |
| " Complementary error function at x.\n", | |
| " \n", | |
| " exp(...)\n", | |
| " exp(x)\n", | |
| " \n", | |
| " Return e raised to the power of x.\n", | |
| " \n", | |
| " expm1(...)\n", | |
| " expm1(x)\n", | |
| " \n", | |
| " Return exp(x)-1.\n", | |
| " This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.\n", | |
| " \n", | |
| " fabs(...)\n", | |
| " fabs(x)\n", | |
| " \n", | |
| " Return the absolute value of the float x.\n", | |
| " \n", | |
| " factorial(...)\n", | |
| " factorial(x) -> Integral\n", | |
| " \n", | |
| " Find x!. Raise a ValueError if x is negative or non-integral.\n", | |
| " \n", | |
| " floor(...)\n", | |
| " floor(x)\n", | |
| " \n", | |
| " Return the floor of x as an int.\n", | |
| " This is the largest integral value <= x.\n", | |
| " \n", | |
| " fmod(...)\n", | |
| " fmod(x, y)\n", | |
| " \n", | |
| " Return fmod(x, y), according to platform C. x % y may differ.\n", | |
| " \n", | |
| " frexp(...)\n", | |
| " frexp(x)\n", | |
| " \n", | |
| " Return the mantissa and exponent of x, as pair (m, e).\n", | |
| " m is a float and e is an int, such that x = m * 2.**e.\n", | |
| " If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.\n", | |
| " \n", | |
| " fsum(...)\n", | |
| " fsum(iterable)\n", | |
| " \n", | |
| " Return an accurate floating point sum of values in the iterable.\n", | |
| " Assumes IEEE-754 floating point arithmetic.\n", | |
| " \n", | |
| " gamma(...)\n", | |
| " gamma(x)\n", | |
| " \n", | |
| " Gamma function at x.\n", | |
| " \n", | |
| " gcd(...)\n", | |
| " gcd(x, y) -> int\n", | |
| " greatest common divisor of x and y\n", | |
| " \n", | |
| " hypot(...)\n", | |
| " hypot(x, y)\n", | |
| " \n", | |
| " Return the Euclidean distance, sqrt(x*x + y*y).\n", | |
| " \n", | |
| " isclose(...)\n", | |
| " is_close(a, b, *, rel_tol=1e-09, abs_tol=0.0) -> bool\n", | |
| " \n", | |
| " Determine whether two floating point numbers are close in value.\n", | |
| " \n", | |
| " rel_tol\n", | |
| " maximum difference for being considered \"close\", relative to the\n", | |
| " magnitude of the input values\n", | |
| " abs_tol\n", | |
| " maximum difference for being considered \"close\", regardless of the\n", | |
| " magnitude of the input values\n", | |
| " \n", | |
| " Return True if a is close in value to b, and False otherwise.\n", | |
| " \n", | |
| " For the values to be considered close, the difference between them\n", | |
| " must be smaller than at least one of the tolerances.\n", | |
| " \n", | |
| " -inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n", | |
| " is, NaN is not close to anything, even itself. inf and -inf are\n", | |
| " only close to themselves.\n", | |
| " \n", | |
| " isfinite(...)\n", | |
| " isfinite(x) -> bool\n", | |
| " \n", | |
| " Return True if x is neither an infinity nor a NaN, and False otherwise.\n", | |
| " \n", | |
| " isinf(...)\n", | |
| " isinf(x) -> bool\n", | |
| " \n", | |
| " Return True if x is a positive or negative infinity, and False otherwise.\n", | |
| " \n", | |
| " isnan(...)\n", | |
| " isnan(x) -> bool\n", | |
| " \n", | |
| " Return True if x is a NaN (not a number), and False otherwise.\n", | |
| " \n", | |
| " ldexp(...)\n", | |
| " ldexp(x, i)\n", | |
| " \n", | |
| " Return x * (2**i).\n", | |
| " \n", | |
| " lgamma(...)\n", | |
| " lgamma(x)\n", | |
| " \n", | |
| " Natural logarithm of absolute value of Gamma function at x.\n", | |
| " \n", | |
| " log(...)\n", | |
| " log(x[, base])\n", | |
| " \n", | |
| " Return the logarithm of x to the given base.\n", | |
| " If the base not specified, returns the natural logarithm (base e) of x.\n", | |
| " \n", | |
| " log10(...)\n", | |
| " log10(x)\n", | |
| " \n", | |
| " Return the base 10 logarithm of x.\n", | |
| " \n", | |
| " log1p(...)\n", | |
| " log1p(x)\n", | |
| " \n", | |
| " Return the natural logarithm of 1+x (base e).\n", | |
| " The result is computed in a way which is accurate for x near zero.\n", | |
| " \n", | |
| " log2(...)\n", | |
| " log2(x)\n", | |
| " \n", | |
| " Return the base 2 logarithm of x.\n", | |
| " \n", | |
| " modf(...)\n", | |
| " modf(x)\n", | |
| " \n", | |
| " Return the fractional and integer parts of x. Both results carry the sign\n", | |
| " of x and are floats.\n", | |
| " \n", | |
| " pow(...)\n", | |
| " pow(x, y)\n", | |
| " \n", | |
| " Return x**y (x to the power of y).\n", | |
| " \n", | |
| " radians(...)\n", | |
| " radians(x)\n", | |
| " \n", | |
| " Convert angle x from degrees to radians.\n", | |
| " \n", | |
| " sin(...)\n", | |
| " sin(x)\n", | |
| " \n", | |
| " Return the sine of x (measured in radians).\n", | |
| " \n", | |
| " sinh(...)\n", | |
| " sinh(x)\n", | |
| " \n", | |
| " Return the hyperbolic sine of x.\n", | |
| " \n", | |
| " sqrt(...)\n", | |
| " sqrt(x)\n", | |
| " \n", | |
| " Return the square root of x.\n", | |
| " \n", | |
| " tan(...)\n", | |
| " tan(x)\n", | |
| " \n", | |
| " Return the tangent of x (measured in radians).\n", | |
| " \n", | |
| " tanh(...)\n", | |
| " tanh(x)\n", | |
| " \n", | |
| " Return the hyperbolic tangent of x.\n", | |
| " \n", | |
| " trunc(...)\n", | |
| " trunc(x:Real) -> Integral\n", | |
| " \n", | |
| " Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.\n", | |
| "\n", | |
| "DATA\n", | |
| " e = 2.718281828459045\n", | |
| " inf = inf\n", | |
| " nan = nan\n", | |
| " pi = 3.141592653589793\n", | |
| "\n", | |
| "FILE\n", | |
| " /usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload/math.cpython-35m-darwin.so\n", | |
| "\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "help(math)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Writing modules\n", | |
| "\n", | |
| "Writing Python modules is very simple. To create a module of your own, simply create a new .py file with the module name, and then import it using the Python file name (without the .py extension) using the import command." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "### Writing packages\n", | |
| "\n", | |
| "Packages are namespaces which contain multiple packages and modules themselves. They are simply directories, but with a twist.\n", | |
| "\n", | |
| "Each package in Python is a directory which MUST contain a special file called __init__.py. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported.\n", | |
| "\n", | |
| "If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar. We also must not forget to add the __init__.py file inside the foo directory." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "#### To use the module bar, we can import it in two ways:\n", | |
| "\n", | |
| "```python\n", | |
| "import foo.bar\n", | |
| "```\n", | |
| "\n", | |
| "or\n", | |
| "\n", | |
| "```python\n", | |
| "from foo import bar\n", | |
| "```" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "source": [ | |
| "In the first method, we must use the foo prefix whenever we access the module bar. In the second method, we don't, because we import the module to our module's namespace.\n", | |
| "\n", | |
| "The __init__.py file can also decide which modules the package exports as the API, while keeping other modules internal, by overriding the __all__ variable, like so:\n", | |
| "\n", | |
| "```python\n", | |
| "# __init__.py\n", | |
| "\n", | |
| "__all__ = [\"bar\"]\n", | |
| "```" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Generators" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "source": [ | |
| "Generators are very easy to implement, but a bit difficult to understand.\n", | |
| "\n", | |
| "Generators are used to create iterators, but with a different approach. Generators are simple functions which return an iterable set of items, one at a time, in a special way.\n", | |
| "\n", | |
| "When an iteration over a set of item starts using the for statement, the generator is run. Once the generator's function code reaches a \"yield\" statement, the generator yields its execution back to the for loop, returning a new value from the set. The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "Here is a simple example of a generator function which returns 7 random integers:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 85, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "And the next number is... 22!\n", | |
| "And the next number is... 18!\n", | |
| "And the next number is... 3!\n", | |
| "And the next number is... 40!\n", | |
| "And the next number is... 28!\n", | |
| "And the next number is... 9!\n", | |
| "And the next number is... 15!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import random\n", | |
| "\n", | |
| "def lottery():\n", | |
| " # returns 6 numbers between 1 and 40\n", | |
| " for i in range(6):\n", | |
| " yield random.randint(1, 40)\n", | |
| "\n", | |
| " # returns a 7th number between 1 and 15\n", | |
| " yield random.randint(1,15)\n", | |
| "\n", | |
| "for random_number in lottery():\n", | |
| " print(\"And the next number is... %d!\" % random_number)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# List Comprehensions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "source": [ | |
| "List Comprehensions is a very powerful tool, which creates a new list based on another list, in a single, readable line.\n", | |
| "\n", | |
| "For example, let's say we need to create a list of integers which specify the length of each word in a certain sentence, but only if the word is not the word \"the\".\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 86, | |
| "metadata": { | |
| "collapsed": true, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "sentence = \"the quick brown fox jumps over the lazy dog\"\n", | |
| "words = sentence.split()\n", | |
| "word_lengths = []\n", | |
| "for word in words:\n", | |
| " if word != \"the\":\n", | |
| " word_lengths.append(len(word))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "source": [ | |
| "Using a list comprehension, we could simplify this process to this notation:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 87, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[5, 5, 3, 5, 4, 4, 3]" | |
| ] | |
| }, | |
| "execution_count": 87, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sentence = \"the quick brown fox jumps over the lazy dog\"\n", | |
| "words = sentence.split()\n", | |
| "word_lengths = [len(word) for word in words if word != \"the\"]\n", | |
| "word_lengths" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Function, Multiple Arguments, \\* and \\*\\*" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "Every function in Python receives a predefined number of arguments, if declared normally, like this:\n", | |
| "\n", | |
| "```python\n", | |
| "def myfunction(first, second, third):\n", | |
| " # do something with the 3 variables\n", | |
| " ...\n", | |
| "```" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "It is possible to declare functions which receive a variable number of arguments, using the following syntax:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 88, | |
| "metadata": { | |
| "collapsed": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "First: 1\n", | |
| "Second: 2\n", | |
| "Third: 3\n", | |
| "And all the rest... [4, 5]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "def foo(first, second, third, *therest):\n", | |
| " print(\"First: %s\" % first)\n", | |
| " print(\"Second: %s\" % second)\n", | |
| " print(\"Third: %s\" % third)\n", | |
| " print(\"And all the rest... %s\" % list(therest))\n", | |
| " \n", | |
| "foo(1,2,3,4,5)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 89, | |
| "metadata": { | |
| "collapsed": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "The sum is: 6\n", | |
| "Result: 1\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "def bar(first, second, third, **options):\n", | |
| " if options.get(\"action\") == \"sum\":\n", | |
| " print(\"The sum is: %d\" % (first + second + third))\n", | |
| "\n", | |
| " if options.get(\"number\") == \"first\":\n", | |
| " return first\n", | |
| "\n", | |
| "result = bar(1, 2, 3, action = \"sum\", number = \"first\")\n", | |
| "print(\"Result: %d\" % result)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Exception Handling" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "When programming, errors happen. It's just a fact of life. Perhaps the user gave bad input. Maybe a network resource was unavailable. Maybe the program ran out of memory. Or the programmer may have even made a mistake!\n", | |
| "\n", | |
| "Python's solution to errors are exceptions. You might have seen an exception before." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 91, | |
| "metadata": { | |
| "collapsed": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "hello\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(a)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "source": [ | |
| "Oops! Forgot to assign a value to the 'a' variable." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "But sometimes you don't want exceptions to completely stop the program. You might want to do something special when an exception is raised. This is done in a try/except block.\n", | |
| "\n", | |
| "Here's a trivial example: Suppose you're iterating over a list. You need to iterate over 20 numbers, but the list is made from user input, and might not have 20 numbers in it. After you reach the end of the list, you just want the rest of the numbers to be interpreted as a 0. Here's how you could do that:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 98, | |
| "metadata": { | |
| "collapsed": false, | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "def do_stuff_with_number(n):\n", | |
| " print(n, end=\", \")\n", | |
| "\n", | |
| "the_list = (1, 2, 3, 4, 5)\n", | |
| "\n", | |
| "for i in range(20):\n", | |
| " try:\n", | |
| " do_stuff_with_number(the_list[i])\n", | |
| " except IndexError: # Raised when accessing a non-existing index of a list\n", | |
| " do_stuff_with_number(0)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Serialization" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "Python provides built-in JSON libraries to encode and decode JSON.\n", | |
| "\n", | |
| "In order to use the json module, it must first be imported:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 93, | |
| "metadata": { | |
| "collapsed": true, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import json" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 94, | |
| "metadata": { | |
| "collapsed": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'email': 'john.d@exmaple.com', 'contacts': ['+214 12513 25135', '+141 14rf4 22452'], 'name': {'last': 'Doe', 'first': 'Jone'}}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "json_string = \"\"\"\n", | |
| "{\n", | |
| " \"name\": {\n", | |
| " \"first\": \"Jone\",\n", | |
| " \"last\": \"Doe\"\n", | |
| " },\n", | |
| " \"contacts\": [\n", | |
| " \"+214 12513 25135\",\n", | |
| " \"+141 14rf4 22452\"\n", | |
| " ],\n", | |
| " \"email\": \"john.d@exmaple.com\"\n", | |
| "}\n", | |
| "\"\"\"\n", | |
| "print(json.loads(json_string))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 95, | |
| "metadata": { | |
| "collapsed": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'[1, 2, 3, \"a\", \"b\", \"c\"]'" | |
| ] | |
| }, | |
| "execution_count": 95, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "json.dumps([1, 2, 3, \"a\", \"b\", \"c\"])\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# Decorators" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "Decorators allow you to make simple modifications to callable objects like functions, methods, or classes. We shall deal with functions for this tutorial. The syntax\n", | |
| "\n", | |
| "```python\n", | |
| "@decorator\n", | |
| "def functions(arg):\n", | |
| " pass\n", | |
| "```\n", | |
| "\n", | |
| "Is equivalent to:\n", | |
| "\n", | |
| "```python\n", | |
| "def function(arg):\n", | |
| " pass\n", | |
| "function=decorator(function)\n", | |
| "```" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 96, | |
| "metadata": { | |
| "collapsed": true, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def repeater(old_function):\n", | |
| " def new_function(*args, **kwds): \n", | |
| " old_function(*args, **kwds)\n", | |
| " old_function(*args, **kwds)\n", | |
| " return new_function # return pointer to the new function " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 97, | |
| "metadata": { | |
| "collapsed": false, | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "6\n", | |
| "6\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "@repeater\n", | |
| "def Multiply(num1, num2):\n", | |
| " print(num1*num2)\n", | |
| " \n", | |
| "Multiply(2, 3)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "subslide" | |
| } | |
| }, | |
| "source": [ | |
| "You can also make it change the output" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Code Introspection" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Code introspection is the ability to examine classes, functions and keywords to know what they are, what they do and what they know.\n", | |
| "\n", | |
| "Python provides several functions and utilities for code introspection.\n", | |
| "\n", | |
| "```python\n", | |
| "help()\n", | |
| "dir()\n", | |
| "hasattr()\n", | |
| "id()\n", | |
| "type()\n", | |
| "repr()\n", | |
| "callable()\n", | |
| "issubclass()\n", | |
| "isinstance()\n", | |
| "__doc__\n", | |
| "__name__\n", | |
| "```" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "# What Next !" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "source": [ | |
| "this was based on [learnpython.org](http://www.learnpython.org)\n", | |
| "\n", | |
| "Other Tutorial:\n", | |
| "\n", | |
| "- [learn python the hard way](http://learnpythonthehardway.org/book/index.html)\n", | |
| "- Interactive Tutorial using Unittesting [python koans](https://github.com/gregmalcolm/python_koans)\n", | |
| "- exercises usnig Unittesting [exercism](http://exercism.io)\n", | |
| "- Python.org Trainig Listing [here](https://wiki.python.org/moin/BeginnersGuide/Programmers)\n", | |
| "\n", | |
| "Beyond Basics [The Hitchhiker’s Guide to Python!](http://python-guide.org)\n", | |
| "\n", | |
| "Must Read For Python Developers [pep8.org](https://pep8.org)\n", | |
| "\n", | |
| "List Of Python 3rd Party Modules [awesome python](http://awesome-python.com)\n", | |
| "\n", | |
| "\n", | |
| "- - -\n", | |
| "\n", | |
| "lastly you can contact me at __m@chehab.me__" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "celltoolbar": "Slideshow", | |
| "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.5.1" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment