Вы находитесь на странице: 1из 10

Advanced Course in Computer Science

Learning to harness the collective intelligence

Books:

Python
Dive into Python - Mark Pilgrim (http://www.diveintopython.org)
A Byte of Python – Swaroop C S (http://www.ibiblio.org/swaroopch/byteofpython/read/)
Think like a Python programmer – Allen Downey (http://www.greenteapress.com/thinkpython/thinkCSpy/ )

Collective Intelligence
Programming Collective Intelligence, Building Smart Web 2.0 Applications - Toby Segaran

NLTK
Natural Language Processing in Python - Steven Bird, Ewan Klein, and Edward Loper (http://nltk.org/index.php/Book)

Required Reading:

The Cathedral and the Bazaar - Eric Steven Raymond


(http://www.catb.org/~esr/writings/cathedral-bazaar/cathedral-bazaar/)

In the beginning was the command line – Neal Stephenson


(http://artlung.com/smorgasborg/C_R_Y_P_T_O_N_O_M_I_C_O_N.shtml)

Test infected: Programmers love writing tests – Kent Beck


(http://junit.sourceforge.net/doc/testinfected/testing.htm)

Mythical Man Month – Frederick Brooks


(http://en.wikipedia.org/wiki/The_Mythical_Man-Month)

Free as in Freedom – Richard Stallman


(http://oreilly.com/openbook/freedom/)
References:

wikipedia.org
google.com
python.org
nltk.org

Course Group:
http://groups.google.com/group/pesit-2008-course

Books (soft copies)


Introduction to the command line:

Unix or Windows (or Mac!!!)?

Unix:
sh, bash etc
mostly default way to interact with the OS, unless one uses X-Windows

Windows
cmd.exe

Mac
Any mac users?

Exercise:

1. How do we access cmd.exe?

2. Create a shortcut to cmd.exe on the desktop

3. Create a course folder and a user folder under it


Course folder name - “collective_intelligence”
Which drive? Let us use a common drive
Introduction to python:

Installation:

We will use python v 2.5.2

Exercise:

1. Install python (from net or shared folder)


Install file is python-2.5.2.msi
Install to <Common Drive>:\python25

2. Open command line and check if python is available. How do you do this?
The python program is called “python.exe” on windows

3. Add python to PATH (if it is not added to it already). What is the PATH?

4. What happened when you executed python.exe?


Python interpreter:

Python is an interpreted language


compiled language – source code converted translated completely to object code (or executable code) before
execution
interpreted language – source code converted to executable progressively, line by line

So what are the advantages of compiled language vs interpreted and vice versa?

Python interpreter works in two ways:


1. the python shell (remember phtyon.exe from previous exercise?)
2. from a script

Exercise:

1. Start a python shell session. You will see the shell prompt:and execute:
>>>
2. Now type print “hello world!” and enter
>>> print “hello world”
3. What happened?

You would you seen “hello world” being printed onto the console
hello world

Congratulations!!! We successfully executed our first python program!

2. Now exit the sheel, open a new file hello.py using your favourite editor. Type print “hello world from script” and
save the file. Use python.exe to run this file from the command shell
cmd> python hello.py

What happened?
Python interpreter (continued):

python scripts use extension of .py (convention). You can have any other extension as well, but don't change
convention just to be different. It is not as cool.

We used the first python command as well.

Which was it?

Using help
Python help is available online. We can also get help locally. For this we may need to install documentation
locally.

Let us check that that

Exercise:

1. Use help on the above command

2. Install documentation locally. Documentation file is doc.tar.bz2.tar


Values and types:

basic components that a program works with


Example: 1, “hello world” etc

You can print a value


>>> print 1
1
>>> print “hello”
hello

Values have a type. Types denote the category of the value. Types can be primitives like numbers(integer, float),
strings, data-structures (list, dictionary, tuples), files

or..... what else can a type be?

Finding the type:


use the function “type”

>>> type(1)
<type 'int'>
>>> type(“hello”)
<type 'string'>
>>> type(“3.14”)

Exercises:

1. what is the type of type


2. what is the type of help
Variables:

one that holds a value


>>> hello = “hello world”

so hello is variable. Notice that we did not assign a type to the variable. Python figures it out. Hence it is called a
dynamic language.

What is its type?

You can print a variable


>>> print hello
hello world

prints the value of the variable

variables are powerful as they store the value and allow for its manipulation

Give a few examples of manipulating variables

Naming variables

there are a few rules for naming variables. Let us find out what some of these are by trial and error
Exercises:

Let us create three string variables:


>>> 9ratnas = “navaratna”
>>> me@home = “my email address”
>>> class = “our class”

What happened?
Variable naming rules:


(underscore or letter) + (any number of letters, digits, or underscores)

case matters

reserved words (keywords) not allowed

Examples:
name, _name, name1, name_1 (good)
name$, 1name, 1_name (bad)

Keywords (31 in number):

break,except,import,print,class,exec,in,raise,continue,finally,is,return,def,for,lambda,try,and,del,from,not,while,as
,elif,global,or,with,assert,else,if,pass,yield

Exercises:

1. What is a variable's type? (we covered it in the last slide)


>>> a = 1
>>> type(a)
>>> a = “a”
>>> type(a)

A variable (which is just a name) does not “carry” the type with it. It just has the type most recently associated
with it. Remember python is a dynamically typed language!
Statements: an instruction that the python interpreter can execute

Examples:
print “hello”
a=1

Operators: symbols that represent computation

Examples:
mathematical (+,-,x,/)
logical (and, or)

many others.. refer to one of the books

Expressions: combination of values, variables and operators

Examples:
>>> 1 + 1
>>> selling_price = cost_price – discount

Comments: notes to help humans understand the program. The computer (python interpreter) will ignore it

anything following a “#” symbol will be treated as a comment

Exercises:

Think like a python programmer book – Chapter 2 exercises

Вам также может понравиться