OIM3600 - Computer Science for Business Students

Session 10 (9/30)

contain

Today's Agenda

Welcome/News/Announcements

  • Class GitHub:
  • Communications:
    • Slack - for debugging, clarifications, real-time discussion, casual chat, etc.
    • Email - specify course # in subject title, e.g. "OIM3600: GitHub settings"
    • Meet with me in person during office hours at least once this semester.
  • Questions?

What we have learned so far

  • Code represent: letters, numbers, colors/images/video/audio
  • Scratch
  • Git/GitHub
  • Programming
    • Functions: arguments, side effects, return values
    • Conditionals
    • Boolean expressions
    • Loops
    • Variables

Quick Quiz - Basics

  1. What keyword should be used to include/use other modules or libraries?
  2. def f(x):
        print(x * 2)
       
    print(f(100))
    
  3. x = 100
    
    def f(): 
        x = 42
        return x * 2
       
    print(x, f())
    
  4. x = 123
    y = x
    x = 'abc'
    print(y)
    

Quick Quiz - Iterations

  1. What will be the last line of output?
    n = 6
    while n > 0:
        print(n)
        n = n - 2
    
  2. What if line 2 is changed to while n >= 0:?
  3. What if line 2 is changed to while n != 0:?
  4. What will be the last line of output?
    n = 5
    while n != 0:
        print(n)
        n = n - 2
    

Shoud I use for or while?

  • Use a for loop
    • when the number of iterations is known in advance.
    • when iterating over a specific range of integers.
    • when looping through elements in a collection or sequence (e.g., string, list, tuple, dictionary, set)
  • Use a while loop
    • when creating an infinite loop.
    • when the number of iterations is unknown and should continue until:
      • a specific condition becomes False.
      • or a break statement is encountered.

Homework / Coding Style

  • Issues on GitHub - Reply and/or Reply (with evidence) and Close.
  • Please follow the instructions.
  • File/folder names - use lowercase and underscore, i.e., cash/cash.py
  • Write comments / pseudo-code / docstrings
  • Grading Rubrics

Lectures

Python Tips - How to Debug

  • Format code first: Alt (⌥ option if macOS) + Shift + F in VSCode.
  • print()
  • PythonTutor.com
  • Debugging in VS Code
    1. Set breakpoints
    2. Run debugger: Press F5 to Start Debugging
      1. "Continue" ( F5 ): Keep running until you hit a stopping point.
      2. "Step Over" ( F10 ): Go through code line by line, skip functions.
      3. "Step Into" ( F11 ): Go inside functions and trace their lines.
    3. Inspect Variables: Use "VARIABLES" panel or "WATCH" panel.