IT 116: Introduction to Scripting
Class 2
Course Work
New Material
Microphone
Readings
If you have the textbook you should read Chapter 1,
Introduction to Computers and Programming,
from the textbook, Starting Out with Python.
Homework 1
Remember, homework 1 is due this Sunday by 11:59 PM.
You will find it here.
Unix Account and Class Directory
Some you have not created a class directory and a
Unix account.
All work for this course MUST be copied to your class directory
A directory is what you probably call a "folder".
Without a class directory you cannot submit assignments.
If you do not have a Unix account go to
http://www.cs.umb.edu/~ghoffman/linux/cs_portal.html#creating_unix_account.
If you do have a Unix account you still need a class directory
for this course.
Go to
https://portal.cs.umb.edu/accounts/login/
,
log in, click the check box for my section of this course, and click
submit.
Help with Creating a Unix Account
If you have difficulty creating an account
must get help NOW!
You can see me after class or during office hours.
You can contact me on Zoom during office hours.
Or you can contact the
Class Assistant.
Whatever it takes, you MUST create a Unix account and
a class directory.
Questions
Are there any questions before I begin?
Course Work
Assignments
- There are two types of assignments for this course
- Class Exercises
- Homework assignments
- In the Class Exercise you simply copy some code
- The idea is to give you some practice writing and debugging Python code
- For homework assignments you write your own code
- There is a Class Exercise for each class
- There is one homework assignment each week
- All Class Exercises and homework assignments are due on the Sunday after they
are assigned
- If you miss the deadline you can still submit your scripts until the following
Saturday
- But you will lose 2 points for each day you are late
Submitting Assignments
- You can create the scripts for assignments on your machine
- But they must copied to your class directory on
pe15.cs.umb.edu
- I will only score assignments copied to your class directory on
pe15.cs.umb.edu
- You will find a link to your class directory in your Unix
home directory
- When you first connect to a Unix machine you will be in your home directory
- In todays Class Exercise you will create two directories in your class directory
- An hw directory for homework assignments
- An ex directory for Class
Exercises
- You must create a new directory under hw for each
homework assignment
- And you must create a new directory under ex or each
Class Exercise
- If a file is in the wrong directory or has the wrong name
you will get a 0 for the assignment
Working on Assignments
- Work on class assignments has two phases
- In the first phase you will write, test and debug the script on your machine
- In the second phase you will move the script to
pe15.cs.umb.edu
- You should also run your script on the Unix machine to make sure it works
Software for the Class
- You need different software for each phase
- For the first phase you need to download Python
- You find instructions
here
- Python comes with a program called IDLE
- You can use IDLE to run Python statements and create scripts
- For the second phase you need
- The SSH client allows you to log in to a Unix machine from your computer
- You will need it to test the assignments to make sure they run correctly
- If you have a Windows machine see
Connecting to a Unix machine from Windows
- If you have a Mac see
Connecting to a Unix machine from MacOS
- You will need FileZilla to copy your script to your class directory
- You will find instructions
here
Class Assistant
- Andrew Newman is an undergraduate IT student who can help you with
your assignments
- You can reach him at Andrew.Newman001@umb.edu
- You should contact the Class Assistant if you are having trouble submitting
your assignments
- I have asked the Class Assistant to schedule a Zoom seesion
each weekend to help you with any issues you might have
- You will find the details
here
New Material
Files
- In order to do anything in this course you have to know about about files
- A file contains data and is linear
- That means it has a beginning and and end
- Like a piece of string
- Files exist on hardware like hard disks, SSDs and thumb drives
- These devices are called long term storage
- They store the files you create ...
- so you can use them later
- Any file you create on such devices will not disappear ...
- unless you delete it
- You create a file whenever you take a picture, save a Word document or send an email
- All the information that computers use is contained in files
- The programs you write for this course will be stored in files
Directories (Folders)
- The hard drive on your computer probably has hundreds of files
- The Unix machine we will be using for this course has even more
- It is very important that files are well organized ...
- or you will not be able to use the data that you create
- To organize your files you need to create directories
- Which are also called folders
- The purpose of a directory is to hold files
- And other directories
- There is one special directory at the top
- On Unix machines this is called the root directory
- Every file on the machine is contained in this top directory ...
- or a subdirectory of the top directory ...
- or a sub-subdirectory
- And so on
- You can create as many directories as you want
- If you drew a map of all the directories on a machine
- It would look like a pyramid
Working With Files
- You will create a file containing Python code for each assignment
- Both Class Exercises and homework assignments
- You will create these files on your machine
- But you must remember where you put them
- If you are careless and forget where you put a file ...
- or put it in the wrong place ...
- all your hard work will be lost until you find it
- Files never disappear
- Unless you delete them
- If you can't find a file you created it isn't gone
- It's just in the wrong place
- Most computers these days have a Documents directory
- You should create a python directory inside Documents
- And save all your Python files there
- When your code runs properly you need to copy them to
pe15 ...
- which is a Unix machine on the CS network
- You will do this using a program called FileZilla
- But you must put them in the right directory on
pe15 ...
- or I will not be able to find them
Your Unix Course Directory
- You create your Python files on your machine
- But I can't see them there
- So you need to move them to the Unix machine
pe15 ...
- where I can see them
- But you need to put them in a particular directory ...
- or I won't know where to look
- There is a special directory on pe15
for this class
- Once you complete the Apply process you will have your own directory ...
- inside this class directory
- The directory name will be your Unix account name
- My Unix account is ghoffman
- I completed the Apply process
- So I have a ghoffman directory in the class 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
Downloading Python
- Go to
https://www.python.org/downloads/
and download the latest version of Python
- The version number should be greater than 3.5
- This will create a Python directory on your machine
- Inside this directory you will find an application named IDLE
Running IDLE
- Open IDLE
- You will see a window like this
- You are now running the Python interpreter
- The three > signs,
>>>, are a prompt
- It is the interpreter's way of saying it is ready for you ...
- to type a Python statement
- Let's write a Python statement that will print a simple message
- When you hit Enter or Return
- The interpreter will run the statement
- It has printed the text inside the parentheses ...
- and issued a new prompt ...
- to say it is ready for your next statement
- When you run IDLE you are talking directly to the Python interpreter
- This is called running Python in
interactive mode
Syntax Highlighting
- Notice that different parts of the window text appear in different colors
print
appears in purple
- That means
print
is function
- The "Hello world!" inside the parenthesis appears in green
- It is a string of characters
- The output of the print statement appears in blue
- Different words mean different things
- And they appear in different colors
- This is called
syntax highlighting
- It is a feature you will find in most text editors used by programmers
- Syntax highlighting can make it easier to find mistakes in your code
Creating a Python Script
- If you click and drag down to to "New File" you will get a new window
- Notice that there is no >>>
prompt in this window
- That's because you are not running the Python interpreter in this window
- Instead you are running the editor built into IDLE
- You can use this editor to create Python scripts
- Which are text files containing Python statements
- When you type Python statements inside this window ...
- the text will appear with the same highlighting colors ...
- as in the interpreter window
- But if you hit Enter or Return you just go down to the next line
- The Python statement is not executed like it was in the interpreter
- You can run the script you created
- But only if you first save the file
- To do this, go to the File menu and click and drag to down to Save
- This will bring up a dialog box
- But here you have to be careful
- You have to save it in the right directory ...
- or you will have a hard time finding it later ...
- when you need to copy it to the Unix machine
- I suggest you first create a python
directory inside your Documents directory
- Save all your scripts in this python
directory
- So you will know where to find them when you move them to
pe15.cs.umb.edu
- I have already created a python
directory
- So I will select that directory
- Now I have to give the script file a name
- And save it
- When I do you can see in the title of the editor window
- That the file has the name hello.py
- All Python files have to have a .py extension
- But I did not type it in when I named the file
- The editor automatically supplied the .py extension
Running a Script in IDLE
- Now that I have saved the file I can run it
- This is done inside the interpreter window
- Select "Run Module" from the Run menu
- The Python interpreter window now moves in front ...
- and the script is run inside the interpreter window
Changing a Python Script
- What if you want to change a script?
- The first thing you need to do is run IDLE
- This will bring up the interpreter window
- In the File menu click and drag down to Open
- Select the python directory
- Then select the file and double click
- The file will appear inside an editor window
- This windows allows you make any changes you need
Two Ways to Run Python
- There are two ways to use Python
- Interactively
- Writing Python scripts
- They are very different in the way they work
- And they are used for different purposes
- When you first open IDLE you see the interpreter window
- In this window you can type a Python statement ...
- and then hit Enter to run that statement
- You can only run one statement at a time
- This is called the interactive mode
- It is mostly used for learning Python ...
- and experimenting with features of the language
- But when you want to use Python to do some real work ...
- you will write a script
- The scripts can have as many statements as you need
- And when you run the script all statements are executed
- Scripts can be run inside IDLE
- But you can also run them at the command line
- And that is how they are usually run by IT people
Python in Class Exercises
- The first several Class Exercises have two parts
- First, I ask you to type Python statements directly into the IDLE
interpreter window
- This will give you a chance to see how they work in Python ...
- and give you immediate feedback when you make a mistake
- In the beginning you will make a lot of mistakes
- But you will get better as you go along
- This is good practice for you
- But I cannot see what you have done
- So I can't assign you a score
- That is why the second part asks you to create script ...
- and copy it to pe15.cs.umb.edu
- On pe15.cs.umb.edu I can see your script ...
- and give it a score
Running Python in Interactive Mode
- 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
- When writing code we often need to store a value that will be needed later
- To make this possible, 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
- If you make a mistake, simply type the command again ...
- and Hit Enter
Python on Unix
- Everything you can do in IDLE ...
- can be done on the Unix command line
- To run Python in interactive mode type
python3
and hit
Enter
$ python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 3
>>> b = 2
>>> a + b
5
>>>
- You leave interactive mode by entering
exit()
- You can run a Python script using the following format
python3 FILENAME
- So this is how to run hello.py
$ python3 hello.py
Hello world!
Scripts versus Interactive Mode
- In general, anything you can do in interactive mode you can also do
in a script
- And vice-versa
- But there are two important differences between them
- If you have defined a variable in an interactive session ...
- it will disappear the moment you leave
- The second difference involves printing values
- In interactive mode, once you have assigned a value to a variable ...
- you can print its value, just by typing the variable name
>>> a = 5
>>> a
5
- This does not work with scripts
Using FileZilla
- In order to move you Python scripts to
pe15.cs.umb.edu
you will need to use FileZilla
- 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
- Username
- Password
- Port
- Host is the domain name of the machine to which you are connecting
- We use pe15.cs.umb.edu
- In the next box, enter your Unix username
- The third box is for your password
- Enter 22 in the last box
- Then click the QuickConnect Button
- The first time you do this, you will see this
- Click Save Password and OK
- When you do you will see the following
- This is a security check
- The alert box is saying that this is the first time you have connected here
- Click Always Trust this Host to proceed
- But if you see this in the future something is terribly wrong ...
- and you had better contact the Class Assistant
- Normally you just click OK
- Then you should see something this
- The top pane shows the connection steps
- If there is a problem it will show up in red here
- The area below shows 4 panes
- Two on the left and two on the right
- The leftmost two panes show your machine
- The rightmost show the Unix machine
- In each set the topmost pane allows you to select a directory
- And the bottom-most shows you what is inside that directory
- You will be using FileZilla to move files from your machine on the left ...
- to the Unix machine on the right
- I need to find my it116 directory
- First I click on my home directory ghoffman
- The I scroll down until my it116 directory comes into view
- When I double_click on the it116 directory ...
- the two right hand panes change
- The bottom right pane shows my empty it116 directory
- If I right-click in this pane ...
- I will get a menu that allows me to create a directory
- I need to change "New directory" into the proper directory name
- Then click OK
- This will create the new directory
- Double-click on the ex directory
- To enter that directory
- And repeat the above procedure to create an ex2 directory
- Now we need to locate the file we want to copy
- First I double-click on my home directory glenn
- Then scroll down to my Documents directory
- In the bottom pane I can double-click on the python directory
- To see the ex2.py file
- I want to move to Unix
- Now I click and drag the ex2.py file
- To the ex2 directory on Unix
Today's Class Exercise
- The Class Exercises give you hands on experience with Python
- The exercise asks you to run Python in interactive mode
- And to create the ex2.py script
- You can do both on your own machine
- But the script must be copied to the correct directory the Unix machine
pe15.cs.umb.edu
- To do the Class Exercise for today you need to use your laptop
- You will use it to connect to pe15
- See the Class Exercise for details
- If you have not completed the Apply process you will not be able to log in
Class Exercise
Attendance