IT 116: Introduction to Scripting
Class 3
Tips and Examples
Review
New Material
Microphone
Readings
If you have the textbook you should read Chapter 2,
Input, Processing and Output,
from the textbook, Starting Out with Python.
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.
The quiz will be available on Gradescope on Tuesday.
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/
Recorded Class Meetings
You will find links to the recordings of class meeting
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.
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.
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.
Questions
Are there any questions before I begin?
Tips and Examples
Some Skills Only Come With Work
- This course requires you to learn and understand a number
of concepts
- You will find a list of them
here
- You must know them on a deep and fundamental level ...
- so that you can use them to accomplish a task
- It is not enough to memorize a definition ...
- though that is a first step
- Each of these concepts is a tool
- And the best way to learn a tool is to use it
- That is why there are so many assignments in this class
- You will be writing small programs for this course
- When you write them think of the concepts you are learning
- And what other things you can do with them
- You might want to play around with a concept
- Write a little something of your own ...
- to better see how they work
- Understanding comes with the effort
- The more you work with a concept ...
- the more you can do with it
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 on
pe15.cs.umb.edu
- And type the name of your file into your Piazza entry
- This way I can run the code myself
- And use various debugging techniques
Review
Running Python in Interactive Mode
- There are two ways of using Python
- Interactively
- Writing Python scripts
- When you run IDLE the first window you see ...
- is the Python interpreter running in interactive mode
- 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
- 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()
- If you have a Mac or a Linux machine you can hit Control
D to quit
Python Scripts
Attendance
New Material
Output
Literals
- Programs often 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 specific values
- We write these values directly inside the script
- A value that is written directly into the code is called a
literal
- In the
print
statement above, "Hello world!"
is a literal
- When I write
print(5)
5 is 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 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
- String literals must be enclosed in either single quotes,
' ...
- or double quotes, "
- It does not mater which one you use
- 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
- When the interpreter saw the ' in what's,
it thought the string was over ...
- and it had no idea what to do with the rest of the line
- To get around this we can enclose the string 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!"
- I tend to use doubles quotes because they are easier to see
- But you should feel free to use either kind of quotes
Triple Quotes
- There is another way to write string literals in Python
- But we will not be using it in this class
- This method involves using three quotes at the beginning ...
- and end of the string
- You can use either three single quotes or three double quotes
- But whatever quote characters you use a the beginning ...
- have to be used at the end
- You can't mix quotes
- 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!"
- Triple quote 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
- To use strings on a computer ...
- we need a table of all the characters and a number ...
- that represents them inside the computer
- In the early days of computing the most frequently used table was 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 had 128 entries which stood for 95 characters ...
- and 33 control codes
- Each character can be stored in 7
bits
of memory
- A bit is a binary digit
- Lowercase and uppercase letters have different numeric values in ASCII
>>> ord("a")
97
>>> ord("A")
65
>>>
- The digits 0 to 9 are also characters
- So 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 a new character table called Unicode
- Unicode represents all the characters in most world writing systems
- The first 128 Unicode characters are the ASCII characters
- So ASCII is included in Unicode
- The latest version of Unicode contains 128,237 characters
- They cover 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
- Japanese Kanji
- Korean Hanja
- Vietnamese Chu Nom
- A writing system is not the same thing as a language
- Cuneiform was a writing system developed in the Near East around
3,200 BCE
- It was used for writing on clay tablets
- The following languages were written in cuneiform
- Sumerian
- Akkadian
- Eblaite
- Elamite
- Hattic
- Hittite
- Hurrian
- Luwian
- Urartian
- Old Persian
- Unicode includes the Cueiform characters
Representing Unicode Values
- There are so many Unicode characters that you can't represent all of
them in 7 bits ...
- as you can with ASCI
- Some characters require 17 bits
- If you used 17 bits to represent every character, text documents would be
extremely large
- But the numbers that represent Unicode characters can be represented
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
- It uses the smallest number of bytes it can for each character
- 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 (Space)
33 !
34 "
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
- The digits 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
- What do you do with something you need to use ...
- but not at this moment?
- You put it somewhere it won't get lost ...
- so you can find it later
- Some old desk had slots to hold such things
- The slots were call pigeon holes
- 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 that has a name ...
- and stores a single value
- In some languages, such as Java, you must specify what type of value the variable holds
- You must do this before you use the variable
- And the type of value it holds cannot be changed
- If you declared the variable number to be a decimal
- 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 the same variable some time later
Creating Variables with Assignment Statements
- In Python 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 its value with another assignment statement
rate = .05
- That is why they are called variables
Class Exercise
Class Quiz