Selection in programmingif…else in Python

When designing programs, there are often points where a decision must be made. This decision is known as selection, and is implemented in programming using IF statements.

Part ofComputer ScienceProgramming

if…else in Python

For , uses the if and else (note the lowercase syntax that Python uses):

Consider the age-related using Python. The steps are:

  • Ask how old you are
  • if you are 70 or older, say “You are aged to perfection!”
  • else say “You are a spring chicken!”

This algorithm would be written in Python (3.x) as:

age = int(input("How old are you?"))
if age >= 70: print("You are aged to perfection!")
else: print("You are a spring chicken!")

The program examines the value of age. If the inputted age is 70 or over, the prints one message. Otherwise (else) it prints another.