Conditional statements allow to execute logic based on a logic.If is used for defining conditional statements in Python.
It is defined using the keywords:
- if
- elif
- else
Following is a sample if statement
if True: print('this will print') elif False: print('this will print') else: print('this will not print')
notice that we have used indentation for nesting the blocks in the if and elif blocks
we normally use conditional expressions using operators in the if statement for comparison as:
x=1 if x==1: print('this will print') elif x==2: print('this will print') else: print('this will not print')
Commonly used Python conditional operators are:
- == equality
- != not equal
- > greater than
- < less than
- >= greater than equal to
- <= less than equal to
there are logical operators
- and
- or
- not
Leave a Reply