bembry.org
Home / Technology / Python / Notes

Lesson 7 Notes: Conditionals --The Power of “If …”

  • Conditionals
    • Test if a “condition” is true
      • Did the user type in the right word?
      • Is the number larger than 10?
    • The result of the test decides what happens
      • All numbers larger than 100, subtract 20
      • When correct word is entered, print “yeah!”
  • Conditional Tests
    • x= = y
      x is equal to y
    • x != y
      x is not equal to y
    • x > y
      x is greater than y
    • x< y
      x is less than y
    • x >= y
      x is greater than or equal to y
    • x<=y
      x is less than or equal to y
  • Examples of Conditional Tests
    • if x == 125:
    • if password == “nix”:
    • if num >= 0:
    • if letter > “L”:
    • if num/2 == (num1-num):
    • if num%5 != 0:
  • Analysis of an “If” statement
      if condition:
          Statements
    • if = keyword
    • Condition = a conditional test (x<y, x==y, etc.)
    • : = Required at end of “if” line
    • Statements = instructions to follow if the condition is true. These instructions must be indented.
  • Example: Simple “If”
      num = input(“Enter your age”)
      if num >= 30:
          old_person(num)
      print
      print "Thank you for your input"
  • If … else
      if condition:
          statements
      else:
          statements
    • “else” must be preceded by an if test.
    • “else” will run only when condition of if statement is not met.
    • Use this construction for two branch with only two choices
  • Example: “If … else”
      if x%2 == 0:
          print “The number is even”
      else:
          print “The number is odd”
  • If … elif … else
      if condition:
          Statements
      elif condition:
          Statements
      else:
          statements
    • For more than two options, use “elif”
    • “elif” is short for “else if”
    • “else” statement will run if others fail
  • Example: “If … elif … else”

      if x < 0:
          print x, “is negative”
      elif x == 0:
          print x, “is zero”.
      elif x > 0:
          print x, “is positive”
      else:
            print “Error.”, x, “is not a number.”
  • Power Keyword: pass
    • Pass
    • A “do nothing” command
      if x== 0:
            Do_something()
      else:
            pass
  • Power Keyword: return
    • Return
    • “Exit the function” command
      Def function(num):
          if num < 100:
              print “you must enter a number above 100”.
              return
          do_something()
  • Odd Notes
    • “if” tests are especially useful for catching and handling errors. Use "else" to catch errors when no conditions are met.
    • Conditional tests can be nested, but this can get confusing and should be used sparingly.
  • Review
    • What is a conditional statement?
    • What is the difference between "else" and "elif"?
    • What command exits a function?
    • What command means "do nothing"?
    • How can "if" test be used to catch errors?
    • What code is used to test for equality? Non-equality (not equal)?
Restricted access