Class 8 - Notes

Upcoming Schedule

If you did not earn a Yellow Belt (by receiving a Gold Star on Project 1), see Yellow Belt Promotion for information on how to advance.

Before Wednesday's class you should have completed Udacity cs101 Lesson 3: How to Manage Data (Notes) and Lesson 3: Problem Set. It is not expected that you do the "three gold stars" problems (the last two problems in Problem Set), but if you do them successfully you will find Project 2 fairly easy.

Project 2 is posted now, and is due at the beginning of class on Monday, 15 February. It involves substantially more challenging programming than Project 1. Please don't wait to get started, and make sure to take advantage of available help.

Dave will not be able to hold his usually schedule office hours this Thursday morning, but will have office hours tomorrow (Tuesday, 3:30-4:30pm, Rice 507). Yuchi has office hours on Wednesday (4-5pm, Rice 514) and Friday (immediately after class).

Slides

[Download (PPTX)]

Code

Download code from class: class8.py

Functions and Procedures

What is a function in mathematics?

What is a function in Python?

Define a Python function that is not a mathematical function:

Project 1 Procedures

def is_brighter(color1, color2):
   if (get_red(color1) + get_blue(color1) + get_green(color1)
       > get_red(color2) + get_blue(color2) + get_green(color2)):
       return True
   else:
    return False

How can we make this function shorter (but still equivalent)?

(In programming) Is shorter always better?

Why should we write procedures to test our procedures?

Assertions. Assertions are a way to program defensively. The interpreter will evaluate the test following assert. If it evaluates to a true value, nothing happens. If not, the execution stops with an AssertionError.

def test_brighter():
   assert is_brighter(WHITE, RED)
   assert not is_brighter(RED, WHITE)
   # ...

Simplifying Code

if any_test: 
    return True
else:
    return False

Can the above code always be simplified? (What needs to be true for it to be equivalent?)