Anonymous user
Community Data Science Workshops (Spring 2014)/Friday April 4th Tutorial: Difference between revisions
Community Data Science Workshops (Spring 2014)/Friday April 4th Tutorial (view source)
Revision as of 02:45, 31 March 2014
, 10 years agoturn 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
type(1)
type(1.0)
So now we've seen two data types:
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
* 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
==Variables==
Line 92:
2 * x
Giving a name to something, so that you can refer to it by that name, is called
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:
"Hello"
Line 192:
<code>TypeError: cannot concatenate 'str' and 'int' objects</code>
Python is giving us a
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
<code>m a happy camper'</code>
Line 289:
== Part 2: Printing==
So far we've been learning at the interactive
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
h = "Hello"
Line 307:
The string manipulate is exactly the same as before. The only difference is that you need to use
<code>h + w</code>
Line 365:
<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>>>></code>. This is Python telling us that we are in the middle of a
<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
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>====
Use the
<pre>
Line 417:
====compound conditionals: <code>and</code> and <code>or</code>====
You can check multiple expressions together using the
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
<pre>
Line 488:
If color had been "purple", that code wouldn't have printed anything.
====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
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:
Use <code>!=</code> to test for inequality:
|