IT 116: Introduction to Scripting
Tips and Examples
You should read Chapter 2, Input, Processing and Output,
from our textbook, Starting Out with Python,
before next week's class.
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 next week.
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 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 Sunday at 11:59 PM
- If the exercise is not complete or it has an error ...
- you will lose two points for each day it is late
- I will not check a Class Exercise from a previous class because it takes too much time
- If I have not checked you Class Exercise in class ...
- I will check it in the following week
- 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
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
- Writing Python scripts
- If you enter
python3 on the Unix command line and hit Enter ...
- you will find yourself using Python's
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
>>> 6 - 5
>>> 3 * 5
>>> 4.1 / 2
- To leave the interactive mode you must hit Control D
Working with Unix
- All work you create for this course ...
- must either be created on users3.cs.umb.edu ...
- in your class directory ...
- or copied there
- You must also run any scripts you create on this Unix machine ...
- to make sure they work in the environment in which I will run them
- 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
Basic Unix Commands
The Hierarchical Filesystem
- Unix uses a hierarchical filesystem
- Which means that all files are kept in directories (folders) ...
- and directories live inside other directories ...
- with one special directory at the top called
- which is written as /
- The hierarchical filesystem can be a little confusing
- If you are every confused by where you are when connected to Unix ...
- use the
cd command to go back to your home directory
Running Python on Your Machine
- All work submitted for this course must be in the proper directory ...
- inside you it116 directory ...
- on users3.cs.umb.edu
- You do not have to install Python on your home machine for this course
- If you want to install Pythonon your machine ...
- in order to experiment with it ...
- go to https://www.python.org/downloads/
and download Python 3
- This will install a program called IDLE
- If you run this program it will launch Python in interactive mode
- You can enter a Python statement in this window ...
- and then hit Enter to run the statement
- Notice that different parts of the window text appear in different colors
print appear in purple
- The "hello" inside the parenthesis appears in green
- The output of the print statement appears in blue
- This is called syntax highlighting ...
- a feature your will find in most text editors used by programmers
- If you click and drag down to to "New File" you will get a window ...
- into which you can type Python statements
- But notice that you don't see the >>> prompt ...
- of the Python interactive mode
- If you hit Enter, it will not run the statement ...
- it will merely move down another line
- This window is a Python text editor
- Notice that you see the same syntax highlighting you see in the interactive mode
- This editor has a number of features which make it easier to write Python scripts
- You can save this file as a Python script ...
- which should have a .py extension
- If you go back to the interactive mode window and type
- where SCRIPT_NAME is the filename of the script ...
- without the .py extension ...
- the script will be run inside the interactive mode window
- You can use this editor to create your Python scripts ...
- and then test them in the interactive mode window
- Running Python on your machine is good for experimenting ...
- but you must do all your assignments on users3
- The output of Python scripts is a stream of text
- We can generate output from Python using
- When we write
- we are writing a Python
- A statement is an individual instruction in the a computer language
- Every time the Python interpreter comes across a statement it does something
- The entire line,
print and what is inside the parentheses ...
- is a statement ...
print itself is a
- A function is a series of Python statements which has a name ...
- and performs some task
- When a statement runs a function ...
- it is said to
make a function call
- Most function need values to do their work
- These values are placed inside parentheses ...
- that come immediately after the function name
- These values that are called
- When we write
- The argument is "Hello world!"
- Functions can have zero, one, or many arguments
- When a function is written ...
- the programmer decides how many arguments it needs
Strings and String Literals
- Take a close look at what happens when we run the print statement above
>>> print("Hello world!")
- The argument to
print is enclosed in quotes ...
- but the output has no quotes
- The argument values can be of various types, such as
- Text values are called
- A string is just a sequence of characters
- When we write out the exact text we want to print ...
- we have created a
- 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
- 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!")
>>> 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!"
- Python also allows you to use triple quotes
- Triple quotes are three single ...
- or three double quotes at the beginning of a string literal ...
- at the beginning and end of a string of characters
- 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""")
- ... 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
SyntaxError: EOL while scanning string literal
- This error message is saying that the interpreter ...
- can't find the end of the string
- In order to represent characters as numbers ...
- we need a table of all the characters ...
- and the corresponding numbers
- In the early days of computing in this country ...
- the most frequently used table of this type ...
- was called ASCII ...
- which 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 8
bits of memory
- Lowercase and uppercase letters ...
- have different numeric values in ASCII
- Since the digits 0 to 9 are characters ...
- each of them is included in the ASCII table
- ASCII also includes a number of symbols
- 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 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 74605 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 here
Ranges of Characters in ASCII
- The ASCII characters fall into certain ranges
- The first 31 characters are control characters
- The characters from 32 47
- The numbers come next
- Then some more symbols
- Then the capital letters
- Some more symbols
- The lower case letters
- And the last few symbols
- This is one of the reasons Unix treats UPPERCASE and lowercase letters ...
- as different characters
- In programming we often need to store a value ...
- that we will use later
- To do this, we create a
- 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 ...
- when you declare 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
- An assignment statement has the general form
VARIABLE_NAME = VALUE
- Whenever you see a format statement in this class where a word is in ALL CAPITALS ...
- that means that it is a placeholder
- In other words, it stands for something 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