The unofficial CircuitPython Reference

elif

description

Else... if or elif<condition>: in Python is an extension of if<condition>: statements. A series of elifs are condition tested in order from top to bottom.

When an elif<condition>: evaluates False the interpreter immediately tests the next elif<condition>: ignoring the indented code block below.

As soon as an elif<condition>: evaluates True, the indented code block immediately below it will execute and any remaining elif conditions will be ignored.

If all elif<condition>: statements evaluate False: the interpreter will either execute the indented code block below else: or if there is no else: present the interpreter will continue on without executing any code.

syntax

if condition:  
  
# if True, execute code in the indented block  
code block

elif condition:  
  # if True, execute code in the indented block  
 code block

methods

parameters

condition : any True/False conditional statement resulting from comparison of one or more values and any logical operators

code block : any block of code indented any amount of space or tab stops

returns

Executes code in the indented code block immediately below the elif<condition>: statement.

Only executes the indented code block under the first elif to evaluate True.

example code

serial output:

code.py output:

The value is between 6 and 10

see also: