Monday, February 6, 2012

csci133c3.py

Welcome to the 3rd notes for the python programming lab. Let's take a look at the code and see what does it do. Today we will talk about the function. Function is a block of code that do varies kind of thing, and might or might not return the value it get.
line = 'This is a sample line of text.'
# Measure the length of the line string
print(len(line))

# Split the line string by spaces, return a list
print(line.split())

# Measure how many word are there now
print(len(line.split()))
Output:
30
['This', 'is', 'a', 'sample', 'line', 'of', 'text.']
7
Pay close attention to line #3, when we call a function (or to use a function). We can also pass some data if the function accept it, that data is called parameter, or argument. So we passed the line into the length function, len(line). And the function returned the result of 30, and then it is being passed to the print function and print on the screen. That is why it is outputed as 30.

Whenever we want to print thing on the screen, we are always actually calling the print function, and passing the number, letter, or word into the function. What about the split function? What does it do? From the python's web manual.
http://docs.python.org/library/stdtypes.html#str.split
str.split([sep[, maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified, then there is no limit on the number of splits (all possible splits are made).

For example, ' 1 2 3 '.split() returns ['1', '2', '3'], and ' 1 2 3 '.split(None, 1) returns ['1', '2 3 '].
The function require a string to be invoked on, that's why it has str.split(), the split function actually takes argument. But at the moment we just want it to split with blanks, or spaces. Note each data type has their own set of functions build in to them.

Example Problem #1: Turn a string into all lower case
If you want to turn a string to lower case
word = 'THE QUEEN'
word = 'THE APPLE'
newWord = word.lower()
print('Before:', word)
print('After:', newWord)
Output:
Before: THE QUEEN
After: the queen
Example Problem #2: Turn every word into lower case, remove the symbols, print them one by one. (One on each line). String is from a random novel on the web.
line = 'http://www.gutenberg.org/cache/epub/39133/pg39133.txt'
newLine = ''
for char in line:
    if char in 'abcdefghijklmnopqrstuvwxyz':
        newLine = newLine + char
    else:
        newLine = newLine + ' '
print('Before:', line)
print('After:', newLine)
print('Split it:', newLine.split())
for word in newLine.split():
    print(word)
Output:
Before: http://www.gutenberg.org/cache/epub/39133/pg39133.txt
After: http   www gutenberg org cache epub       pg      txt
Split it: ['http', 'www', 'gutenberg', 'org', 'cache', 'epub', 'pg', 'txt']
http
www
gutenberg
org
cache
epub
pg
txt 
So before we go, let's go to this link, download the file into our current dir. Because the next tutorial we will use that file! http://www.gutenberg.org/cache/epub/39133/pg39133.txt , and rename it to book.txt

Practice Problem:  Write a program to clean up the following string, turn all letters into lower case, and print one word per line.
foo = '[_He approaches Fabiani._]'
Output:
he
approaches
fabiani

No comments:

Post a Comment