Lab 1
Programming for Data Analysis
Lehman College, City University of New York
Spring 2017
Today's lab will focus on using the Blackboard system and simple programs
in Python.
Software tools needed: web browser and Python IDLE programming
environment.
Using Blackboard
This course will use the on-line Blackboard system for submitting
work electronically. Blackboard should be accessible
through your CUNY portal account (http://portal.cuny.edu).
If you have not used Blackboard in the past, information is available at the Computer Center
(http://www.lehman.edu/itr/blackboard.php).
They can set up your account and have information sheets about using the system.
We will be using Blackboard for in-class quizzes, submitting programs, and
posting grades. Quizzes and submitting programs are done via the Content
menu (left hand side of Home screen).
Chrome: There were known bugs using the previous version of Blackboard with the Chrome browser. In particular, the Chrome browser often would freeze during quizzes. These have reportedly been fixed in the new version.
Timing Out: If the system times out and locks your attempt (happens rarely
when the browser or PC crashes), send email to an instructor so they
can clear the attempt so you can try again.
Using Python
We will be using the IDLE programming environment for Python, since it is very simple and comes with all distributions of Python (if you would prefer to use another programming environment, both Spyder and Jupyter/iPython, are on the lab machines).
There are several ways to launch IDLE:
- On some machines, the IDLE icon has been saved to the tool bar. Click the icon, and the program will launch.
- Click simultaneously on COMMAND and SPACE (the key with the apple or flower on it, along with the space bar). This will launch Spotlight, the built-in searching feature. Type idle3, and it will find and launch idle for you.
- Open a terminal window (a window that allows you to type commands), and type idle3 (or idle on some systems) at the prompt.
To see that it works, type at the prompt:
print("Hello, world!")
Instead of using
the shell window (which lets us try things immediately), let's use a text window, where
we can save our program for later and submit it to Blackboard (this is the basis of the first program, due on Friday).
- First, open up a text window: on the menu bar, choose "File" and from that menu, choose "New File" (you can also type "⌘N").
- In that window, type:
#Name: ...your name here...
#Date: February 3, 2017
#This program prints: hello world
print("Hello world")
- Save the program (using the "Save" under the "File" menu or "⌘S"). When you save it, name it something that you will be remember for the future and end it in .py. For example, ps1.py. At the end of lab, save your programs to a USB drive, DropBox, or mail them to yourself.
- Run your program (using the "Run Module" from the "Run" menu or F5 key).
- If it prints "Hello World" to the screen, then log into Blackboard (see notes above). On the left hand menu, choose "Programming Problems". From the list, choose "Program #1".
In the file upload, enter the name of the .py file you just created and ran, and click "Submit".
More Python: Turtles
Now that you have just submitted your first program, let's try some other Python commands:
- Open up a new file window in IDLE ("File > New File" or "⌘N").
- Type (or copy) into your window:
import turtle
tia = turtle.Turtle()
for i in range(4):
tia.forward(150)
tia.right(90)
- Save your program ("File > Save" or "⌘S").
Note: Choose a name for your file that is not turtle.py.
When executing the "import turtle" statement, the computer first looks in the folder where the file is saved for the turtle module and then in the libraries (and other places on the path). So, it thinks the module is itself, causing all kinds of errors. To avoid this, name your program something like "myTurtle.py" or "program2.py".
- Run your program (using the "Run Module" from the "Run" menu or F5 key).
- Change your program so that it draws a hexagon (6-sided polygon).
- Test your program and modify until you have a hexagon. When you do, add comments at the top of your program:
#Name: ...your name here...
#Date: February 3, 2017
#This program draws a hexagon.
Run your program after editing to make sure you do not have any typos.
- Log into Blackboard (see notes above). On the left hand menu, choose "Programming Problems". From the list, choose "Program #2".
In the file upload, enter the name of the .py file you just created and ran, and click "Submit".
A quick overview of the parts of your second program:
- Lines that begin with # are ignored by Python-- they are comments for you to remember what you did and others to follow what's going on.
- The line import turtle loads in the built-in turtle graphics drawing package. It's part of all versions of Python but, to keep programs small, is not included unless you explicitly import it. In addition to built-in packages, there are many others that have been written to make Python more useful. We will use both kinds as the semester progresses.
- The line tia = turtle.Turtle() creates a turtle object called tia (you can call your turtles almost any combination of letters (and underscores and numbers)-- we used a name starting with "t" since turtle starts with "t").
- The next line: for i in range(4): is the first part of a for-loop that will repeat the commands indented beneath it 4 times.
- The turtle class has many functions that you can use for your turtle. The next two lines demonstrate two of them:
- tia.forward(150) moves tia forward 150 steps.
- tia.right(90) turns tia to the right 90 degrees.
Here's a list of more turtle commands and the turtle chapter from the textbook.
In-class Quiz
During lab, there is a quiz on the academic integrity policy of City University of New York. The password to access the quiz will be given during lab.
What's Next?
If you finish the lab early, now is a great time to get a head start on the programming problems due early next week. There's instructors to help you and you already have Python up and running. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.
Using Python on Your Computer
The Python programming language and IDLE environment are freely available
for many platforms from
python.org or
Anaconda.
For this class, we are using
Python 3. Many features of the language
(including the syntax of
print statements) changed between the second and third
version, so, you must use the Python 3 for submitting programs.