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:

  1. and, which checks if both the statements are True;
  2. or, which checks if at least one of the statements is True;
  3. 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:

  1. Anything shrouded in parenthesis () is evaluated first;
  2. not is evaluated next;
  3. and is evaluated next;
  4. 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"

Leave a Reply

Your email address will not be published. Required fields are marked *