Control Flow

If_else determines the logic flow of the program
  • The structure of the if-else statement has the form:

if (condition1 is true):
  do A
elif (condition2 is true):
  do B
else:
  do C
  • example 1: grading test scores

In [1]: grade=None

In [2]: score=86

In [3]: if (score>93):
   ...:     grade='A'
   ...: elif (score>85):
   ...:     grade='B'
   ...: else:
            grade='C'    

In [4]: grade
Out[4]: 'B'
  • example 2: palindrome

In [8]: seq1="ACTCA"

In [9]: if seq1==seq1[::-1]:
   ...:     print("%s is a palindrome!" % seq1)
   ...:     
ACTCA is a palindrome!

Last updated

Was this helpful?