Write some code!

Now that you can experiment in the shell and run Python scripts in Wing, let't try a few more things.
  1. Try out some arithmetic operators
    1. We saw in class that // is the operator for integer division and that you can use % to get the remainder. For example, try 25 // 10 and 25 % 10 in the shell.
    2. What would be the value if the second number is larger? E.g. try 2 // 3 and 2 % 3.
    3. What happens if you divide by zero?
    4. In your hello.py script, add another line such as
      print((2 + 3) * 4)
      
      and try running it. Next, add a space at the beginning of that line and see what happens.
      • (Python relies on having statements lined up with the same indentation in order to correctly group them. This eliminates the need for special punctuation to group statements together.)
      Fix the indentation and run the code again.
  2. Try running your own code using the ActiveCode and CodeLens features of the textbook:
    1. Open the second chapter of the textbook scroll down to the second ActiveCode window (the one with Hello, World). Remember these pages may take 15 seconds to load at first. If you have the page open already, reload it (click the circular arrow in your browser toolbar).
    2. In Wing, highlight the text of your hello.py script and copy it using Ctrl-C or the Edit menu.
    3. Highlight the contents of the ActiveCode window and paste in your code in its place using Ctrl-V or the context menu (right-click).
    4. Run your code using the Run button.
    5. Now, press the Show CodeLens button. You should be able to scroll down and see your own code in the CodeLens window.
    6. Use the Forward and Backward buttons to step through the code.
      • The CodeLens feature in the textbook can be awkward for experimenting with your own code because you have to reload the page and re-paste the code in order to edit it. Instead, see the next step.
  3. Try running your own code in Python Tutor online:
    1. Go to the Python Tutor site.
    2. Click on "Start visualizing your code now"
    3. Select "Python 3.3" from the drop down menu
    4. Copy and paste your program
    5. Click "Visualize Execution"
    6. In the next screen use the Forward and Back buttons to execute the code
    7. Click on "Edit code" under the code window to modify the code again
  4. Try running a more interesting program in Python Tutor:
    1. Go to http://web.cs.iastate.edu/~smkautz/cs127f16/examples/week2/change.py. Download this file to your workspace, i.e., right-click, select Save As, and save it where you saved your hello.py script. Then open it in Wing.
    2. This is a completed script that we started writing in class on Monday for making change from a given amount of money in cents. Run it and see if it works. Then modify the initial value of amount and try it again.
    3. Next, copy and paste the code into the Python Tutor editor and step through the code. This is much more interesting than hello.py because you can watch the variables as they are created and updated.

Checkpoint 4

Show the TA that you can do everything described above and explain the results.