IT 116: Introduction to Scripting
You should read Chapter 1, Introduction to Computers and Programming, from our
textbook, Starting Out with Python,
before next week's class.
I have posted homework 1 here.
It is due this coming Sunday at 11:59 PM.
Completing the Apply Process
A few of you have not completed the Apply process.
You must fix this by going to the see the operators in the Unix Lab.
I will read out the names of those students who have not completed the Apply
process after taking attendance.
Types of Programming Languages
- Python was conceived in the late 1980s
- It's first implementation was started in December 1989 ...
- by Guido van Rossum in the Netherlands
- Guido van Rossum is still the principle author of Python
- The Python community has given him the title of Benevolent Dictator for Life (BDFL)
- There are two versions of Python currently in use
- Python 2.0 was released in October 2000
- Python 3.0 is a major release
- Unfortunately, Python 3.0 is not backwards compatible with Python 2.0
- We will be using Python 3.5.1 or later
- The Python design philosophy emphasizes code readability
- It tries to express concepts in fewer lines of code ...
- than would be possible in languages such as C
- The core philosophy of Python is spelled out in the document "PEP 20 (The Zen of Python)"
- It includes the following statements
- Beautiful is better than ugly
- Explicit is better than implicit
- Simple is better than complex
- Complex is better than complicated
- Readability counts
Working with Python
- In this class, we will be working with Python on the Linux machine users3.cs.umb.edu
- All your work for this course must be copied to this machine
- You should feel free to work on your own machine
- If you own a Mac, Python is already installed, but it is the wrong version
- In this class we will be using Python 3.5.1
- Go to https://www.python.org/download/ to download this version of Python
- You will find instructions for installing Python
- Appendix A in your textbook has some additional advice on installing Python on a PC
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 be running Python in
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
- Notice that the command I used was
- If you run
python you will be using the wrong version of Python
- In interactive mode you are talking directly to the Python
- 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
help() will get you started
Welcome to Python 3.5's help utility!
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.5/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
help() with no arguments,
puts you in help mode ...
- as you can see by the "help>" prompt
- when you are in help mode, you can just type a topic name ...
- to get information on that topic
- to see a list of topics type "topics"
Here is a list of available topics. Enter any topic name to get more help.
ASSERTION DELETION LOOPING SHIFTING
ASSIGNMENT DICTIONARIES MAPPINGMETHODS SLICINGS
ATTRIBUTEMETHODS DICTIONARYLITERALS MAPPINGS SPECIALATTRIBUTES
ATTRIBUTES DYNAMICFEATURES METHODS SPECIALIDENTIFIERS
AUGMENTEDASSIGNMENT ELLIPSIS MODULES SPECIALMETHODS
BASICMETHODS EXCEPTIONS NAMESPACES STRINGMETHODS
BINARY EXECUTION NONE STRINGS
BITWISE EXPRESSIONS NUMBERMETHODS SUBSCRIPTS
BOOLEAN FLOAT NUMBERS TRACEBACKS
CALLABLEMETHODS FORMATTING OBJECTS TRUTHVALUE
CALLS FRAMEOBJECTS OPERATORS TUPLELITERALS
CLASSES FRAMES PACKAGES TUPLES
CODEOBJECTS FUNCTIONS POWER TYPEOBJECTS
COMPARISON IDENTIFIERS PRECEDENCE TYPES
COMPLEX IMPORTING PRIVATENAMES UNARY
CONDITIONAL INTEGER RETURNING UNICODE
CONTEXTMANAGERS LISTLITERALS SCOPING
CONVERSIONS LISTS SEQUENCEMETHODS
DEBUGGING LITERALS SEQUENCES
- To exit the help mode type "q"
You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)". Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.
- In interactive mode, you can use the Python interpreter as a calculator
>>> 5 + 6
>>> 6 - 5
>>> 3 * 5
>>> 4.1 / 2
- But the best way to use interactive mode is to experiment with Python
- The simplest thing a computer program can do is to print something
>>> print("Hello world!")
- All computer languages have variables
- A variable is simply a place in memory with a name ...
- that can hold a single value
- Giving a value to a variable is called assignment
- In Python, the assignment operator is the equal sign, =
>>> a = 5
>>> b = 6
>>> a * b
>>> a - b
- If you type something that is not a legal Python statement ...
- you will get an error
>>> a $ b
File "<stdin>", line 1
a $ b
SyntaxError: invalid syntax
- If you have written programs before this will not surprise you
- But there is one kind of Python error that you have not seen before
- Python uses whitespace differently than other languages
- Whitespace are the characters
- The newline character is what you get when you press Enter on a PC ...
- or Return on a Mac
- Most computer languages ignore whitespace most of the time
- It's normal purpose is to make the code more readable
- But in Python, whitespace has meaning ...
- when it comes at the beginning of a line
- I'll save the details for later ...
- but for now do not put extra spaces ...
- before the start of a statement
>>> a + 1
File "<stdin>", line 1
a + 1
IndentationError: unexpected indent
- If you look carefully, you'll there is an extra space before the prompt
- Removing this space fixes the error
>>> a + 1
- To leave the interactive mode you must hit Control D
Experimenting with Python
- The interactive mode is a great way to experiment with Python
- Those of us who work with computers are lucky
- Whenever we have a question, we can ask the machine
- If we want to know if something is a legal Python statement ...
- we can just type it in and see what happens
- Chemists are not so lucky
- To find out if a chemical reaction will work ...
- they need a laboratory stocked with chemicals and equipment
- But we carry our laboratories around in our laptops
- We can experiment at any time ...
- in any place
Scripts versus Interactive Mode
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 that I will use when I score 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 Unix commands
- It will be frustrating at first ...
- but you will soon learn how to do what you need
- Be patient and ask for help in the class discussion area ...
- when you run into trouble
Connecting to a Unix Machine
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
- 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
- There should be a it116 directory in your home directory
- You can use
ls to see if this is true
ls is used without an argument ...
- it lists the contents of your current directory
- You need to go to this directory using
$ cd it116
- This directory should be empty
- You need to create the ex directory ...
mkdir (make directory)
$ mkdir ex
- To be sure this works, use
The Hierarchical Filesystem
- Before we go any further, we needed to talk a little about the way Unix arranges files
- 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
Copying Files to Unix
- FileZilla makes copying files to a Unix machine easy
- You can download it from https://filezilla-project.org
- When you open FileZilla you will see a window like this
- At the top of the window are four fields you must fill in
- Host is the domain name of the machine to which you are connecting
- For this class, that machine is users3.cs.umb.edu
- In the next box, enter your Unix username
- The third box is for your password
- Enter 22 in the last box
- Your window should now look like this
- Hit the QuickConnect button
- The first time you do this, you may see a an alert box that looks like this
- Click the checkbox "Always trust this host, add this key to the cache"
- Click the OK button
- You should now see you Unix home directory on the right side of the screen
- The rectangle directly below the top four fields shows the status of your connection
- The rest of the window is split into two sections
- The section on the left shows the current directory of your machine
- The section on the right shows the current directory of the Unix machine
- The small box on top of each section gives your current path on that machine
- The large rectangle below this box gives you a graphical view ...
- of your current directory in the hierarchical filesystem
- The rectangle below shows the contents of your current directory
- If click on the triangle to the left of the directory ...
- it will show the directories inside that directory
- You can use the scroll bar on the right to see the entire list
- If you click on the icon of the directory itself ...
- you will change your current directory
- Notice that the path in the box on top has changed
- Using this technique you can go to the directory ...
- that holds the file you want to copy
- Now you need to repeat this process ...
- on the left side of the screen ...
- to get to the Unix directory where you want to move the file
- Now drag the file from your machine on the left ...
- to the Unix machine on the right
nano Text Editor
- For the Class Exercises you will need to create Python script on users3
- You could do this using a text editor on your machine ...
- and then copying it to users3 using FileZilla
- Notice that I said text editor
- It can't be a Word or RTF document
- Alternatively, you can ssh to users3 ...
- and use the
nano text editor
nano is a simple text editor created as part of the GNU project
nano you issue a command by holding down the Control key ...
- while pressing a letter key
- 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
- When I write something like "Control A" ...
- I mean hold down the Control key while pressing the A key
- 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
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
- I have created a web page with instructions for using
- There is a link to it on the class web page