Skip to main content

Section 12.2 Week 1 Lab

Note 12.2.1. Material Covered.

Today’s lab focuses on getting Python and IDLE installed and working on your computer.

Subsection 12.2.1 Level 1 - Show IDLE is working on your computer

  1. Follow the instructions on UMLearn in Week 0 – Programming Resources. Then watch the IDLE introduction movie and go on to complete the next step.
  2. Launch IDLE:
    On a Mac, use spotlight (type command-space and then type IDLE) or find IDLE in your launchpad and double-click on it.
    On Windows, type IDLE in the Windows search bar.
  3. When IDLE opens, you are in the IDLE console. To make sure it is working, type the following: print(“Hello World”)
  4. Then in the upper left hand corner, click ‘File’, then ‘New File’ to open an empty file that you can then type, run, and save code.
  5. Now type the same code from Step 3 and then save this file with the name: test.py
    Pick a directory to save your .py files so in the future you can easily find them.
  6. When you open a new file, a ‘Run’ option should appear in your toolbar. Click it and then click ‘Run Module’.
  7. Show your instructor that you have IDLE installed and working on your computer to get credit for Part 1.

Note 12.2.2.

Integrated Development and Learning Environment (IDLE)
  1. IDLE is a simple programming environment that comes with any Python 3.x installation.
  2. It offers a good way to test out syntax and see how a certain Python command works.
  3. Typing code in IDLE is NOT the same thing as typing code in a file but it does offer the ability to create, edit and save Python programs as .py files.
  4. When you create a Python program and save it as a .py file, you should run it to test it.
  5. The main assignments for this course require the submission of .py files on UMLearn.

Subsection 12.2.2 Level 2 - Write a turtle program

Note 12.2.3.

Do NOT ever save one of your files as turtle.py because this action will overwrite the turtle module and your turtle-based programs will stop working.
  1. Open a new file in IDLE, then type in the following:
    import turtle
    wn = turtle.Screen()
    niki = turtle.Turtle()
    
  2. Notice that the color of “import” is orange. This is called syntax highlighting which IDLE does to show that it recognizes certain Python commands.
  3. Add a few turtle movements to the above code and then save this program as lab_test.py
  4. Now click Run -> Run Module (or type F5) to run your program. This will open a new window with title of Python Turtle Graphics.
  5. Notice the three dark red >>> in IDLE which indicates that the program ran and is finished with IDLE ready to run another program.
  6. Edit your program so that your turtle draws a simple picture of a house ⌂ and save this file as house.py
    If you get stuck, add the following code (with comments to describe what the code does):
    # draws bottom of house
    niki.right(90)
    niki.forward(128)
    niki.left(90)   
    niki.forward(128)
    niki.left(90)
    niki.forward(128)
    niki.left(90)
    niki.forward(128)
    
    # draws roof of house
    niki.right(135)
    niki.forward(90)
    niki.right(90)
    niki.forward(90)
    
  7. Add a comment block at the top of your file.
    ################################
    # First name  Last name
    # COMP 1000
    # Week 1 Lab, Part 2
    # Turtle house drawing 
    ################################
    
  8. Lastly, select ’Show Line Numbers’ under ’Options’ in the toolbar.
  9. Show your instructor that your turtle program draws a house to get credit for Part 2.

Note 12.2.4.

For windows operating system users: to associate .py files with IDLE, right click on the file and select “Properties” from the context menu and then the “Change” button where it says “Open with..”

Subsection 12.2.3 Level 3 - Type turtle program into IDLE

In this last part, you will type your turtle program directly into the IDLE console, one line at a time. This is a very different experience because IDLE interprets and executes each line after you hit ‘return’.
  1. In the IDLE console, type import turtle then press ’return’. Python imported the turtle module and is now ready to accept the next command (see the >>>).
  2. Next type wn = turtle.Screen() then press ’return’. Python creates and displays an empty turtle canvas and again the prompt >>> indicates that IDLE is ready for the next command.
  3. Now type niki = turtle.Turtle() and press ’return’. Python creates a turtle and displays it at the x,y coordinate (0,0) which is the center of the canvas. Again the >>> prompt appears.
  4. Now make the turtle move with niki.forward(100).
  5. Notice that text entered into IDLE can’t be edited after the line of code has been executed when you press ’return’. Test this out by trying to change a line of code that you entered.
  6. Let’s say you were writing a program and you didn’t remember how Python handles mathematical order of operations (for example, “4 + 8/2”). How is IDLE useful in this situation as compared to just typing the statement directly into your program file?
  7. Show your instructor your IDLE console to demonstrate that you have tried the above commands for Steps 1 to 4 to get credit for Part 3.
Congratulations you have completed your first lab writing Python code! 👏🎉🥳
You have attempted of activities on this page.