Learning Objectives:
- Students will install and run Python on their Computer
- Students will write and run a simple Python "Hello" program
- Students will submit a program to Gradescope
- Students will write programs that use the Turtle module to draw simple graphics
- Students will explore several Turtle commands
Software tools needed: web browser and Python programming environment.
Download the Skeletal Notes and Focus Questions to guide you while studying this lab.
These are a useful tool for note taking and you can keep these handy to study for and refer to during the final exam.
Using Blackboard
This course will use the on-line Blackboard system for course announcements and lecture previews. Blackboard should be accessible
through your CUNY First account (see Hunter ICIT Blackboard page for directions on using the system and how to get help).
The lecture previews can be found under the Content
menu (left hand side of Home screen).
Quizzes & Code Review
Each week in the 1001G lab, there is a
quiz.
Make sure to write legibly and include your name and EmpID, so that you get credit for the quiz.
You must also visit the 1001G lab to take a code review (oral walkthrough of a program you wrote) every week. Links to schedule appointments for quizzes and code reviews will be available on Blackboard.
See the
topics and deadlines for information about future quizzes and code reviews. The grades and feedback for quizzes and code reviews will be available on Gradescope.
Installing Python on Your Computer
If you need help setting up your personal computer you can go to tutoring in the G lab!
The Python programming language and IDLE environment are freely available
for many platforms from
python.org.
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.
Linux:
If you have a Linux machine (Ubuntu or Debian based), at a terminal window type the following commands:
sudo apt update
sudo apt -y upgrade
sudo apt install python3 python3-pip idle3
pip3 install numpy pandas matplotlib scipy folium image
sudo apt install spyder3
Windows:
If you have a Windows computer, you can install python3 with the following instructions:
- Go to the following link.
- Click latest Python 3 release.
- Scroll down to the files section.
- Click Windows x86-64 executable installer and save the file.
- Once it is finished downloading, run the installer.
- Make sure you click Add Python to PATH (This makes it a lot easier to install Python packages).
- Click install now.
- If asked do you want to allow this app to make changes to your device, click yes.
- Wait until you see the setup was successful screen and then you can hit close.
You have successfully installed Python on your computer. To open IDLE, search IDLE in the Windows search bar.
macOS:
If you have a Mac computer, you can install python3 with the following instructions:
- Go to the following link.
- Click latest Python 3 release.
- Scroll down to the files section.
- Click macOS 64-bit installer and save the file.
- Once it is finished downloading, run the installer.
- Hit continue, continue again, continue again, and then agree.
- Click install.
- Enter your password or use TouchID if prompted.
- Wait until you see the installation was completed successfully screen and then you can hit close.
- You can keep the install or move it to the trash.
You have successfully installed Python on your computer. To open IDLE, search IDLE in Spotlight search or find it in Launchpad. To open IDLE from terminal, type idle3.
Python Packages:
You can install the Python packages used in this course with the following instructions:
- If on Windows, search cmd in the Windows search bar and open it. If on macOS, search terminal in Spotlight search and open it.
- Install the Python packages by typing the following command:
pip3 install numpy pandas folium image matplotlib scipy
These installation instructions were written by Owen Kunhardt. You can find the full guides at the following links: Windows macOS
If you are unable to successfully install python on your computer or need to work on a tablet, there are many free on-line versions that you could use via a browser, such as replit.
There are many others you are welcome to experiment with; repl.it will support all packages used in this course.
Using Python
We will be using the IDLE programming environment for Python, since it is very simple and comes with all distributions of Python.
To launch IDLE:
If you are running Widnows, search IDLE in the Windows search bar.
- If you are running a Unix-like operating system such as a Linux distribution or MacOS, locate the terminal on your machine (you can search Spotlight for 'terminal' on a mac).
- In the terminal window, type:
idle
(followed by an enter/return).
- A new window will launch for IDLE.
- To see that it works, type at the IDLE prompt:
print("Hello, World!")
Write and submit your first program.:
Instead of using the shell window (where we can try things immediately), let's use a text window, where we can save our program for later and submit it to Gradescope (this is the basis of the first program).
- First, open up a text window: on the menu bar, choose "File" and from that menu, choose "New File".
- In that window, type:
#Name: ...your name here...
#Email: ...your email here...
#Date: January 27, 2024
#This program prints: Hello, World!
print("Hello, World!")
- Save the program (using the "Save" under the "File" menu). When you save it, name it something that you will be remember for the future and end it in .py. For example, ps1.py.
- Run your program (using the "Run Module" from the "Run" menu).
- If it prints "Hello, World!" to the screen, then log into Gradescope (see notes below):
- On the left hand menu, choose "Assignments".
- From the list, choose "1. Hello, World!".
In the file upload, drag over the .py file you just created and ran, and
- click "Submit".
Using Gradescope
This course will use the on-line Gradescope system for submitting
work electronically.
- Open your email and click the links to set up your account.
- Once you have your account, click on the "Assignments" menu (left hand side of window), and then choose the assignment "1. Hello, World!"
- Drag and drop your program onto the submit box.
- Click on confirmation button to submit your program. You will see a pop-up box confirming that the program was uploaded.
- In 30 to 60 seconds, the Gradescope page will have your results (remember to put your name on the top of the file for full credit!).
- Each program can be submitted multiple times up to the deadline.
More Python: Turtles
Now that you have just submitted your first program, let's try some other Python commands. Here's a quick demo (click the triangle to run the program):
A quick overview of the above 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 thomasH = turtle.Turtle() creates a turtle object called thomasH (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:
- thomasH.forward(150) moves thomasH forward 150 steps.
- thomasH.right(90) turns thomasH to the right 90 degrees.
Let's write the same program in IDLE:
- Open up a new file window in IDLE ("File > New File").
- Type (or copy) into your window:
import turtle
thomasH = turtle.Turtle()
for i in range(4):
thomasH.forward(150)
thomasH.left(90)
- Note: if you are working with IDLE, you may want to setup the screen to exit on click, so the graphics window does not stall:
import turtle
wn = turtle.Screen()
thomasH = turtle.Turtle()
for i in range(4):
thomasH.forward(150)
thomasH.left(90)
wn.exitonclick()
- Save your program ("File > Save").
IMPORTANT: 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).
- Modify your program so that it draws an octagon.
- Test your program and modify until you have a octagon (Hint: you need to modify the number of repetitions --number in parenthesis after range-- and the angle --number in parenthesis after left--). Play around with these numbers, run your modified program, observe and adjust until it draws a octagon. When it does, add comments at the top of your program:
#Name: ...your name here...
#Email: ...your email here...
#Date: February 2, 2024
#This program draws an octagon.
Run your program after editing to make sure you do not have any typos.
- Log into Gradescope (see notes above). On the left hand menu, choose "Assignments". From the list, choose "2. Octogon".
In the file upload, drag the .py file you just created and ran, and click "Submit".
More Turtle Commands
To review, we introduced the turtle commands:
as well as importing the turtle package (
import turtle) and creating ("instantiating") a turtle (
thomasH = turtle.Turtle()).
There are many more turtles commands. Over the next couple of classes, we will use those in the turtle chapter from the textbook. In addition to the ones that control movement, you can also change the color of the turtle, change the size of the drawing pen, and change the backbround color (by making a window or screen object, and changing it's color):
A complete list of turtle commands is part of the Python 3 documentation.
What's Next?
You can start working on this week's programming assignments. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.
Keep in mind that the due dates are one week late for flexibility (if one week there is a setback and you can't submit your programs, you will have time catch up).
Still, each week you should work on the programming assignments for that week, even if they are due a week later. If you are on a roll, you are welcome to work ahead!!!