Philadelphia Python Workshop/Projects: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Geography76
No edit summary
imported>Maneesha
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Welcome to the Saturday afternoon projects section of the Philadelphia Python Workshop! During our first project session from 1-2pm, we'll work through a WordPlay exercise to practice manipulating and matching strings. During our second project session, we'll use the Twitter API to write the basic parts of a Twitter client. You also have the option of reviewing basic concepts instead of participating in the Twitter project.
Welcome to the Saturday afternoon projects section of the Philadelphia Python Workshop!



==Setup==
==Setup==


See the [[Philadelphia Python Workshop/Setup#Goal_.236:_get_dependencies_installed_for_the_Saturday_projects|Friday setup instructions]].
See the [[Philadelphia Python Workshop/Setup#Goal_.236:_get_dependencies_installed_for_the_Saturday_projects|Friday setup instructions]].



==Wordplay==
==Wordplay==


[[File:Crossword.png|100px]]
[[File:Crossword.png|100px]]

===Test the Wordplay code===

Start a command prompt and navigate to the Desktop\Wordplay directory where the Wordplay code lives. For example, if the Wordplay project is at <code>C:\Users\jesstess\Desktop\Wordplay</code>,

<pre>
cd C:\Users\jesstess\Desktop\Wordplay
</pre>

will change you into that directory, and

<pre>
dir
</pre>

will show you the source code files in that directory. One of the files is "words1.py", which has a ".py" extension indicating that it is a Python script. Type:

<pre>
python words1.py
</pre>

at the command prompt to execute the words1.py Python script. You should see a column of English words printed to the screen. If you don't, let a staff member know.

===Success!===

You've completed setup for the Wordplay project.


===Wordplay goals===
===Wordplay goals===
Line 269: Line 245:




==Twitter==
==ColorWall==

<table>
<tr>
<td>
[[File:Twitter.png|100px]]<br />
<center><b>Twitter</b></center>
</td>
<td>
Use the Twitter API to write the basic parts of a Twitter client. See what your friends are tweeting, get trending topics, search tweets, and more.
</td>
</tr>
</table>
===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====

<code>pass</code> 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.

===Python Twitter library functions we will use===

A library provides a collection of functions for you, and defines a contract for using those functions. Here are the functions in the python-twitter library that will be useful for the project:

<code>api.GetSearch(searchString)</code>

Given a string to search for, this function will return a list of tweets matching that search string.

<code>api.GetUserTimeline(username)</code>


[[File:Colorwall_matrix.png|200px]]
Given a username, this function will return a list of tweets belonging to that username.


===ColorWall goals===
===Python Twitter Project Instructions===


*Learn about importing modules
Here are the [[Philadelphia Python Workshop/Projects/Twitter|Twitter Project Instructions]].
*Basic introduction to classes

Latest revision as of 16:40, 31 July 2014

Welcome to the Saturday afternoon projects section of the Philadelphia Python Workshop!


Setup

See the Friday setup instructions.


Wordplay

Wordplay goals

  • practice for loops
  • practice using lists
  • practice manipulating strings
  • get experience with regular expressions
  • have fun cheating at crosswords and Words with Friends

Concept review

Indentation reminder

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

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

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

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

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

Because James is not older than Alice, the if conditional is False, 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:

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

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

range

>>> range(5)
[0, 1, 2, 3, 4]
>>> for i in range(5):
...     print "Hi" * i
...

Hi
HiHi
HiHiHi
HiHiHiHi

if statements inside for loops

>>> for i in range(80):
...     if i % 9 == 0:
...         print i, "is divisible by 9."
...
0 is divisible by 9.
9 is divisible by 9.
18 is divisible by 9.
27 is divisible by 9.
36 is divisible by 9.
45 is divisible by 9.
54 is divisible by 9.
63 is divisible by 9.
72 is divisible by 9.

for loops inside for loops

>>> letters = ["a", "b", "c"]
>>> numbers = [1, 2, 3]
>>> for letter in letters:
...     for number in numbers:
...         print letter * number
...
a
aa
aaa
b
bb
bbb
c
cc
ccc
>>> for number in numbers:
...     for letter in letters:
...         print number * letter
...
a
b
c
aa
bb
cc
aaa
bbb
ccc

Imports

Imports look like this:

>>> import random
>>> import time

In the above example, random and time 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 random module:

>>> 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

randint is a function in the random 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

in keyword

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

>>> "a" in "apple"
True
>>> "z" in "apple"
False
>>> dogs = ["pug", "boxer", "dalmation"]
>>> "tiger" in dogs
False
>>> "pug" in dogs
True

Reversing lists

Here's a quick way to reverse a list.

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

Let's break down why this works:

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

>>> fruits[0]
'apples'

We can also slice lists:

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

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

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

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 -1 to say backwards:

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

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:

  • rstu will match anything that contains "rstu", for example "understudy".
  • ^aa will match anything that starts with "aa", for example "aardvark".
  • a.b.c will match anything containing "a", then any single character, then "b", then any single character, then "c", for example "iambic".
  • ss.*ss will match anything containing "ss", then anything, then "ss", for example "messiness".
  • ^c.d.e$ 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".

Python Wordplay Project Instructions

Here are the Wordplay Project Instructions.


ColorWall

ColorWall goals

  • Learn about importing modules
  • Basic introduction to classes