Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Conditions (What If?) 83

The operator that tests to see if two values are not equal is !=,
an exclamation point followed by the equal sign. This combination
may be easier to remember if you say “not equal to” when you see
!= in a statement. For example, you might read if x != 5 aloud as
“if x is not equal to five.”
The result of a test involving a conditional operator is one of
the Boolean values, True or False. Go to the Python shell and try
entering some of the expressions shown in Figure 5-3. Python will
respond with either True or False.


Figure 5-3: Testing conditional expressions
in the Python shell


We start by going to the shell and entering x = 5 to create a
variable called x that holds the value 5. On the second line, we
check the value of x by typing it by itself, and the shell responds
with its value, 5. Our first conditional expression is x > 2, or “x is
greater than two.” Python responds with True because 5 is greater
than 2. Our next expression, x < 2 (“x is less than two”), is false
when x is equal to 5 , so Python returns False. The remaining con-
ditionals use the <= (less than or equal to), >= (greater than or
equal to), == (is equal to), and != (not equal to) operators.
Every conditional expression will evaluate to either True or
False in Python. Those are the only two Boolean values, and the
capital T in True and capital F in False are required. True and False

Free download pdf