Lab 1
Getting Started With Python and IDLE
Preliminaries
This lab consists of some exercises to help you become comfortable with the the basics of Python and the IDLE IDE.
Task 1: install the necessary software
In your work for this course, you will need to have access to the following programming tools:
- Python 3, including IDLE
The steps needed to obtain and install these tools depend on the operating system that you’re using. Select the appropriate link below:
- [Mac OS X users][mac]
- [Windows users][win]
- [Ubuntu Linux users][lin]
Task 2: explore IDLE
First, start the IDLE program. If you are using your own computer, you should have installed some version of Python 3 (see task 1). If you need help locating the IDLE program on your own computer, ask one of the teaching staff members to help you.
Let’s review the basics of using IDLE. The easiest way to do so is to download a Python file, open it in IDLE, and then run it in IDLE.
- Right-click on the following link, and then choose “Download File As” or “Save Link As” in order to download the file: lab1task2.py
- Save the file. If you’re using a lab computer, save it to the Z: drive.
- In IDLE, choose File > Open... (or use Ctrl + o” and locate the file on the Z: drive. Double-click on the file (or click “Open”) to open it in IDLE. This should open up another window on your screen, which we will refer to as the text editor. This is where we can write programs and save them.
- From within the text editor, press the F5 key to run the program in the Python Shell (it should execute in the other open IDLE window).
Note
When working on a machine in the lab, you should save all of your work on the Z: drive. This is a special directory maintained by the computer science department which is accessible from any computer to which you are logged on using your CS account (either in the teaching lab or in the undergraduate lab). If you do not save your work to the Z: drive, you will not be able to access your work again after you log out!
To make it easier to manage the browser (the guide to the lab), the text editor (where you are developing a program), and the Python Shell (where Python runs and displays results) on your computer, we recommend resizing and rearranging the windows in the following way:
This allows you to make the best use of all three windows simultaneously.
Task 3: modify IDLE’s settings
To facilitate your use of IDLE, we recommend that you take the following steps to change IDLE’s settings:
-
Click the Options menu, then click Configure IDLE. (On OS X, you would click the IDLE menu and then Preferences.)
-
In the resulting window, use the drop-down box to change the font size to at least 12 points.
-
In the same window, select the Keys tab.
-
In the section of the Keys tab called Custom Key Bindings, scroll down to find the line for history-next.
-
Click on the line for history-next and then click the button labeled Get New Keys for Selection. In the resulting window, scroll down to find and select Down Arrow. Then click OK.
-
When prompted, enter a name for your new Custom Key Set. You can just use your first name. Click OK.
-
Click on the line for history-previous and then click the button labeled Get New Keys for Selection. In the resulting window, scroll down to find and select Up Arrow. Then click OK.
Task 3.1: Changing IDLE’s font size
On some computers, IDLE’s default font size is too small.
In these cases, the _
character (the underscore) may be too
small or invisible. Since we will use the _
character very frequently
when writing Python code, you should change IDLE’s font size to at least 12
points.
On OS X:
-
Click the IDLE menu, then click Preferences....
-
In the resulting window, use the drop-down box to change the font size to at least 12 points.
On Windows:
-
Click the Options menu, then click Configure IDLE.
-
In the resulting window, use the drop-down box to change the font size to at least 12 points.
Task 4: understand and debug Python code
Create a new file in IDLE and name it lab1task4.txt. Put all answers that you write for this task in this file.
Let’s take another look at the program from Task 2.
-
What types of statements are included in this program?
-
In what order are the statements executed?
-
Explain in detail what happens when the following two statements are executed:
course = course + 1 print('maybe you will take CS', course, 'next semester')
-
Consider the following two lines of the program:
weeks = days // 7 print('that is approximately', weeks, 'weeks')
-
What is the difference between
weeks
and'weeks'
? -
Why does the program print 5 weeks instead of 6 or 5.7142857...?
-
How could we make the program print a more precise result for the number of weeks?
-
-
Copy and paste the following lines into the program, after all of the existing lines:
print() print('Go Terriers!") print('pi is approximately' (22 / 7)
-
Save the changes to the program using Ctrl-S. Then try to run it using F5. What happens?
-
The new lines include several bugs. See if you can find and fix them. Feel free to ask the TF or CA if you need help.