Skip to content

Instantly share code, notes, and snippets.

@sarguido
Last active January 4, 2016 02:09
Show Gist options
  • Save sarguido/8553438 to your computer and use it in GitHub Desktop.
Save sarguido/8553438 to your computer and use it in GitHub Desktop.
A2PyLadies Intro to Python Workshop, Part 1
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Intro to Python - Part 1\n",
"\n",
"Welcome to the Ann Arbor PyLadies/Girl Develop It Detroit Intro to Python class! You're going to learn the basics of Python programming while also learning to build a simple Hangman game. It'll be a fun way to show what you've learned.\n",
"\n",
"You can access this page at any time, and I'll distribute the link in class. This is part 1 of 2. For the scripts used in class, you can go to https://github.com/A2PyLadies/a2pyladies-pythonintro/tree/master/scripts to check them out and/or download them.\n",
"\n",
"This workshop is adapted from the materials at https://github.com/calebsmith/gdi-intro-python.\n",
"\n",
"## About this workshop\n",
"\n",
"The number one priority is that you have a good time learning Python! If you have questions at any time, please don't hesitate to raise your hand. We'll have several people walking around the room to help you out if you need it. This page will help you follow along.\n",
"\n",
"## About programming\n",
"\n",
"What is programming? Programming is teaching the computer to do a task. A program is made up of lines of code in one or more files, each of which solve part of the overall task. A programming language like Python, which is very readable by humans, has to be translated into a form that the machine can understand. Python uses an interpreter to do the translating.\n",
"\n",
"## About Python\n",
"\n",
"Python is a powerful programming language that's good for beginners to learn because of its readable, clean syntax. It can be used for a variety of things like data analysis, web development, software, and game development. One advantage of using Python is that it's easy to develop things quickly. There are two versions of Python: 2.7.x and 3.3.x. For the purposes of this workshop, we'll use Python 2.7.\n",
"\n",
"## Installing Python and a text editor\n",
"\n",
"Let's set up our computer for Python programming! The first thing you'll need it Python itself. If you use Mac OS X or Linux, you're in luck: Python comes with those operating systems, so you can skip this step. \n",
"\n",
"To install on Windows, go here: http://www.python.org/download/releases/2.7.6/ You'll most likely want to install either this one: **Windows x86 MSI Installer (2.7.6) (sig)** or this one: **Windows x86 MSI program database (2.7.6) (sig)**. Then, after installing Python, open the \"Powershell\" program. Windows PowerShell can be opened by searching for \"Windows Powershell\" in the start menu. In PowerShell, type:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[Environment]::SetEnvironmentVariable(\"Path\", \"$env:Path;C:\\Python27\", \"User\")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once you've installed Python, close the Power Shell.\n",
"Open/Re-Open the terminal/power shell and type in \"python\". The interactive Python shell should start. If it doesn't, please raise your hand and someone will come around to help you.\n",
"\n",
"The next thing you'll want to install is a text editor. If you already have a text editor installed, like TextWrangler or Notepad++, feel free to skip this step. I highly recommend Sublime Text 2: http://www.sublimetext.com/ \n",
"\n",
"## Start with interactive shell\n",
"\n",
"Let's go back to the interactive shell you started in the terminal/power shell. (Remember, you can access it by simply typing \"python\".) We're going to play around with some basic operations, just to get a feel for what Python is like.\n",
"\n",
"### Math\n",
"\n",
"Try typing in the following and hitting 'enter', and see what happens:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"5 + 7"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"12"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"3 - 4"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"-1"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"10 / 2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"5"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"8 * 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"24"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's really easy to do mathematical operations in Python. Feel free to explore and type in other operations here.\n",
"\n",
"### Variables\n",
"\n",
"Variables are the names of objects that store something. Try typing the following:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = 10\n",
"a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"10"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, when you hit 'enter' after typing the letter 'a', 10 is printed out. That's because by assigning 'a' the value of '10', you're storing the value of '10' inside of 'a'. You can do many things with variables:\n",
"\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a + 5"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"15"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"b = 20\n",
"a + b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"30"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c = a * b\n",
"c"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"200"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Types\n",
"\n",
"Every object in Python has what we call a \"type\". The three most common types that we're going to look at (for now) are *int*, *float*, and *string*.\n",
"\n",
"- *Int*: we've already encountered this type! The numbers and the variables from the previous examples are all *ints*. *Int* is short for integer. Any number you type in without any decimals (e.x. 10, 4, 6, 3453453453) are all *ints*.\n",
"- *Float*: a *float* is a number that is a decimal. So, a *float* could be 56.8, 0.1, and so on. *Float* is short for floating point decimal, or floating point real number.\n",
"\n",
"At this point, you might be wondering how *ints* and *floats* are different. What happens when you try each of the following operations?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"4 / 5"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"0"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"4.0 / 5.0"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"0.8"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For *ints*, the operation is rounded down to 0. However, for *floats*, the result is a decimal. \n",
"\n",
"- *String*: Strings are most simply thought of as text. To make a string, you have to put single quotes or double quotes around your variable object. Here are some strings:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"py = 'python'\n",
"py"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"'python'"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num = '10'\n",
"num"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"'10'"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you ever get confused about the type of an object, there's an easy way to check with the 'type' function (we'll get more into what functions are later):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"one = 1\n",
"two = 2.0\n",
"three = 'three'\n",
"four = '4'\n",
"type(one), type(two), type(three), type(four)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"(int, float, str, str)"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that variable four is a string, even though it's a number, because it's surrounded by single quotes.\n",
"\n",
"### Boolean expressions\n",
"\n",
"We can compare variables with what are called boolean expressions. These check for things like equality, non-equality, greater than, and less than. When we check for equality, we use two equal signs, '==', because just one '=' means variable assignment. Here are some of the different boolean expressions:\n",
"\n",
"- == is equal to\n",
"- != is not equal to\n",
"- > is greater than, >= is greater than or is equal to\n",
"- < is less than, <= is less than or equal to\n",
"\n",
"Try out the following:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ten = 5 + 5\n",
"ten == 10"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": [
"True"
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"four != 4"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
"True"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"90 < 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
"False"
]
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"1 >= one"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": [
"True"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### raw_input\n",
"\n",
"A fun thing you can do, as well as something we'll need for our hangman game, is take input from the command line and store it in a variable. We'll do that with a function called 'raw_input'. Type out the following in the shell:\n",
"\n",
"- name = raw_input('What is your name? ')\n",
"- (Here, type in your name)\n",
"- name\n",
"\n",
"Your output should look something like this:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"name = raw_input('What is your name? ')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"What is your name? Sarah\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"name"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": [
"'Sarah'"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Moving to the text editor\n",
"\n",
"Now that we've gotten a bit of a feel for Python syntax, we're going to start writing programs using the text editor we installed. First, though, let's get out of the interactive shell. To exit the shell, simply type"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"quit()"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and hit enter. And that's it! Let's set up our text editor.\n",
"\n",
"You should have followed the installation instructions for Sublime Text 2 (or whatever text editor you've chosen to use). If you haven't, please do that now, or if you need help, raise your hand. Once you've gotten it installed, go ahead and open it up. \n",
"\n",
"Let's create a new file. Go to File > New File. Let's also save it as a Python file. Go to File > Save As... and type in 'script.py'. Next, save it to your Desktop. Keep the file open, since we'll be coming back to it in a second.\n",
"\n",
"Back in the command line, let's navigate to the Desktop as well. Typing 'cd Desktop' and pressing enter should work, but if it doesn't, please raise your hand.\n",
"\n",
"Now that you're in the Desktop, let's go back to our script.py file. On the first line, type"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print 'Hello, world!'"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and save the file. Next, go back to the command line, and type python script.py. 'Hello, world!' should be printed out."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You've just run a program! By typing in 'python script.py', the shell knows to run the file as a Python file, through the Python interpreter. All we're doing is printing a short sentence to the console.\n",
"\n",
"## End of part 1\n",
"\n",
"This concludes part 1 of the workshop. We'll take a break now. If you have any questions, please see me or any of the other workshop leaders."
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment