IT 117: Intermediate Scripting
Class 3
General Advice
Course Work
Tips and Examples
Python Review
Microphone
Trouble? Questions?
Has anyone had any trouble doing something for this class
or have any questions?
Homework 2
I have posted homework 2
here.
It is due this coming Sunday at 11:59 PM.
Today's Class
Today I will continue the review of the material
covered in IT 116.
General Advice
Do You Have Enough Time to Do the Work for This Course?
- Many of you work, either part or full time
- This cuts down on the time you have to study
- Technical courses are filled with details where accuracy is very important
- They also introduce concepts which may take some time to fully understand
- Such courses require a significant amount of study time ...
- to learn the material and do well in class
- This studying should not be done on one day ...
- but spread out over the days of the week
- You should not be taking this course ...
- if you can't study several hours a week for it
- Too many students rush to finish a degree ...
- without doing the work to really learn the material
- These people are setting themselves up for professional failure
- If you try to do too much ...
- If you try to do too much ...
- you may end up accomplishing too little
- Today is the last day of Add/Drop ...
- when dropping the class won't mean losing money
- Be honest with yourself
- Looking at all your other commitments ...
- do you have enough time for the work I will demand of you?
Course Work
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 day's quiz
- Get a blank piece of paper
- Write the answers to the questions on the paper
- Go to the listing of Class Quiz Answers you will find
here
- Check your answers
- Do not give me the paper with your answers
- Class quizzes are not graded
- They are designed to help you learn the material covered in class
First Graded Quiz
- The first Graded Quiz will be given on Tuesday next week
- I will pass out the exam papers after my lecture
- Questions on the Graded Quiz come from the Class Quizes
- Usually the quizzes from the previous week
- Unless I tell you otherwise ...
- you should expect a Graded Quiz each week on Tuesday
- You will be able to see your scores on Gradescope
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 a number of ways you can do this
- Post a question in the class discussion area
- Ask other students in the class
- Come to Zoom Office Hours
- DO NOT EMAIL ME with your question
- I have close to 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 on
pe15.cs.umb.edu ...
- and type the name of your file into your Piazza entry
Making Script Executable
- All scripts submitted for this course must be
executable
- If your scripts are not executable you will lose points
- You make a file executable by doing two things
- Using the
hashbang line
- Giving all uses read and execute permissions
- 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
- In FileZilla you can give execute permission by right clicking
on the script file ...
- selecting "Permissions" from the menu ...
- and entering "755" in the box provided
Login Failure
- If you enter the wrong password when connecting to pe15 ...
- you will be blocked from logging in for 10 minutes
- Wait 10 minutes and try again
- If you have forgotten or misremembered your password, you can change it
- Go to
https://portal.cs.umb.edu/password/
- If you have trouble with your account send an email to operator@cs.umb.edu
Attendance
Python Review
for Loops
The range Function
- Python's
range function makes it easy to create a list
of integers ...
- that can be used in a
for loop
>>> 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
- It is the amount added to the previous number ...
- to get the next number in the sequence
>>> 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 variable by a certain amount
- If I wanted to add 1 to the variable num I
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 one loop inside another
- 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
- Here's 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 prints 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 predefined functions inside it
- They are called
builtin 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 that script
- If you find yourself using certain functions over and over again ...
- you can put them in a file ...
- and import them into other scripts
- These files are called
modules
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 ...
- and function calls
>>> result = math.sqrt(7)
>>> result
2.6457513110645907
>>> round(math.sqrt(7))
3
- Functions use a
return statement
to send a value back to the function call
- Here is the format
return EXPRESSION
- An expression is anything that the Python interpreter can turn into a value
- There are four types of expressions
- Litterals
- Variables
- Calculations involving operators
- Function calls
- 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 a call to the function ...
- inside the parentheses of a call to
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 builtin functions
- Builtin 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 contained in special files which make up 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 import 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 a function or a variable imported from a module ...
- you must use
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 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 ...
- its 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 must use dot notation
Files
- A file is simply a linear arrangement of data ...
- on some long term storage device ...
- like a hard drive, thumb drive or CD ROM
- 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: letters, symbols and digits
- These characters are grouped into lines ...
- which have the newline character \n at the end
- So in some sense, a text file is a list of lines
- Binary files store their information as a sequence 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 has data which allows the you to use 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 file object ...
- to write 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
Using a File More Than Once
- The file objects created with the access modes above only move in one
direction
- If you have read a line, you cannot go back
- If you need to read the lines in a file more than once
- you have to reate 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 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())
Class Exercise
Class Quiz