Comparators
Comparators work with booleans to define a variable as either ‘True’ or ‘False’. For example:
Var1 = 5 == 5 Var2 = 10 < 5 print(Var1) print(Var2) True False
Comparators are listed below:
Equal to
5 == 5
Not equal to
5 != 8
Less than
5 < 10
Less than or equal to
5 <= 6
Greater than
5 > 2
Greater than or equal to
5 >= 5
Boolean Operators
Boolean operators compare statements and result in boolean values. There are three boolean operators:
and
, which checks if both the statements areTrue
;or
, which checks if at least one of the statements isTrue
;not
, which gives the opposite of the statement.
Booleans have an order of operation just like any with arithmetic operators. This is the order in which they function:
- Anything shrouded in parenthesis () is evaluated first;
not
is evaluated next;and
is evaluated next;or
is evaluated last.
If Statements
Standard if statement
if 8 * 10 == 80: print "It's true!"
If/else statement
if 5 * 10 ==40: print "It's 50!" else: print "Incorrect"
If/else/elif statement
if Var1 < 10: print "Less than 10" elif Var1 > 10: print "Greater than 10" elif Var1 == 10: print "Var1 is 10." else: print "You're out of control"