Monday, February 20, 2012

csci133c4.py

Since we downloaded a txt file, this tutorial is about opening it. Here is the code.
with open('book.txt') as book:
    # The book is a list containing all lines
    for line in book:
        # If the line contain the string 'yourselves'
        if 'yourselves' in line:
            print(line)
The first line there is a reserved python keyword with, followed by open('filename'). It is actually a path name between the (), but if you don't provide a path, it is assumed it is the current directory. In case you have to access a file from a different folder, you would provide it a path with the file name. Such as ../../csci133/ch1/csci133c4.py.

You would open the text file into another object . You don't have to call it book, you can call it anything you want, that would be list containing all the "lines". In the event of it is actually a text file with strings after strings, you does make sense to call it something along the line of data, book, and myFile. You can try to iterate through it with for loop.
for object in objects:
    # do something here
Example problem #1: Count the number of words contained in the txt file
count = 0
with open('book.txt') as book:
    for line in book:
            # += is the same as count = count + count
            count += len(line.split())
print('There are', count, 'of words.')
Before we are done, there is also way to get input from the user, you would set up a variable, and assign it the value of the input.
Example problem #2: Take an input from the user (like cin in C++)
name = input('What is your name?')
print('Hello, nice to meet you', name) 
Python's list comprehension
It is a handy way to write code in a very short way, compare the two code. They do the same thing. I heard from people, it is a way to pretend to be smart, on the other hand, it seems useful to write code in a short way I guess. I prefer slightly the longer way, but I have a feeling in the professional world, everyone use list comprehension. Because it is faster, and less likely to create errors.
for number in numbers:
    data[number] = data[years][time] 
    return data
the data to be changed for object in objects
return [data[number][time] for number in years]
# Regular code
iceCreams = IceCreamFlavor()
iceCreamMenu = [] # iceCreamMenu is a list
for number in range(5):
    iceCreamMenu = iceCreams.random()

# Same code with list comprehension
iceCreams = IceCreamFlavor()
iceCreamMenu = [iceCreams.random() for number in range(5)]
The more I use it, the more I think it is useful. Just like all functions and methods (well, most of them). It is meant to help us write program faster and easier. Just like a a lot of the helper function is build in, this is build in to help us not have to spend as much time to get what we want.

Notice the range(5)? That means it is created a list of 5 numbers, [1, 2, 3, 4, 5]. Useful tool as well! Instead of doing a = [1, 2, 3, 4, 5]. And for number in a.
# The range(5)
for number in range(5):
    # Statement

# Same as this
numbers = [0, 1, 2, 3, 4]
for number in numbers:
    # Statement
Reference: http://docs.python.org/library/functions.html#range
range([start], stop[, step]) 
It is very commonly used in for loops, the start and stop arguments must be integers, if you didn't give it a step argument, it will be default to 1. If you didn't give it a start argument, it will be default to 0.
The range() function create a list with the number 0, 1, 2, 3, 4. Notice python is zero based, so if you need to print out from 1 to 5. You would not want to offset the number variable. <Correct way: to use the start argument>
# To print 1-5 the correct way
for number in range(1,6):
    print(number)
Output:
>>> for number in range(1,6):
...     print(number)
... 
1
2
3
4
5
>>>  
As for the step argument, it is what the increment is used. If you use the step of 2. Ex: range(0, 10, 2), it will give you [2, 4, 6, 8, 10]

Practice Problem:
# Write a range(start, stop, step) that generate the following results
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 2, 4, 6, 8, 10]
[1, 3, 5, 7, 9, 11]
[0, 50, 100]
[3, 9, 12, 15, 18, 21]

Answer to the problems: Notice for the question when it ask you where to stop, you can either stop at the next value (11) for the case of 10, or stop at the next increment value, 150 for the case of 100, or 101 for the case of 100. It will still work the same. I just like to do it this way, if you get the same answer, then you should be good to go.
>>> for number in range(10):
>>> for number in range(1, 11):
>>> for number in range(0, 12, 2):
>>> for number in range(1, 13, 2):
>>> for number in range(0, 150, 50):
>>> for number in range(3, 23, 3):
Note: Because reading again on the range() documentation, I thought the only way to do print 1-5 is to offset. Which show us how important and good it is to read the reference.

No comments:

Post a Comment