Boolean Conditions

A quick remainder of the definition of boolean; a condition that evaluates to true or false. Similar to previous posts, these conditions can be made more complexly adding operators such as “and”, “or” and “not”.

  • The “and” operator

This operator combines 2 arguments and evaluates as to true only if both of the statements are true – otherwise the overall combination of the statements will evaluate to false.

[Console]

>>> 1 == 1 and 2 == 2

>>> 3 == 3 and 4 == 5

>>> 6 != 6 and 7 == 7

>>> 8 > 9 and 9 < 8

=====

[Interpreter]

True

False

False

False

  • The “Or” operator

The “or” operator also takes two arguments however will evaluate to true if both or one of the statements rings true. Of course, if both statements are false then the statements will overall evaluate to false too

[Console]

>>> 1 == 1 and 2 == 2

>>> 3 == 3 and 4 == 5

>>> 6 != 6 and 7 == 7

>>> 8 > 9 and 9 < 8

=====

[Interpreter]

True

True

True

False

  • The “not” operator

The concept of this operator is very simply however when applied to real life scenarios it can take a little while to get the hang of it. The “not” operator is basically an operator that will invert the final evaluation of the statement. For example if a statement evaluates to true, by adding the not operator in the code it will cause the statement to evaluate to false. In addition, unlike the previous examples this operator does not account for 2 statements but actually only takes 1 into account.

[Console]

>>> not 1 == 1

>>> not 1 > 2

=====

[Interpreter]

False

True

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s