The unofficial CircuitPython Reference

if

description

If is a basic control structure found in many programming languages. It allows for code to execute on a conditional basis using comparison operators to generate True/False statements. When the condition in an if statement is True the code in the block below is executed; when False, it is ignored.

syntax

if condition:  
  
# 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 if statement.

example code

serial output:

code.py output:

Hello World
Variable is less than 100
Variable is between 1 and 100
Variable is either 10 or 50
Variable is exactly 10

see also: