3.1.7. Conditional Sentences Semantics of Conditional Sentences

  • Conditional Sentences, like functions affect the flow of control

  • Conditional Sentences affect the flow of control by values Syntax of Conditional Sentences

  • Conditional Sentences are compound sentences consisting of one or more clauses headed by the keywords:

    • if

    • elif

    • else

  • Each if or elif clause is followed by a Logical Expression and a colon :

  • If the Logical Expression for a clause is True the body of the clause is executed

  • The body must be indented by 4 spaces (or a tab) Examples of Conditional Sentences

def greet(friend, money):        # Function has two predicates
    if friend and (money > 20):
        print "Hi!"
        money -= 20
    elif friend:
        print "Hello!"
        print "Ha ha!"
        money += 10
    return money

money = 15

money = greet(True, money)
print "Money:", money
print ""

money = greet(False, money)
print "Money:", money
print ""

money = greet(True, money)
print "Money:", money
print ""
Money: 15

Ha ha!
Money: 25

Money: 5
# Conditionals Examples

# Return True if year is a leap year, False otherwise

def is_leap_year(year):          # Testing a predicate - convention to use is_function_name
    if(year % 400) == 0:
        return True
    elif (year % 100) == 0:
        return False
    elif (year % 4) == 0:
        return True
        return False

year = 2010
leap_year = is_leap_year(year)

if leap_year:
    print year, "is a leap year"
    print year, "is not a leap year"
2010 is not a leap year