IT 116: Introduction to Scripting
Class 3
Today's Topics
Tips and Examples
Review
New Material
Reading Assignment
You should read Chapter 2, Input, Processing and Output,
from our textbook, Starting Out with Python, before next week's class.
Homework 2
I have posted homework 2 here.
It is due this coming Sunday at 11:59 PM.
Let's take a look at it.
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.
If you miss the graded quiz on Tuesday you can take it on Thursday.
If you miss it on both Tuesday and Thursday you will get a 0 on the quiz.
Tips and Examples
Class Exercises
- There is a Class Exercise for every class except the review sessions for the mid-term and final
- Each Class Exercise must be in a separate directory inside the ex directory
- The ex directory must be inside your it116 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 the following 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
- This script tests 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
- If you want to run my test scripts on your Class Exercise
you will find my test scripts here /home/ghoffman/code/it116_code/testing_scripts_it116
Creating the Class Exercise Script on Unix
- Many Class Exercises have two parts
- An interactive part where you type commands directly into Python
- And a script part where you write a script
- It is the scripts that I will check to give you a score
- All Class Exercise scripts must be created in a specific directory with a specific name
- Otherwise my test scripts will not see them
- If my test scripts cannot see your Class Exercise script you will get a 0 for the assignment
- The first thing you will need to do for today's Class Exercise is to log in to users3.cs.umb.edu
- When you do this you will be in your
home directory
- This directory belongs to your Unix account
- No one else can change it
- Then you need to go to your it116 directory
cd it116
- All Class Exercise must be inside a directory called ex inside your it116 directory
- You created this ex directory for the last Class Exercise
- You only create this directory once
- For each new Class Exercise you will have to create a directory inside the ex directory
Review
Working with Python
- In this class, we will be working with Python on the Linux machine users3.cs.umb.edu
- All our work for this course must be copied to this machine
- We will be using Python 3.5.1 or later
Running Python in Interactive Mode
- There are two ways of using Python
- Interactively
- Writing Python scripts
- If you enter
python3
on the Unix command line and hit Enter
you will find yourself using Python's
interactive mode
$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
- In Python interactive mode, you can type Python statements directly at the command line
- And get the results immediately
- Whenever you see >>>, Python is ready for your input
- In interactive mode, you can use the Python interpreter as a calculator
>>> 5 + 6
11
>>> 6 - 5
1
>>> 3 * 5
15
>>> 4.1 / 2
2.05
- To leave the interactive mode you must type
>>> exit()
- or hit Control D
Python Scripts
Working with Unix
- All work you create for this course
- must be created on users3.cs.umb.edu
in your class directory
- You must also run any scripts you create on this Unix machine to make sure they work
- This means you must deal with the command line
- There are no menus on the command line
- You must type the name of the command and any arguments it needs
- And then hit Enter (PC) or Return (Mac)
- If you make a mistake you will get an error message which is usually not very helpful
- Fortunately, you only need to know a few commands
- It will be frustrating at first but you will soon get learn how to do what you need
- Be patient and ask me for help when you run into trouble
Connecting to a Unix Machine
New Material
Basic Unix Commands
- As I said earlier, you only need to know a few Unix commands to do the work in this course
- You will find a good summary of them
here
- You will have to create a directory for each homework assignment and Class Exercise
- These directories must be created inside the it116 directory
which you will find inside your home directory
- For today's Class Exercise, you will have to create an ex directory
inside your it116 directory
- Here's how you do it
- After you log in, you will be in your home directory
- To make sure, you can use the
pwd
command
$ pwd
/home/ghoffman
- There should be a it116 directory in your home directory
- You can use
ls
to see if this is true
$ ls
- When
ls
is used without an argument it lists the contents of your current directory
- You need to go to this directory using
cd
$ cd it116
- This directory should contain two other directories, ex
and hm
$ ls
ex hw
- Every time you work on a new Class Exercise you need to go to the
ex directory
cd ex
- and use
mkdir
to create a new directory for this assignment
- For today's Class Exercise you will create ex3
mkdir ex3
- You will then need to go into this directory
cd ex3
- Now you need to create the script using
nano
nano ex3.py
- Once you have created the script you can see the text using
cat
which prints the file to the screen
$ cat ex3.py
a = 5
b = 6
c = 10
print("a + b + c = ", a + b + c)
print("a * b + c = ", a * b + c )
print("a * b * c = ", a * b * c)
print("c - b - a = ", c - b - a)
print("a * b / c = ", a * b / c)
print()
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts")
print("02125-3393")
The nano
Text Editor
- All your assignments for this class involve creating scripts on the Unix machine users3.cs.umb.edu
- Scripts are just text files that contain Python statements
- Test files only contain characters you type at the keyboard
- They do not contain fonts or styles like bold
- To create these files you need to use a Unix text editor
- There are many Unix text editors that you can use but I would suggest using
nano
nano
is a simple text editor created as part of the GNU project
- In
nano
you run a command by holding down the Control key while pressing a letter key
- The Control key is on the lower left side of your keyboard and is often labeled "Ctrl"
- The control key is a modifier key
- If you hold down the Control key and press another key it makes something happen
- You can move to the beginning of a line of text by pressing Control A
- And move to the end of the line with Control E
- "Control A" means hold down the Control key while pressing
A
- Although I used a capital A here
I do not mean you should hold down the shift key
- I use capital letters when writing control key sequences because the capital letters are easier to read
- Some of the basic
nano
commands appear at the bottom of the page
- The ^ in this list of commands stands for the Control key
so ^O means Control O
- The
nano
feature set is limited
- You can only work with one file at a time
- You can search for text but there is no search and replace feature
nano
does have a limited cut and paste feature
- If you press Control K the entire line will disappear
- Then if you go to another line and press Control U
the line will be pasted back at that point
- When you want to save a file you press Control O
- And the name of the file will appear at the bottom of the screen
- You need to hit Enter to accept that name and complete the save process
- Control X will quit
nano
- I have created a web page with instructions for using
nano
here
- There is a link to it on the class web page
The Hierarchical Filesystem
- Unix uses a hierarchical filesystem
- Which means that all files are kept in directories (folders)
- And directories live inside other directories
- There is one special directory at the top called
root
- root is written as /
- The hierarchical filesystem can be a little confusing
- But it is very important to know where you are in the Unix filesystem
- If you think you are in one directory,but you are actually in another,
certain Unix commands will not work the way you want them to
- You can always learn where you are by using
pwd
$ pwd
/home/ghoffman
- The output of the command above tells me I am in the ghoffman
directory
- This directory is inside the home directory
under the top directory, /
- Every time you connect to users3.cs.umb.edu
you will be in your home directory
- The name of your home directory is the name of your Unix account
- The name of my Unix account is ghoffman so this is the name of my home directory
- In our Unix system all home directories are contained in the
directory named home which is inside the top directory
- If you are every confused by where you are when connected to Unix >use the
cd
command with no argument
cd
- This will take you back to your home directory
Output
Literals
- Programs are used to calculate values
- But some values don't need to be calculated
- We know that there are 7 days in a week and 24 hours in a day
- When we write programs that need these values we need to write them directly inside the program
- A value that is written directly into the code and does not have to be calculated
is called a literal
Strings and String Literals
- All computer programs use different types of values
- The values can be words, numbers or other things
- When the value is just text, not a number, it is called a
string
- A string is just a sequence of characters
- A string can be calculated or we can write the string directly inside the code
- When we write a string directly inside the code we have created a
string literal
- In Python, string literals must be enclosed in either single quotes, '
or double quotes, "
- You can use either single or double quotes when writing a string literal
- But you must use the same type of quote at the end of a string that you used at the beginning
- If you don't you will get an error message
- In some computer languages, like C and Java each type of quote has a special purpose
- But not in Python
- Why?
- What if you wanted to print the following
What's for dinner?
- If you enclosed the string inside single quotes you would get an error
>>> print("Hello world!")
Hello world!
>>> print('What's for dinner?')
File "<stdin>", line 1
print('What's for dinner?')
^
SyntaxError: invalid syntax
- But we'll have no trouble if we enclose it in double quotes
>>> print("What's for dinner?")
What's for dinner?
- Similarly, if the string contained double quotes
we would enclose the string literal in single quotes
>>> print('He cried "Help!"')
He cried "Help!"
- Most of the time I use single quotes because I can write them without holding down the Shift key
- But you should feel free to use either kind of quotes
Triple Quotes
- There is another way to write string literals in Python that we will not be using in this class
- This method involves using three quotes at the beginning and end of the string
- You can use either single quotes or double quotes but whatever quote characters you use a the beginning
have to be used at the end
- You can't mix quotes
- It does not matter which type of quote you use as long as the quotes at the beginning and end are the same
- If you use triple quotes to enclose a string literal you can use both single and double quotes freely inside the string itself
>>> print('''He's crying "Help!"''')
He's crying "Help!"
- The other advantage to using triple quotes is that they can hold a string literal
that runs across several lines
>>> print("""Line 1
... Line 2
... Line 3""")
Line 1
Line 2
Line 3
- ... is the way the Python interpreter tells you it is waiting for more
- You can't do this with ordinary quotes
>>> print("Line 1
File <stdin>, line 1
print("Line 1
^
SyntaxError: EOL while scanning string literal
- This error message is saying that the interpreter can't find the end of the string
Characters
ASCII
- In order to represent characters as numbers we need a table of all the characters
and their corresponding numbers
- In the early days of computing in this country the most frequently used table of this type was called ASCII
- ASCII stands for American Standard Code for Information Interchange
- You will find the table for ASCII here
- ASCII was developed in the United States for use with teletype machines
- ASCII has 128 characters which stood for 95 characters
- And 33 control codes
- Each character can be stored in 7
bits of memory
- Lowercase and uppercase letters have different numeric values in ASCII
>>> ord('a')
97
>>> ord('A')
65
>>>
- Since the digits 0 to 9 are characters each of them is included in the ASCII table
>>> ord('1')
49
>>> ord('2')
50
>>> ord('3')
51
- ASCII also includes a number of symbols
>>> ord('#')
35
>>> ord('$')
36
>>> ord('%')
37
>>> ord('&')
38
Unicode
- ASCII was good enough for English but lacks characters used in many European languages
- Let alone Asian and African languages
- Nowadays we use an new character table called Unicode
- Unicode represents all the characters in most world languages
- The first 128 Unicode characters are the ASCII characters
- so ASCII is included in Unicode
- The latest version of Unicode contains 128,237 characters covering 135 modern and ancient writing system
- Most of the major world writing systems are contained in Unicode
- But Chinese characters pose a challenge
- Altogether there are over 50,000 Chinese characters though a good modern dictionary will rarely list over 20,000
- An educated Chinese person will know about 8,000 characters
- But you only need to know about 2-3,000 to read a newspaper
- Unicode currently has 74,605 CJK characters which are used in writing Chinese
- But also in Japanese Kanji Korean Hanja and Vietnamese Chu Nom
Representing Unicode Values
- With close to 130,000 characters you cannot represent all Unicode characters in only 7 bits as you can in ASCII
- Some characters would require 17 bits
- If you used 17 bits to represent every character text documents would be extremely large
- But the number values that represent different Unicode characters
can be represented inside the computer in different ways
- Each of these schemes is called an encoding
- The most commonly used Unicode encoding is UTF-8
- This is the encoding Python uses by default
- UTF-8 uses a variable number of bytes to represent each Unicode character
- Either 1, 2, 3 or 4 bytes
- If you stick to English each character is one byte long
- You will find a listing on Unicode characters represented in UTF-8
here
Ranges of Characters in ASCII
- The ASCII characters fall into certain ranges
- The first 31 characters are control characters
- The characters from 32 to 47 are symbols
32
33 !
34 "
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
- The numbers come next
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
- Then some more symbols
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
- Then the capital letters
65 A
...
90 Z
- Some more symbols
91 [
92 \
93 ]
94 ^
95 _
96 `
- The lower case letters
97 a
...
122 z
- And the last few symbols
123 {
124 |
125 }
126 ~
- Both Unix and Python are
case sensitive
- which means that UPPERCASE and lowercase letters are totally different characters
Variables
- In programming we often need to store a value that we will use later
- To do this, we create a
variable
- A variable is a location in the computer's memory with a name that stores a value
- In a many computer languages, such as Java you must specify what type of value a variable will hold
before you can use it
- So if you declared the variable to be a decimal number you could not store a string in it
- This is not true in Python
- You can store a string in a Python variable one moment >\then store an integer in it at some time later
Creating Variables with Assignment Statements
- You create a variable by giving it its first value
- This is done with an
assignment statement
- An assignment statement has the general form
VARIABLE_NAME = VALUE
- Whenever a word appears in ALL CAPITALS it is a
placeholder
- In other words, it stands for a value that you have to supply
- If I wanted to create the variable name and give it my name as a value
I would write
name = "Glenn Hoffman"
- If I wanted to define the variable
rate with the value .03 >I would write
rate = .03
- Once you have defined a variable, you can change it's value with another assignment statement
rate = .05
- That is why they are called variables
Attendance
Class Quiz