5Operators

The previous chapter introduced data types and some basic operators but in this chapter we build on this by introducing a number of key operators that are important to understand. As shown in our introductory examples in the previous chapter, we can define a variable as follows:

image

Here, we are assigning the variable x with the result of 1 + 2 so x is 3. Now, if we wanted to see if the value of x was equal to 3 we would use == which refers to equality.

image

We have shown how to test for equality but what about inequality. Well Python has that sorted as well, instead of using == we use != which is not equals. Using the example from before we get the following:

image

What we have here is the result of an equality statement like this being of type boolean (True or False).

We can also test to see if something is greater or less than another element.

image

Here we have introduced the following tests which takes the value on the left against the value on the right and tests for

  • greater-than for greater ...

Get The Python Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.