a > b # a is greater than b
The above expression is an assertion that states “A is greater than B”. The expression has a truth value that can go either way… It is, or it isn’t. The expression by itself yields a boolean. This is to say the expression evaluates to a boolean (True or False).
When used in an if
statement, its truth value is assessed in order to determine whether to follow the truthy branch, or not.
if a > b:
# truthy branch
Above is an example of single-way branch, since there is no alternate branch to follow. This is the simplest multi-way branch:
if a > b:
# truthy branch
else:
# falsy branch (default)
Above we have described one of the many relationships that we can test our data points against. Think, “How does A relate to B?”
The tyipcal relationship operators we use in Python are,
==
!=
<
>
<=
>=
Notice that if there is an =
it is always on the right side of the pairing.
When measuring a value as being some amount or less, we look for the relationship that is less than or equal to.
a <= 2
We wouldn’t expect a weight of zero or less since we’ve just put a package on the scale. This means a
can be expected to be a number greater than zero (moot) anywhere up to and including 2 weight measure units (lb, kg, &c.).
Given there is a scale of weight ranges, it is very likely we will implement a multi-way branch that employs elif
which allows new conditions.
if a > b:
# truthy branch
elif a > c:
# truthy branch
elif a > d:
# truthy branch
else:
# default branch
Imagine things as being placed on the scale: If it is NOT less than or equal to 2 then it MUST be greater than 2.
If you mean the syntax highlighting, focus less on the colors and more on whether the code is correct.