Community Data Science Workshops (Spring 2014)/Friday April 4th Tutorial: Difference between revisions

turn bold stuff into wikiformatting
imported>Mako
(turned pre tags into real wiki tags)
imported>Mako
(turn bold stuff into wikiformatting)
Line 56:
[[File:Geometry.png|150px]]
 
There's a helpful <b>'''function</b>''' (more on what a function is in a second) called <code>type</code> that tells you what kind of thing -- what <b>'''data type</b>''' -- Python thinks something is. We can check for ourselves that Python considers '1' and '1.0' to be different data types:
 
type(1)
type(1.0)
So now we've seen two data types: <b>'''integers</b>''' and <b>'''floats</b>'''.
 
By the way, what is a "function"? Here are the important ideas about functions:
Line 67:
* A function encapsulates a useful bit of work and gives that work a name.
* You provide input to a function and it produces output. For example, the <code>type</code> function takes data as an input, and produces what type of data the data is (e.g. an integer or a float) as output.
* To use a function, write the name of the function, followed by an open parenthesis, then what the function needs as input (we call that input the <b>'''arguments</b>''' to the function), and then a close parenthesis.
* Programmers have a lot of slang around functions. They'll say that functions "take" arguments, or that they "give" or "pass" arguments to a function. "call" and "invoke" are both synonyms for using a function.
 
Line 78:
===Command history===
 
Stop here and try hitting the Up arrow on your keyboard a few times. The Python <b>'''interpreter</b>''' saves a history of what you've entered, so you can arrow up to old commands and hit Return to re-run them!
 
==Variables==
Line 92:
2 * x
 
Giving a name to something, so that you can refer to it by that name, is called <b>'''assignment</b>'''. Above, we assigned the name 'x' to 4, and after that we can use <code>x</code> wherever we want to use the number 4.
 
Variables can't have spaces or other special characters, and they need to start with a letter. Here are some valid variable names:
Line 161:
[[File:Letter.png|100px]]
 
So far we've seen two data types: <b>'''integers</b>''' and <b>'''floats</b>'''. Another useful data type is a <b>'''string</b>''', which is just what Python calls a bunch of characters (like numbers, letters, whitespace, and punctuation) put together. Strings are indicated by being surrounded by quotes:
 
"Hello"
Line 192:
<code>TypeError: cannot concatenate 'str' and 'int' objects</code>
 
Python is giving us a <b>'''traceback</b>'''. A traceback is details on what was happening when Python encountered an Exception or Error -- something it doesn't know how to handle.
 
There are many kinds of Python errors, with descriptive names to help us humans understand what went wrong. In this case we are getting a <code>TypeError</code>: we tried to do some operation on a data type that isn't supported for that data type.
Line 241:
'I'm a happy camper'
 
This gives us another <b>'''traceback</b>''', for a new kind of error, a <code>SyntaxError</code>. When Python looks at that expression, it sees the string 'I' and then
 
<code>m a happy camper'</code>
Line 289:
== Part 2: Printing==
 
So far we've been learning at the interactive <b>'''Python interpreter</b>'''. When you are working at the interpreter, any work that you do gets printed to the screen. For example:
 
h = "Hello"
Line 297:
will display "HelloWorld".
 
Another place that we will be writing Python code is in a file. When we run Python code from a file instead of interactively, we don't get work printed to the screen for free. We have to tell Python to print the information to the screen. The way we do this is with the <b>'''print</b>''' function. Here's how it works:
 
h = "Hello"
Line 307:
 
 
The string manipulate is exactly the same as before. The only difference is that you need to use <b>'''print</b>''' to print results to the screen:
 
<code>h + w</code>
Line 365:
&nbsp;&nbsp;&nbsp;&nbsp;<code>if 6 > 5:</code><br />
<br />
part, and press Enter. The next line will have <code>...</code> as a prompt, instead of the usual <code>&gt;&gt;&gt;</code>. This is Python telling us that we are in the middle of a <b>'''code block</b>''', and so long as we indent our code it should be a part of this code block.</li>
 
<li>Press the spacebar 4 times to indent.</li>
Line 384:
</pre>
 
What is going on here? When Python encounters the <code>if</code> keyword, it <i>evaluates</i> the <i>expression</i> following the keyword and before the colon. If that expression is <b>'''True</b>''', Python executes the code in the indented code block under the <code>if</code> line. If that expression is <b>'''False</b>''', Python skips over the code block.
 
In this case, because 6 really is greater than 5, Python executes the code block under the if statement, and we see "Six is greater than five!" printed to the screen. Guess what will happen with these other expressions, then type them out and see if your guess was correct:
Line 400:
====more choices: <code>if</code> and <code>else</code>====
 
<b>'''<code>if</code></b>''' lets you execute some code only if a condition is <code>True</code>. What if you want to execute different code if a condition is <code>False</code>?
 
Use the <b>'''<code>else</code></b>''' keyword, together with <code>if</code>, to execute different code when the <code>if</code> condition isn't <code>True</code>. Try this:
 
<pre>
Line 417:
====compound conditionals: <code>and</code> and <code>or</code>====
 
You can check multiple expressions together using the <b>'''<code>and</code></b>''' and <b>'''<code>or</code></b>''' keywords. If two expressions are joined by an <code>and</code>, they <b>'''both</b>''' have to be <code>True</code> for the overall expression to be <code>True</code>. If two expressions are joined by an <code>or</code>, as long as <b>'''at least one</b>''' is <code>True</code>, the overall expression is <code>True</code>.
 
Try typing these out and see what you get:
Line 461:
====even more choices: <code>elif</code>====
 
If you need to execute code conditional based on more than two cases, you can use the <b>'''<code>elif</code></b>''' keyword to check more cases. You can have as many <code>elif</code> cases as you want; Python will go down the code checking each <code>elif</code> until it finds a <code>True</code> condition or reaches the default <code>else</code> block.
 
<pre>
Line 488:
If color had been "purple", that code wouldn't have printed anything.
 
<b>'''Remember that '=' is for assignment and '==' is for comparison.</b>'''
 
====In summary: the structure of if/elif/else====
Line 504:
[[File:Scales.png|100px]]
 
So far, the code we've written has been <i>unconditional</i>: no choice is getting made, and the code is always run. Python has another data type called a <b>'''boolean</b>''' that is helpful for writing code that makes decisions. There are two booleans: <code>True</code> and <code>False</code>.
 
True
Line 522:
Use <code>==</code> to test for equality. Recall that <code>=</code> is used for <i>assignment</i>.
 
This is an important idea and can be a source of bugs until you get used to it: <b>'''= is assignment, == is comparison</b>'''.
 
Use <code>!=</code> to test for inequality:
Anonymous user