Scripting > Scripting Basics


Conditional Expressions

Conditional expressions have the form:

 

if [not] condition :

  if-group

[elif [not] condition :

  elif-group]

[else :

  else-group]

 

The “:” (colon) character must be placed after each condition.

The condition in each test is an expression built from one or more simple conditions using the form:

 

simple-condition ( and | or ) [not] simple-condition

 

Parentheses can be used to group conditions.

The simple-condition in each expression is either an expression using one of the conditional operators mentioned before or is of the form:

 

item [not] in sequence

 

Example: Conditional Expressions


if (x < y or y >= z) and string_1.index(“debug”) != -1 :

    # do something

    …

elif z not in value_list or (x < z * 2.5) :

    # do something different

    …

else :

    # do something else

 

If the statement group to be processed upon a condition is a single statement, that statement may follow the condition on the same line (after the colon character).

 

Example: Simple Conditional Expressions


if x1 < x2 : xMax = x2

else          : xMax = x1