IT 117: Intermediate Scripting
Tips and Examples
In Chapter 9, Dictionaries and Sets, from our textbook,
Starting Out with Python,
you should read section 9.1, Dictionaries, before next Tuesday's class.
I have posted homework 2 here.
It is due this coming Sunday at 11:59 PM.
Class Page for IT 116
You may find it useful to look at my materials for IT 116.
I have a web site where I post most of the materials for my courses for
the previous semester.
You will find these materials here
I have included this link on the class page for the course.
Go to the index for the Class Notes and search for a topic.
You will find the index here
Tips and Examples
- There is a Class Exercise for every class ...
- except the review sessions
- Each Class Exercise must be in a separate directory ...
- inside the ex directory ...
- inside your it117 directory
- If you finish the exercise in class I will check it
- If you do not finish it in class you can finish it later
- All Class Exercises must be finished before Sunday at 11:59 PM
- If I have not checked you Class Exercise in class ...
- I will check it in the following week by running a script ...
- on all exercises I have not checked in class
- If your exercise is OK when I run my script you will hear nothing
- If there is a problem with your script or it is missing ...
- I will send an email to you UMB email address
- If the exercise is not complete or it has an error ...
- you will lose two points for each day it is late
- you will find my test scripts here /home/ghoffman/code/it117_code/testing_scripts_it117
- The trick to writing software ...
- is to break down a big task into smaller task ...
- and work on these smaller tasks one-by-one
are a feature of all modern programming languages
- that makes working on these smaller tasks easier
- A function is a group of statements that has a name and performs a specific task
- Each function operates in its own environment ...
- so it can be isolated from the rest of the program
- This means that a mistake in the bigger program ...
- or another function ...
- should not affect how the program works
- The Python interpreter has a number of functions ...
- that come with the Python installation package
- They are called
built-in functions ...
- and you can use them in any Python script
- You can see a list of them here
- But you can define your own Python functions
- Most Python scripts have a number of functions inside them
- You can also create files with a number of useful functions ...
- and then use the Python
import command ...
- to bring these functions into any Python script
Defining a Function
Calling a Function
- When we make the function call
- The interpreter creates memory space for the function cheer ...
- creates the variable cheer ...
- and assigns it the value of the variable home_team ...
- from the program's section of memory
- The picture in memory now looks like this
- Once the function has finished it's work ...
- it's memory space disappears ...
- and memory again looks like this
- Since a function runs inside its own space in memory ...
- any variables you created inside a function ...
- are invisible outside that function
- Variables created inside a function are called
- A local variable is created inside a function ..
- and can only be used inside that function
- The function can see and use the values of all variables ...
- in the main program memory
- But the program ...
- and other functions ...
- cannot see the local variables inside a function
Functions that Return Values
- Functions that return values can be used in assignment statements
>>> result = math.sqrt(7)
- For a function to give a value back ...
- to the Python statement that called it ...
- it must use the
- A return statement
is a Python statement that uses the
return keyword ...
- and has the format
- Remember, an expression is anything that the Python interpreter can turn into a value
return statement does two things
- It ends the execution of the function
- It sends a value back to the function call
- Since a
return statement always causes the function to quit ...
- any Python statements after
return will never be executed
- Here is a new version of the cheer function ...
- that returns a value
>>> def cheer(team):
... return 'Go ' + team + '!'
- Since this version does not actually print anything ...
- we need to put the call to the function ...
- inside a call to
>>> print(cheer('Red Sox'))
Go Red Sox!
- We can do this because this version of cheer returns a value ...
- so it is an expression ...
- and we can always use an expression as the argument to a function
Returning Multiple Values
- A Python
return statement can return more than one value
- All you have to do is follow the
return keyword ...
- with a comma separated list of expressions
return EXPRESION, EXPRESSION [, EXPRESSION, ...]
- Using this feature I can write a function to get both the first and last name
>>> def get_full_name():
... first_name = input("Please enter your first name: ")
... last_name = input("Please enter your last name: ")
... return first_name, last_name
- When this function is called it will ask for two values ...
- and return each to a separate variable
>>> fname, lname = get_full_name()
Please enter your first name: Glenn
Please enter your last name: Hoffman
- which I can use
>>> print("Hello", fname, lname)
Hello Glenn Hoffman
Standard Library Functions
- A file is simply a linear arrangement of data on some storage medium
- Files are identified by their name and their location
- The data inside a file can be in any one of a number of formats
- But all these formats come in two different types
- Text files contain characters, symbols and digits which make sense to us
- Binary files store their information a sequences of binary numbers
- All of the work we will be doing in this class will be with text files
Accessing the Information in Files
- All operations inside a computer take place in the computer's working memory ...
- usually called RAM, which stands for Random Acces Memory
- But files live in some storage device ...
- like a hard drive or a CD ...
- outside of RAM
- To work with a file the computer needs a structure in its memory ...
- that can talk to the file wherever it lives
- This structure is called a
- An object
is a section of the computer's memory that holds values associated with some thing ...
- and functions that work with these values
- The functions inside an object are called
- Since the file lives outside of the working memory the computer uses ...
- the Python interpreter must create a file object in RAM ...
- in order to do anything with the file
- Objects have no name ...
- they only have a location in the computer's working memory
- So when a file object is created ...
- a variable is also created that refers to this file object
- Files are often large ...
- so most of the time the computers RAM ...
- only holds a small part of the file ...
- in the file object
Opening a File
Writing Data to a File
- Objects contain values ...
- and the functions that work with those values
- the functions inside objects are called methods
- To call a method you must use dot notation
- Dot notation for methods has the following format
- To write data to a file we must first open it for writing
teams_file = open("teams.txt", "w")
- This will give us a file object referenced by teams_file
- Now we can use the write method of this file open ...
- to send some text to the file
Closing a File
- When you have finished working with a file ...
- you should close it
- This will allow other programs to use the file
- To close a file you use the close method
- The general format for using the close method is
- If I created a file object whose location was contained in the variable file ...
- and I wanted to close it ...
- I would write
Using a File More Than Once
- If I use a file object to read through a file once ...
- and I want to read through it a second time ...
- I must create a new file object
- That's because a file object can only read through the file ...
- going forward
- A file object cannot be "rewound" ...
- to the beginning of a file
- To create a new file object ...
- you should first close the original file object
- then create a new file object on the original file
- Every time you want to go through a file a second time ...
- for either reading or writing ...
- you must create a new file object
Appending Data to an Existing File
Reading Files with a
- But Python has a better way of using the readline method ...
- inside a loop
for loop works a list of values
- As long as there is a value in the list that has not already been used ...
- that value is assigned to the loop variable
- This process continues ...
- until all the list values have been used
- A text file broken up into lines ...
- each separated from the next by newline ...
- is a list of sorts
- It is a list of lines
- Python let's you loop over a file object ...
- assigning each line in turn ...
- to the loop variable
- When you do this ...
- you don't have to use readline
- Here is the format
for VARIABLE in FILE_VARIABLE:
- Using this feature of the Python file object ...
- we can rewrite our script
filename = input("Filename: ")
file = open(filename, "r")
total = 0
count = 0
for line in file:
number = int(line)
total += number
count += 1