Condition

If else

Python

 

If

Python 2.7
status = 0

if status == 0:
    print("Ready")

The code block (indented code) will run if the condition specified is met.

Comparison Expressions and operators can be combined to form new condition expressions:

status=0; isDeviceOn=True

if isDeviceOn and (status == 0 or status == 1):
  print('Not done yet')

This will print Ready on Console.

 

If Else

Python 2.7
status = 1

if status == 0:
    print("Ready")
else:
    print("Running or Done")

By using else, you can specify a portion of code to be run, in case if expression evaluates to False.

This will print Running or Done on Console.

 

Complex If

Python 2.7
status = 2

if status == 0:
    print("Ready")
elif status == 1:
    print("Running")
else:
    print("Done")

You can have as many elif as you want.

This will print Done on Console.

 

Nested If

Python 2.7
status = 2

if status > 0:
    if status == 1:
        print("Running")
    else:
        print("Done")
else:
    print("Ready")

You can nest if statements to control the logic of your algorithms.

This will print Done on Console.

 

Single If Statement

Python 2.7
status = 1

if status == 0: print("Ready")
else: print("Running or Done")

If you only have one statement in your blocks or multiple statements separated by a semicolon (;) in one line, you can have them on the same line as the block header.

This will print Running or Done on Console.