{ "metadata": { "celltoolbar": "Slideshow", "name": "", "signature": "sha256:d43a8f5391bde0382c044b463298a27d3c23c084a17751ad8d6814c571a54575" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Intro to Python" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "#Presented by Chris (@0xCMP)\n", "##FALL 2014 - Programming Languages" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#Python\n", ">Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C. The language provides constructs intended to enable clear programs on both a small and large scale.[20]\n", ">\n", ">Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.\n", ">\n", ">-- [**Wikipedia on Python**](https://en.wikipedia.org/wiki/Python_(programming_language)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#Overview of Python\n", "Scripting language created by Guido van Rossum\n", "\n", "Published in 1991 (23 years ago)\n", "\n", "- Applications in:\n", " * **Web programming**\n", " * *Flask, Web.py, Django*\n", " * **Simulation and Scientific applications**\n", " * *SymPy, NumPy, Matplotlib*\n", " * **Systems programming**\n", " * *systemd*\n", " * **Desktop GUI programming**\n", " * *Tk, QT, GTK*\n", " * **Game programming**\n", " * *pygame*\n", " * **Sever scripting**\n", " * **Plugin language**\n", " * **Security tools**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#Who uses Python? For what?\n", "Most places these days we know about use it for web programming.\n", "\n", "* [Dropbox]\n", " * Desktop app is written in python\n", " * Backend was python, now migrating to **Go**\n", " * Employs Guido currently (creator of python)\n", "* [Google]\n", " * Used python since beginining\n", " * One of their 3 \"Offical Languages\" (C++, Java, Python)\n", " * Languages allowed for production\n", " * Employed Guido for a time (2005-2012)\n", "* [NASA]\n", " * Some use it for data analysis in place of Matlab\n", "* Quora\n", " * Website written in Python using Django Web Framework\n", " * Also uses Cython for speed\n", "* CCP (Eve Online)\n", " * Uses stackless python for their server and clients\n", " * http://community.eveonline.com/news/dev-blogs/stackless-python-2.7/\n", " * [rfp-ep8-mempressure.mp3](./rfp-ep8-mempressure.mp3)\n", "\n", "[Many more at Python's website.](https://www.python.org/about/success/)\n", "\n", "[Dropbox]: https://en.wikipedia.org/wiki/Dropbox_(service)#Technology \"Dropbox's technology\"\n", "[Google]: http://stackoverflow.com/questions/2560310/heavy-usage-of-python-at-google \"Google's technology\"\n", "[NASA]: http://www.reddit.com/r/Python/comments/1r1yke/what_do_you_use_python_for/cdj2cwn" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#What is it good at?\n", "**Python is great at getting an application created quickly and cleanly**\n", "\n", "* Single threaded or I/O intensive things\n", "* Works well for many applications thanks to libraries with C extensions\n", "* Not as fast as C/C++, but it is very easy to call C/C++ code\n", "* Also strong as a scripting language for a program\n", " * *Sid Myer's Civilization mods are written in Python because of this.*\n", " * Use of python to script robots at a semiconductor manufacturing facility in Fishkill, NY originally controlled with C++\n", " * https://www.python.org/about/success/philips/" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "#What is it not good at?\n", "* Kernel development\n", "* Drivers\n", "* Crtical real time code\n", "* Embedded and mobile devices\n", "* Highly important code (airbags, audo jack)\n", "\n", "http://www.reddit.com/r/Python/comments/1dsv7m/what_is_python_not_a_good_language_for/" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#How to setup Python\n", "Everyone needs to go to [http://python.org/download]()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#Windows\n", "* Download the latest 2.7 32 bit python. \n", "* Will install to `C:\\Python27`\n", " * easier to go to with `cd \\py`\n", " * keeps seperate from other python versions (like Python 3.4)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#Mac OS X\n", "* You already have Python 2.7 installed.\n", "* Check your version with `python -V` in the terminal\n", "* Download newest version from python.org \n", " * or through homebrew ([brew.sh/](http://brew.sh)) with `brew install python`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#Ubuntu\n", "* You likely have it installed already.\n", "* Install via `apt-get`\n", " 1. Go to a terminal\n", " 1. `sudo apt-get update`\n", " 1. `sudo apt-get install python`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#Note about installing Python 3\n", "##Do not install python 3 for this class. \n", "I will talk about python 3 later and explain it's differences then. **It is actually incompatible with python 2 in important ways.**\n", "\n", "IF, you wish to also install it for mac and linux look for `python3` in `brew` or `apt-get install`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#How to use it\n", "1. open your terminal\n", " * OS X: spotlight search \"Terminal\" or install \"iTerm2\"\n", " * Linux: refer to distro docs\n", " * Windows: use powershell, search in start menu by searching \"powershell\" and pin it to somewhere\n", "2. type `python`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#The interpreter and quick intro\n", "Lets try the following in the REPL (read, evaluate, print loop)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"hello world\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hello world\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "1+1" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "2" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "1/2" ], "language": "python", "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "0" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "> **Notice for later**: Not *true* division" ] }, { "cell_type": "code", "collapsed": false, "input": [ "1//2" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "0" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"hello \" * 4" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hello hello hello hello \n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Real Python...\n", "(for 2nd class)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "##Lists, Dictionaries, and Slicing" ] }, { "cell_type": "code", "collapsed": false, "input": [ "item = [1,2,3,4,5,6]\n", "print item\n", "print type(item)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 4, 5, 6]\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "##Slicing" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print item[3:4]\n", "print item[:4]\n", "print item[3:]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[4]\n", "[1, 2, 3, 4]\n", "[4, 5, 6]\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "myDict = {\"key\":\"value\", \"name\":\"chris\"}" ], "language": "python", "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "print myDict" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'name': 'chris', 'key': 'value'}\n" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"My name is %s\" % myDict['name']" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "My name is chris\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#`if` Statements" ] }, { "cell_type": "code", "collapsed": false, "input": [ "item = \"5\"\n", "if item == 5:\n", " print \"It's 5\"\n", "elif item == \"5\":\n", " print \"It's %s\" % item\n", "else:\n", " print \"I don't know what it is.\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "It's 5\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "item = \"5\"\n", "if item is not None:\n", " print \"item isn't None\"\n", "else:\n", " print \"item is None\"\n", "print \"---\\nSetting item to None\\n---\"\n", "\n", "item = None\n", "\n", "if item is None:\n", " print \"item is None\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "item isn't None\n", "---\n", "Setting item to None\n", "---\n", "item is None\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "##`for` loops" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_list = [1, 2, 3]\n", "my_strings = [\"things\", \"stuff\", \"abc\"]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "for item in my_list:\n", " print \"Item %d\" % item" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Item 1\n", "Item 2\n", "Item 3\n" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "for (index, string) in enumerate(my_strings):\n", " print \"Index of %d and value of %s\" % (index, string)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Index of 0 and value of things\n", "Index of 1 and value of stuff\n", "Index of 2 and value of abc\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "##Types and getting more help" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Type of my_list is %s and the first element is %s\" % (type(my_list), type(my_list[0]))" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Type of my_list is and the first element is \n" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "print dir(my_list)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "##`class`es" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class A_old():\n", " pass\n", "\n", "class A_new(object):\n", " pass\n", "\n", "print \"Old type %s and New type %s\" % (type(A_old), type(A_new))" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Old type and New type \n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "my_old_a = A_old()\n", "my_new_a = A_new()\n", "print \"Old object %s and New object %s\" % (type(my_old_a), type(my_new_a))" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Old object and New object \n" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "##[Equality and Variables](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables)\n", "In Python variables are more like \"tags\" than \"boxes\"\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", "Normal\n", "![](http://python.net/~goodger/projects/pycon/2007/idiomatic/a2box.png)\n", "![](http://python.net/~goodger/projects/pycon/2007/idiomatic/b2box.png)\n", "\n", "Python\n", "![](http://python.net/~goodger/projects/pycon/2007/idiomatic/a2tag.png)\n", "![](http://python.net/~goodger/projects/pycon/2007/idiomatic/ab2tag.png)\n", "
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "item = [1, 2, 3]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "item is item" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "True" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "item is item[:]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "False" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "item == item[:]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "True" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Some Simple Programs" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Question and answer" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import re\n", "\n", "user = {}\n", "user['name'] = raw_input(\"What is your name? \")\n", "user['quest'] = raw_input(\"What is your quest? \")\n", "user['will-get-shrubbery'] = raw_input(\"We want.... ONE SHRUBBERY. \")\n", "user['favorite-color'] = raw_input('What is your favorite colour? ')\n", "\n", "print '-'*60\n", "\n", "accepted = re.compile(r\"^((sure|SURE)|(y|Y).*)\")\n", "accepted_status = \"will acquire\"\n", "\n", "if accepted.search(user['will-get-shrubbery']) is None:\n", " accepted_status = \"will not aquire\"\n", "\n", "print \"%s is on a quest %s and %s a shrubbery. His favorite color is %s\" % (user['name'].title(),\n", " user['quest'].lower(), accepted_status , user['favorite-color'].upper())\n", "\n" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What is your name?King Arthur\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What is your quest?To find the Holy Grail\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "We want.... ONE SHRUBBERY.sure\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What is your favorite colour?blue\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "------------------------------------------------------------\n", "King Arthur is on a quest to find the holy grail and will acquire a shrubbery. His favorite color is BLUE\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Linked List" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#Resources\n", "* [Best Python Resources for Beginners and Professionals **(Highly Recommended)**](http://freepythontips.wordpress.com/2013/09/01/best-python-resources/)\n", "* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)\n", "* [Python 2.7.8 Offical Tutorial](docs.python.org/2/tutorial/index.html)\n", "* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)\n", "* [Code Like a Pythonista: Idiomatic Python](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html)\n", "* [Dive in to Python](http://www.diveintopython.net/toc/index.html)" ] } ], "metadata": {} } ] }