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 ...
- excep the review sessions
- Each Class Exercise must be in a separate directory ...
- inside the ex directory ...
- inside your it116 directory
- If you finish the execise 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 execise 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
- If you download Python 3 from https://www.python.org/downloads/
on your machine ...
- it 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
- 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, "
- 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
- Everything in a computer's memory is a
- Binary number are written in powers of 2 ...
- and use a string of 1s and 0s
- To represent a character inside a computer we need a table of number values ...
- and the specific character that value represents
- For example, we might say that the value 1 ...
- represents the character a
- In the early days of computing in this country ...
- the most frequently used table of number values and their characters ...
- was called ASCII ...
- which stands for American Standard Code for Information Interchange
- You will find the table for ASCII here
- ASCII only has 128 characters ...
- and 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
- The first 128 characters in Unicode are ASCII
- 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
- You will find a listing here
- 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