{ "metadata": { "kernelspec": { "codemirror_mode": { "name": "ipython", "version": 2 }, "display_name": "IPython (Python 2)", "language": "python", "name": "python2" }, "name": "", "signature": "sha256:ee402650c9c8bdf6b8ce855b70a387d27a8deea6b89f3d23cf71c6a67169f6fb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Introduction to Python Workshop\n", "Part 1" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Welcome again!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to thank the many people that have made this workshop possible.\n", "\n", "First, the generosity of our sponsors have provided facilities for the workshop, food and refreshments, and travel assistance for our guest speakers. Please give a hand to our fabulous sponsors:\n", "- [Ansir Innovation Center](http://ansirsd.com/) Twitter: @AnsirSD\n", "- [Python Software Foundation](https://www.python.org/psf/) Twitter: @ThePSF\n", "\n", "We are committed to offering a positive and productive workshop for you. We are proud to be an [OpenHatch](https://openhatch.org) (@openhatch) affiliated event. OpenHatch is a non-profit that helps people become contributors to free and open source software. OpenHatch is a friendly community and can help you find a suitable project if you are interested in contributing.\n", "\n", "While we are thanking the PSF and OpenHatch, I would like to thank Jessica McKellar, PSF Director and OpenHatch board member (@jessicamckellar), for sharing her materials from her Intro to Python workshop as well as providing encouragement and support to us.\n", "\n", "If anyone wishes to tweet their appreciation, please do so.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A programming community outreach workshop, brought to you by the generous volunteers and leadership from:\n", "\n", "- [PyLadies San Diego](www.meetup.com/sd-pyladies/)\n", "- [San Diego Python User Group](www.meetup.com/pythonsd/)\n", "- [Inland Empire Pyladies](www.meetup.com/iepyladies/)\n", "- [Inland Empire Python](www.meetup.com/iepython/)\n", "\n", "Thanks to David and Kendall of SDPUG, Juliet of PyLadies SD, and John of Inland Empire Python for their support." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Introduction of who is here to teach and help you today.**\n", "- Audrey\n", "- Danny\n", "- Rise\n", "- Trey\n", "- Alain\n", "- Micah\n", "- Jim\n", "- Others that are helping on day of event\n", "- Carol\n", "\n", "Please take a moment to share 2-4 sentences about yourself.\n", "\n", "We are all volunteers. If you enjoy this workshop and decide to continue with Python programming, we encourage you to volunteer at the next Intro to Python Workshop.\n", "\n", "We are also very thankful that you have chosen to spend the better part of your Saturday sharing Python with us. It's a language that we have fun using to make all sorts of projects and a thoughtful, accepting, considerate, and fun community that we are glad to take part in.\n", "\n", "And now...let's get going with some Python development!\n", " " ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "The Game Plan" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Did you do your Python setup homework?" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Recap: Setting Up Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should have completed the Python 3.4 setup instructions on your own:\n", "http://nbviewer.ipython.org/github/pythonsd/intro-to-python/blob/master/part-0.ipynb\n", "\n", "If not, please do it now." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Stuck? Raise Your Hand / Place Yellow Sticky Note" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tried but ran into errors? Raise your hand or place a yellow sticky note on your screen to show that you need help. A volunteer will come over to help you.\n", "\n", "Don't be shy. Setup problems are common. It's particularly important for everyone who's stuck to get help right now before we move on, to ensure that you get the most out of this workshop." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Done With Setup?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Volunteers will be walking around to check everyone's setup. Have your setup checked.\n", "\n", "When a volunteer comes over, open up a command prompt and type this, to show that you are ready:\n", "\n", "```\n", "$ python3 --version\n", "Python 3.4.1\n", "```\n", "\n", "Once you've had your setup checked:\n", "* While waiting, introduce yourself to at least 3 of the people sitting near you. Tell them your name, something about yourself, and why you decided to come to today's workshop." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Setup Complete!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After completing these steps, you should:\n", "* Have Python installed\n", "* Be able to enter and exit Python\n", "\n" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Let's get started with the interactive lecture" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Python as a Calculator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From your command prompt, type `python3` to enter IDLE.\n", "\n", "Some regular math operations." ] }, { "cell_type": "code", "collapsed": false, "input": [ "2 + 2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "4" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "1.4 + 2.25" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "3.65" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "4 - 2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "2" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "2 * 3" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "6" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "4 / 2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "2" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "0.5/2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "0.25" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Special type of division (floor)**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "3 // 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "15.5 // 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Horizontal line spacing does not matter in Python in an individual statement. In a multiline program it does, you typically indent 4 spaces." ] }, { "cell_type": "code", "collapsed": false, "input": [ "2 + 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "2+2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Parens and order of operations follow typical mathematics conventions in Python. `_` in IPython signifies the previous result." ] }, { "cell_type": "code", "collapsed": false, "input": [ "(1 + 3) * 4" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 4" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x * 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "_ + 8" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**A trip to PyCon**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "jeans = 5" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "shoes = 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "socks = 12" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "shirts = 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "items_packed = jeans + shoes + socks + shirts" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "items_packed" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(items_packed)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Using type() to find the datatype**\n", "\n", "\n", "Let's use a function. You'll be hearing more about functions later today. For now let's say that a function is like a Personal Assistant. You ask for a job to be done, and if you give the assistant the correct instructions, he will do the tasks asked.\n", "\n", "One handy thing that our Python Personal Assistant, aka Funky Function, can do is tell us a variable's current data type." ] }, { "cell_type": "code", "collapsed": false, "input": [ "type(shirts)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(0.99)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Questions?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**String data type**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Hello\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Python, I'm your #1 fan!\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(\"Hello\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Concatenating strings**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = \"Carol\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "2 + 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Carol\" + \"Willing\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\"Carol \" + \"Willing\"\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Carol\" + \" \" + \"Willing\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "name = \"Carol\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"My name is \" + name" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Tip - arrow up to save time typing*" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Hello\" + 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Hello\" + \"1\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(\"1\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Hello\" + str(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "len(\"Hello\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "len(name)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"The length of my name is \" + str(len(name))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Hello\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "'Hello'\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Quotes (single, double, triple)**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Python, I'm your #1 fan!\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"A\" * 40" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "h = \"Happy\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "b = \"Birthday\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "(h + b) * 10" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Displaying versus printing in IPython**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Hello\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Questions? Quick review" ] }, { "cell_type": "code", "collapsed": false, "input": [ "3 ** 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(1.0)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(\"1\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Make choices" ] }, { "cell_type": "code", "collapsed": false, "input": [ "True" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "False" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(False)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "0 == 0" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "0 == 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "0 != 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"a\" == \"A\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "1 > 0" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "2 >= 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "-1<0" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ ".5 <= 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"H\" in \"Hello\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"x\" in \"Hello\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"a\" not in \"abcde\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(\"True\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(true)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 4" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x == 4" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "if 6 > 5:\n", " print(\"Six is greater than 5\")\n", " " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "if 0 > 2:\n", " print(\"Zero is greater\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "if \"banana\" in \"bananarama\":\n", " print(\"I miss the 80s\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of two actions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sister = 15" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "brother = 12" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "if sister > brother:\n", " print(\"Sister is older\")\n", "else:\n", " print(\"Brother is older\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "1 > 0 and 1 < 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "1 < 2 and \"x\" in \"abc\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"a\" in \"abc\" and \"b\" in \"abc\" and \"c\" in \"abc\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"a\" in \"hello\" or \"e\" in \"hello\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "temp = 32" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "if temp > 60 and temp < 75:\n", " print(\"Nice and cozy\")\n", "else:\n", " print(\"Too extreme for me\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of three things" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sister = 15\n", "brother = 15\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "if sister > brother:\n", " print(\"Sister is older\")\n", "elif sister == brother:\n", " print(\"Twinsies!!\")\n", "else:\n", " print(\"Brother is older\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exiting the interpreter is a good thing to know.** \n", "To exit, type `exit()` or press CNTL-D." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Practice problems - Codeacademy practice\n", "Strings and choices\n", "http://bit.ly/py-practice" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import YouTubeVideo\n", "# a tutorial about Python at PyCon 2014 in Montreal, Canada by Jessica McKellar\n", "# Credit: William Stein.\n", "YouTubeVideo('MirG-vJOg04')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import IFrame\n", "# Pull in the tutorial prep information from OpenHatch wiki\n", "IFrame('http://bit.ly/intro-setup', width='100%', height=350)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }