Conditional Logic

A special expression that evaluates to either the value True or False is called Boolean expression. Boolean expressions are also known as conditions. The following boolean expression determines whether 7 is greater than 4 and whether 7 is less than 4:

7 > 4
7 < 4

True and False are keywords—words that Python reserves for expressing a specific concept in code. Using a keyword as an identifier (i.e. the name of a variable) causes a SyntaxError.

It is also important to notice that True and False are each capitalized and are not contained within quotation marks; they are not string values! You’ll often create conditions using the comparison operators in the table found below:

Table of operators


Operators >, <, >= and <= all have the same precedence. Operators == and != both have the same precedence, which is lower than that of >, <, >= and <=. A syntax error occurs when any of the operators ==, !=, >= and <= contains spaces between its pair of symbols, like so:

# Do not put spaces in between > and =!
3 > = 2

Another syntax error occurs if you reverse the symbols in the operators !=, >= and <= (by writing them as =!, => and =<).