IT 117: Intermediate Scripting
Class 3
Tips and Examples
Python Review
Login Failure
If you enter the wrong password 5 times when connecting to pe15
you will be temporarily blocked from logging in to this machine.
This lock out will only last for 2 hours.
After 2 hours, you should be able try again.
If you have forgotten or misremembered your password, you can change it.
Go to https://portal.cs.umb.edu/password/
Recordings of Class Meetings
You will find links to the recordings of class meetings
here.
Use of Storage Space on the CS LAN
All of you will have an account on the Computer Science Department network.
With this account you get a home directory.
There is a limit on the total size of the files in your home directory.
So you should not store there anything you do not need for your classwork.
Homework 2
I have posted homework 2
here.
It is due this coming Sunday at 11:59 PM.
Format of Homework Assignments
Class Quizzes
From now on, each class will have a Class Quiz.
The Class Quiz is ungraded.
It is designed to help you remember the material covered in class.
Here is how it works.
Go to the listing of Class Quizzes you will find here.
Click on the link for the quiz you need to take.
Answer all the questions on a piece of paper.
Go to the listing of Class Quiz Answers you will find here.
Check your answers.
First Graded Quiz
The first graded quiz will be given on Tuesday next week.
The questions from the graded quiz are taken from the ungraded quizzes of the
previous week.
Unless I tell you otherwise, you should expect a graded quiz each week.
The quiz will be available on Gradescope on Tuesday.
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
http://96.126.105.215/umb_classes_html/it116_html/class_page_it116.html
.
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
http://96.126.105.215/umb_classes_html/it116_html/class_notes_it116/class_notes_index_it116.html
.
Do Not Post Anything From this Class Online
There are many web site that claim to "help" students
learn new material.
As far as I am concerned sites like Chegg and CourseHero get their money
by helping students cheat on their assignments.
Posting ANYTHING on such sites is a violation of your
Oath of Honesty and will be reported to the Provost's Office.
This includes quiz and exam questions as well as homework assignments
and Class Exercises.
Tips and Examples
Class Exercises
Getting Help
- This course makes you do things that you may not have done before
- Whenever you do something new there is always a good chance you will make
a mistake
- When this happens, the first thing you should do is reread the instructions
- Mistakes often happen when we read too fast
- If you cannot fix the problem in a reasonable amount of time, ask for help
- There are two ways you can do this
- Post a question in the class discussion area
- Email the Class Assistant
- DO NOT EMAIL ME with your question
- I have over 100 student each semester
- With that workload I must be very careful how I spend my time
- I would rather respond to your question on Piazza than in an email
- Only one student will see an email response
- But an answer on Piazza can be seen by the entire class
- Only send me an email when you have an issue that affects you and you alone
- Like being out of the country due to a family emergency
Describing a Problem
Don't Use Screenshots in Piazza
- Many students paste screenshots of code into Piazza
- This is not a good idea
- I have a hard time reading some of the screen shots
- And I can't copy the code into the Python interpreter to test it
- Instead copy the code to your class directory
- And type the name of your file into your Piazza entry
- This way I can run the code myself
- And use various debugging techniques
Making Script Executable
- All scripts submitted for this course must be
executable
- If not you will lose points
- You make a file executable by doing two things
- The hashbang line must be the very first line of the script
- The first two characters of this first line must be #!
- This must be followed by the absolute address of the Python 3 interpreter
- For this course the hashbang line must be
#! /usr/bin/python3
- You can make a file executable by running the following Unix
command on it
chmod 755 FILENAME
- FILENAME is a placeholder
- You replace this with the name of the file you are making
executable
- If I wanted to make the file hw2.py
executable, I would would write the following on the Unix
command line
chmod 755 hw2.py
Python Review
for
Loops
The range
Function
- Creating the list of integers is not hard if the list is short
- But what if we wanted to add the numbers from 1 to 100?
- Creating that list would be very tedious
- Python's
range
function makes it easy to create a list
of integers
>>> for number in range(10):
... print(number)
...
0
1
2
3
4
5
6
7
8
9
- Notice that the sequence of numbers started with 0
- And ended with one less than the value of the argument
- You can also give the
range
function two arguments
- In this case the first argument is the starting number
- And the second argument is one more than the last number
>>> for number in range(5, 11):
... print(number)
...
5
6
7
8
9
10
- You can even give three arguments to the
range
function
- If you do the first two arguments do the same thing as the two argument version
- But the third argument is the step value
- The step value is the amount by which each number is changed from the number
that came before
>>> for number in range(2, 11, 2):
... print(number)
...
2
4
6
8
10
Augmented Assignment Operators
- When writing code we often want to change a number by a certain amount
- If we wanted to add 1 to the variable num we could write
num = num + 1
- Statements like this are very common
- So Python has a set of special operators that will do three things
- Get the current value of the variable
- CHANGE this number by applying the operator
to the number on the right
- Assign this new value to the variable
- So instead of writing the statement above we could write
num += 1
- This operator works as follows
- Get the current value of the variable on the left
- Get the current value of the expression on the right
- Add the two values
- Assign this new value to the variable
- Using this operator makes the code shorter
- Which reduces the risk of errors
- An operator like this is called an
augmented assignment operator
- Python has an augmented assignment operator for every arithmetic operator
Operator |
Example |
Equivalent To |
+= |
numb += 5 |
numb = num + 5 |
-= |
num -= 5 |
num = num - 5 |
*= |
x *= 5 |
x = x * 5 |
/= |
x /= 5 |
x = x / 5 |
//= |
x //= 5 |
x = x // 5 |
%= |
x %= 5 |
x = x % 5 |
**= |
x **= 5 |
x = x ** 5 |
Nested Loops
- Just as you can have an
if
statement inside another if
statement
- You can have a loop inside another loop
- When you have one loop inside another it is called a
nested loop
- You can nest any type of loop inside another loop
- But nested
for
loops are the most common
- When you use a nested loop be sure each loop variable has different name
- If you don't you will get very strange results
- Let's write a script to create a times table
$ cat times_table.py
# This script creates a times table using nested for loops
for row in range(1, 11):
for column in range(1, 11):
entry = row * column
print(entry, end="\t")
print()
$ python3 times_table.py
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
- The loop variable row gets a new value with each
pass through the loop
- It keeps that value while the inner loop variable
column ranges from 1 to 10
- Notice the print statement in the innermost loop
- It print a number followed by a Tab
- This keeps the output for each row on its own line
- We need a call to
print
after the inner loop to move
to the next line
Functions
- The trick to writing software is to break down a big task into smaller tasks
- Then work on these smaller tasks one-by-one
- Functions
are a great help in splitting the work into smaller tasks
- 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
- The Python interpreter has a number of function that are predefined inside it
- They are called
built-in functions
- They are always available for you to use in your scripts
- You can see a list of them here
- But you can also define your own Python functions
- Most Python scripts contain a number of functions written specially for the script
- If you find yourself using certain function over and over again you can put
them in a file
- These files are called
modules
- You can bring these functions into your script using an
import
statement
Function Names
Defining a Function
Calling a Function
Parameters
Local Variables
Functions that Return Values
- Functions that return values can be used in assignment statements
>>> result = math.sqrt(7)
>>> result
2.6457513110645907
- Functions use a
return statement
to send a value back to the function call
- Here is the format
return EXPRESSION
- Remember, an expression is anything that the Python interpreter can turn into a value
- A
return
statement does two things
- It ends the execution of the function
- It sends a value back to the function call
- A
return
statement always causes the function to quit
- So 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 + " !"
- This version does not actually print anything
- So we need to put the call to the function inside the parentheses of
print
>>> print(cheer("Red Sox"))
Go Red Sox!
- We can do this because cheer now returns a value
- So a call to this new version of cheer 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
- To do this you follow the
return
with a comma separated list of
expressions
return EXPRESION [, 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
- So we will need two variables to receive these values
>>> fname, lname = get_full_name()
Please enter your first name: Glenn
Please enter your last name: Hoffman
>>> print("Hello", fname, lname)
Hello Glenn Hoffman
Standard Library Functions
- All computer languages come with functions that perform useful operations
- Here are some examples from Python
- print
- int
- float
- input
- range
- All of these are built-in functions
- Built-in functions are actually part of the Python interpreter
- So they are always available
- You do not have to do anything special to use them
- But every time you install Python you get many other useful functions
- These are called the
standard library
- The standard library consist of many files called modules
- Each module contains functions and variables that do related things
- For example the math module contains many
common mathematical functions
- But you cannot use a module functions inside a script unless you imported them
- You do this with an
import statement
import
statements have the following form
import MODULE_NAME
The MODULE_NAME is the file name without
the .py extension.
- When using function or variable imported from a module you must employ
dot notation
- First you write the name of the module
- Followed by a dot ( . )
- Followed by the name of the function or variable
- Here are some examples
>>> math.pi # trying to use a math module variable before importing math
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
>>> import math
>>> math.pi
3.141592653589793
>>> math.sin(math.pi)
1.2246467991473532e-16
>>> math.cos(math.pi)
-1.0
>>> math.ceil(4.3)
5
>>> math.floor(4.3)
4
>>> math.floor(-4.3)
-5
>>> math.ceil(-4.3)
-4
Objects
- A
variable
is a place in memory with a name that holds a single value
- Like an integer, float or boolean
- Objects
can hold more than one value
- They can also contain functions that work with those values
- The functions inside an object are called
methods
- Objects do not have names
- Instead they have a location in memory
- In order to work with an object this location has to be stored in a variable
- You can think of this variable as pointing to the object
- To refer to function or variable inside an object you use dot notation
Files
- 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
- As far as Python is concerned, there are two types of files
- 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
Using Files
- All operations inside a computer take place in the computer's RAM
- But files live in some external storage device like a hard drive or a CD
- To work with a file the computer needs a structure in RAM that can access the file
- This structure is called a
file object
- To work with a file you must create a file object connected to the file
Creating a File Object
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
OBJECT_VARIABLE.METHOD_NAME
- To write data to a file we must first open it for writing
teams_file = open("teams.txt", "w")
- The variable teams_file points to the new
file object
- Now we can use the write method of this new
file object to send some text to the file
teams_file.write("Red Sox\n")
- If you want to write a line to a text file you must include the
newline
character
- Which we write as \n
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
FILE_VARIABLE.close()
- Closing destroys the file object
- And breaks the connection to the file
- To close the file object pointed to by file
you would write
file.close()
Using a File More Than Once
- The file objects created with the access modes above only move in one
direction
- If you have read or written a line, you cannot go back
- If you need to read the lines in a file more than once you have to
- Close the first file object
- Create a new file object on the same file
Appending Data to a File
Reading Files with a for
Loop
- The best way to read a file is with a
for
loop
- You create a file object for reading
- And then use the variable that points to this object after
in
in the loop header
- The general form of a
for
loop is
for LOOP_VARRIABLE in LIST_OF_SOME_SORT:
STATEMENT
STATEMENT
...
- The LIST_OF_SOME_SORT
can be many things
- It can also be a file
- Text files contain strings with a newline character at the end
- So you can think of a text file as a list of lines
- To read a file you put the variable pointing to the file object
after the keyword
in
for VARIABLE in FILE_VARIABLE:
STATEMENT
...
- Here is an example
$ cat print_file.py
#! /usr/bin/python3
# prints the lines in a file with a for loop
filename = input("Filename: ")
file = open(filename, "r")
for line in file:
print(line)
$ cat print_file.py
#! /usr/bin/python3
# prints the lines in a file with a for loop
filename = input("Filename: ")
file = open(filename, "r")
for line in file:
print(line)
$ cat numbers.txt
3
3
9
5
2
7
6
8
10
1
$ ./print_file.py
Filename: numbers.txt
3
3
9
5
2
7
6
8
10
1
- Why is there a blank line after every number?
- Because the loop variable line contains the
entire line of text
- Including the newline character at the end of the line
- To fix this we should write
print(line.strip())
Attendance
Class Exercise
Class Quiz