Boston Python Workshop 4/Twitter handout: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
(Created page with '===Twitter goals=== * practice for loops * practice using functions * practice implementing functions * see what it's like to use an API * have fun collecting data from Twitter …')
 
(vFGrzpLhrxeOKPrtjSt)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
There's nothing like the reilef of finding what you're looking for.
===Twitter goals===

* practice for loops
* practice using functions
* practice implementing functions
* see what it's like to use an API
* have fun collecting data from Twitter

===Concept review===

====Indentation reminder====

In Python, indentation matters. Everything is indented by a multiple
of some number of spaces, often 4.

In <code>if</code> statements, you indent everything you want to be run if the if
conditional is <code>True</code>. For example:

<pre>
>>> James = 35
>>> Alice = 30
>>> if James > Alice:
... print "James is older than Alice."
...
James is older than Alice.
>>></pre>

Because James really is older than Alice, the <code>if</code> conditional is <code>True</code>,
so Python does execute the code indented under the if line. In this
case we print "James is older than Alice."

<pre>
>>> James = 35
>>> Alice = 30
>>> if James < Alice:
... print "James is younger than Alice."
...
>>></pre>

Because James is not older than Alice, the <code>if</code> conditional is <code>False</code>, so
Python does not execute the code indented under the if line.

In for loops, you indent everything you want to be run each loop For
example:

<pre>
>>> names = ["Jessica", "Adam", "Liz"]
>>> for name in names:
... print "Hello", name
...
Hello Jessica
Hello Adam
Hello Liz</pre>

The <code>print</code> line is indented 4 spaces under the <code>for</code>. That's how
Python knows to execute the print line for every name in names.

====Functions====

Let's review what we know about functions:

* They do some useful bit of work.
* They let us re-use code without having to type it out each time.
* They take input and possibly produce output (we say they return a value). You can assign a variable to this output.
* You define a function using the def keyword.
* You call a function by using its name followed by its arguments in parenthesis.

Here's an example:

<pre>
>>> def add(x, y):
... return x + y
...
>>> add(1, 2)
3
>>> add(-1, 1)
0
>>> add(.5, .75)
1.25</pre>

====Pass====

pass is a keyword that just means "do nothing". It most often shows up
as a place-holder for code that doesn't exist yet. For example:

<pre>
>>> def testFunction():
... pass
...
>>></pre>

====Imports====

Imports look like this:

<pre>
>>> import random
>>> import time</pre>

In the above example, <code>random</code> and <code>time</code> are both Python modules. Modules
are Python files outside of the current Python file that contain
Python code, like functions and variables. You can use code from
modules by first importing the module. Here's an example from the
<code>random</code> module:

<pre>
>>> import random
>>> random.randint(0, 10)
7
>>> random.randint(0, 10)
6
>>> random.randint(0, 10)
1
>>> random.randint(0, 10)
3
>>> random.randint(0, 10)
4
>>> random.randint(0, 10)
9</pre>

<code>randint</code> is a function in the <code>random</code> module. It takes a lower bound as
the first argument and an upper bound as the second argument and
returns a random integer between those bounds.

===New Wordplay material summary===

====<code>in</code> keyword====

<code>in is a keyword checking for containment. You can use it in a couple
of ways:

<pre>
>>> "a" in "apple"
True
>>> "z" in "apple"
False</pre>

<pre>
>>> dogs = ["pug", "boxer", "dalmation"]
>>> "tiger" in dogs
False
>>> "pug" in dogs
True</pre>

====Reversing lists====

Here's a quick way to reverse a list.

<pre>
>>> fruits = ["apples", "bananas", "cherries"]
>>> fruits[::-1]
['cherries', 'bananas', 'apples']</pre>

Let's break down why this works:

First, remember that we can get individual elements from lists:

<pre>
>>> fruits[0]
'apples'</pre>

We can also slice lists:

<pre>
>>> fruits[0:2]
['apples', 'bananas']
>>> fruits[:2]
['apples', 'bananas']
>>> fruits[1:]
['bananas', 'cherries']</pre>

We can also make a copy of the list by taking a slice from the
beginning to the end of the list:

<pre>
>>> my_fruits = fruits[:]
['apples', 'bananas', 'cherries']</pre>

There's an extended slicing syntax that let's you say what direction
you want to slice in. By default it is forward, but you can supply a
<code>-1</code> to say backwards:

<pre>
>>> fruits[::-1]
['cherries', 'bananas', 'apples']</pre>

====Regular expressions====

* ^ is an anchor that means "beginning"
* $ is an anchor that means "end"
* . means a single wildcard character
* .* means any number (including zero) of wildcard characters

Examples:

* <code>stuv</code> will match anything that contains "rstu", for example "understudy".
* <code>^aa</code> will match anything that starts with "aa", for example "aardvark".
* <code>a.b.c</code> will match anything containing "a", then any single character, then "b", then any single character, then "c", for example "iambic".
* <code>ss.*ss</code> will match anything containing "ss", then anything, then "ss", for example "messiness".
* <code>^c.d.e$</code> will match anything that starts with "c", then any single character, then d, then any single character, then ends with an "e", for example "cadre".

Latest revision as of 20:14, 15 January 2012

There's nothing like the reilef of finding what you're looking for.