Tuesday, February 17, 2015

Notes on Python for Informatics Chapter 3: Conditional execution

Source

What is Conditional execution?

Conditional execution is a building block of programs. A condition determines if a part of the program will run at all.
  • conditional statement: A statement that controls the flow of execution depending on some condition.
  • condition: The boolean expression in a conditional statement that determines which branch is executed.
    • boolean expression: An expression whose value is either True or False.
    • branch: One of the alternative sequences of statements in a conditional statement. 

Types of conditional execution

The most simple conditional statement has only one branch:

if x > 0:
    print 'x is positive'
More complex versions are:
  • alternative execution: A conditional statement with two alternative branches.
if x%2 == 0 :
    print 'x is even'
else :
    print 'x is odd'
  • chained conditional: A conditional statement with a series of alternative branches. 
if x < y:
    print 'x is less than y'
elif x > y:
    print 'x is greater than y'
else:
    print 'x and y are equal'
  • nested conditional: A conditional statement that appears in one of the branches of another conditional statement. 
if x == y:
    print 'x and y are equal'
else:
    if x < y:
        print 'x is less than y'
    else:
        print 'x is greater than y'

What type of Conditions can be used?

  • comparison operator: One of the operators that compares its operands:
    • <        # strictly less than
    • <=      # less than or equal
    • >        # strictly greater than
    • >=      # greater than or equal
    • ==      # equal
    • !=       # not equal. (<> was used in old version)
  • logical operator: One of the operators that combines boolean expressions:
    •  x or y      # if x is false, then y, else x
      • This is a short-circuit operator, so it only evaluates the second argument if the first one is False.
    • x and y     # if x is false, then x, else y
      • This is a short-circuit operator, so it only evaluates the second argument if the first one is True.
    • not x    # if x is false, then True, else False
      • not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error.
    • short circuit: When Python is part-way through evaluating a logical expression and stops the evaluation because Python knows the final value for the expression without needing to evaluate the rest of the expression.
    • guardian pattern: Where we construct a logical expression with additional comparisons to take advantage of the short circuit behavior. 
      • i.e. if we want to avoid zero division, we could put a !=0 condition on the divisor.

Compound Statements 

  • compound statement: A statement that consists of a header and a body. The header ends with a colon (:). The body is indented relative to the header.
  • body: The sequence of statements within a compound statement.
Examples:
  • if statement is used for conditional execution
  • while statement is used for repeated execution
  • for statement is used for repeated execution
  • try statement is used forexception handling
Syntax:
  • Indenting and dedenting is a grammar rule of Python. 
  • A compound statement's body is always indented relative to the header. 
  • To set the end of the body, the code should be dedented. 
  • When indenting, whitespace should be used consequently: it is required to use only one type of whitespace within a single code file (4 spaces or one tab for one level of indentation).

Handling (catching) exceptions

It is possible to resolve foreseen errors with the try statement.
Unresolved errors make the program quit with a traceback massage (if it was run from a script file).
  • traceback: A list of the functions that are executing, printed when an exception occurs. 
inp = raw_input('Enter Fahrenheit Temperature:')
try:
    fahr = float(inp)
    cel = (fahr - 32.0) * 5.0 / 9.0
    print cel
except:
    print 'Please enter a number'

Reserving space in code structure for later usage

It is possible to leave parts of code to be written later by reserving the space in the code structure with the pass statement:
if x < 0 : 
    pass # need to handle negative values!

No comments:

Post a Comment