Class 3: Control Flow

From Colettapedia
Jump to navigation Jump to search

Boolean expressions

  • True False - both keywords
  • not
  • < == != > <= >=
  • priority - order of operations
  • “short-­‐circuit" operator - bail out, I've seen enough
  • any(), all()

Conditional

  • simple conditional
  • one-alternitive conditional
  • multi-test conditional
  • pass
  • ternary operator

While loop

  • while is a keyword
  • while with a counter
  • loop forever - break
  • use of the type()

For Loop

  • break to exit for loop early
  • continue statement
  • nested for loops
  • else clause belongs to for loop

iteration

  • range(len), or range(start, finish)
  • for-in
for item in L:
  print item
  • enumerate
for index, item in L:
  print index, item
  • if need only index
for index in range(len(L)):
  print index

Exception Handling

  • pyhton docs
  • Offer a way to fail gracefully
  • finally is for cleanup actions.
  • Handle two kinds of exceptions with one except: except (IDontLIkeYouException, YouAreBeingMeanException) as e:
  • "enclose risky in try, what do to is doesn't work in except, what do to if it does work in else, and finally always happens regardless of whether it works or not"
  • "In python, using exceptions for control flow is common and normal" - this stack overflow page