IT 116: Introduction to Scripting
Class 2
Today's Topics
New Material
Reading Assignment
You should read Chapter 1, Introduction to Computers and Programming, from our
textbook, Starting Out with Python,
before next week's class.
Homework 1
Remember, homework 1 is due this Sunday by 11:59 PM.
You will find it here.
Unix Lab Assistant
An IT student has been stationed in the Unix Lab to you with
your Unix questions.
They can also provide basic help with your assignments.
You can find details here.
This link also appears on the class web page.
Completing the Apply Process
A few of you have not completed the Apply process.
You must do this to get a class directory.
Without a class directory you cannot submit assignments.
New Material
We Will Be Using Unix in This Class
- All work in this course will be done on the Unix machine users3.cs.umb.edu
- You will need to connect to this machine from your computer
- This will mean that you will have to learn a few simple Unix commands to do your work
- But it will help you in your professional career
- A great deal of IT work can only be done at the command line
Working with Unix
- All work you create for this course must be done on users3.cs.umb.edu
- You must also run your scripts 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
- 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
- You can also get help from other students
- And people in the Unix Lab on the 3rd floor of McCormack
Connecting to a Unix Machine
Your Home Directory
- When you first connect to the Unix machine you will be in your
home directory
- A directory
holds files and other directories
- It is called a folder on Windows and the Mac
- Every Unix account on our system has a home directory
- This directory belongs to you and you alone
- You have complete control over this directory
- If you enter the Unix command
ls
you will see what is inside this directory
ls
is short for "list"
- If you have completed the Apply process you should see an
it116 directory
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
- There is one special directory at the top called
root
- But it is written as /
Moving To A Different Directory
- To move to a different directory you use the
cd
command
cd
stands for "change directory"
- The command has the following format
cd NAME_OF_DIRECTORY
- NAME_OF_DIRECTORY is a
placeholder
- When you use
cd
you replace NAME_OF_DIRECTORY
with the name of a directory
- To move to your it116 directory, type
$ cd it116
- The hierarchical filesystem can be a little confusing
- If you are ever confused by where you are when connected to Unix, there is a simple fix
- Use the
cd
command without an argument to go back to your home directory
Always Know Where You Are
- The hierarchical filesystem is like pyramid
- The root directory is at the top
- All other directories are inside this directory
- Or in sub-directories
- Or in sub-sub-Directories
- And so on
- The pyramid gets wider the deeper you go
- Many commands will not work properly if you are in the wrong directory
- Always know where you are in the hierarchical filesystem
- One way to learn your location is to use the
pwd
command
$ pwd
/home/ghoffman
pwd stands for "print working directory"
Creating A New Directory
Types of Programming Languages
Python
- Python was conceived in the late 1980s
- It was first implemented in December 1989 by Guido van Rossum in the Netherlands
- There are two versions of Python currently in use
- Python 2 was released in October 2000
- Python 3 is the latest version of Python
- Unfortunately, scripts written in Python 3 will not always work in Python 2
- And vice versa
- We will be using Python 3 in this course
- The Python design philosophy emphasizes code readability
- It tries to express concepts in fewer lines of code than 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
- We will be working with Python on the Linux machine users3.cs.umb.edu
- All your work for this course must be done on this machine
- If you wish, you may install Python on your own machine
- But this is not necessary for this course
- The Resources page has a download link for Python
- 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.2
Running Python in Interactive Mode
- There are two ways to use Python
- Interactively
- Writing Python scripts
- If you enter
python3
on the Unix command line and hit Enter you will be running Python in
interactive mode
$ python3
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
python3
, not python
- If you run
python
you will be using the wrong version of Python
- In interactive mode you are talking directly to the Python interpreter
- In this 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
- Typing
help()
will get you started
>>> help()
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>
- Typing
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"
help> 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
help>
- To exit the help mode type "q"
help> 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
11
>>> 6 - 5
1
>>> 3 * 5
15
>>> 4.1 / 2
2.05
- 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!")
Hello world!
- When writing code we often need to store a value that will be needed later
- All computer languages have variables
- A variable is a place in memory with a name, that holds 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
30
>>> a - b
-1
- 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
- To leave the interactive mode enter the following
exit()
- If you using a Mac or a Linux machine you can quit interactive mode using 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 you want to know if something is legal in Python, 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
Python Scripts
Scripts versus Interactive Mode
Today's Class Exercise
- The Class Exercises give you hands on experience with Python
- All Class Exercises must be done on the Unix machine
users3.cs.umb.edu
- In today's exercise you will practice some basic skills needed for this course
- You will find it here
- Let me show you how to connect to the machine and do the Exercise
Attendance