OO Programming and Data Structures | CS 241

02 Prepare : Reading

Outcomes

This week we will work with two different topics in Python: Functions and Files.

At the end of this week, successful students will be able to:

  1. Show fluency in discussing files and functions.

  2. Correctly use files and functions to solve problems.

The preparation material for this week will again come from the online textbook at: Runestone Academy

Functions

Functions are one of the most fundamental constructs that we will use this semester. You should be familiar with them generally from your prerequisite course, but to understand in more detail how they work in Python, read the following:

Instructor Tip:

Please note that this reading refers back to some "Turtle Graphics" material that was covered in a previous chapter of that book (Chapter 4 - Python Turtle Graphics). Feel free to go back and read over that material if you would like a better understanding of this library, but in short, the "turtle" is like a point on the screen that can move forward, turn left/right, and leaves a trail where it goes.

When reading this material, make sure to use the exercises and interactive code blocks to enhance your understanding of the material.

Using a Main Function

In many programming languages, the program always starts running at a main function. In Python, your code doesn't actually have to be in a function. The execution starts at the top and proceeds line by line. However, in most cases, convention is that we still create a main function and use that to start our program.

So in practice, all of your code should be in functions, and then have a main function that is the master-delegater function that drives the rest of the program. Then, to make sure the program starts with main, we typically put this code at the bottom of our program:


if __name__ == "__main__":
    main()

This is a little bit complex, and you'll understand it better when we break our programs into multiple files, but the purpose of this logic is to make sure that your program is the one that the user actually ran, as opposed to your file being included by someone else's program.

Thus, the translation of this code is essentially, "If my program is the one that is being run directly, please call the main function to get it started."

If you're still not quite sure about this, it's ok, it will become more clear as we get used to it.

Files

There are many different ways to work with file input/output in Python. We will begin by learning how to work with them directly to loop through the contents of the file.

First, read the following:

Then, refer to the following information that summarizes working with files and parsing strings in Python. This will be directly applicable to your assignment this week, and may be helpful to refer back to later.

Reading from Files in Python

There are several ways to read data from files in Python, but if all you need to do is open it and read straight through it, the process is very simple.

To open a file, simply call open(filename), for example:


f = open("myFile.txt", "r")

The "r" says that we are opening the file just for reading.

Once, open, we can get all the lines of the file at once, or go through them one at a time.


# This is one way
all_lines = f.readlines() # reads all the lines into the all_lines variable

# This was is probably even easier
for line in f: # gets one line at a time from the file
    print line # we can print it or do anything else we want with it here

Parsing Strings in Python

There are also may ways to work with strings in Python, depending on the complexity of the task at hand. The simplest case is when you need to split a string based on a delimiter character. In this case you can use the .split(delimiter) function as follows:


# Consider a line with a : as a delimiter
line = "this:line:has:lots:of:words:in:it"

# The split function breaks a single string up into a list of strings
words = line.split(":")

# You can work with the entire list:
print(words)

# Or access a specific word:
print(words[3])

# Or access each of them one at a time
for word in words:
    print(word)