Skip to Content
The Python Book
book

The Python Book

by Rob Mastrodomenico
January 2022
Beginner to intermediate
272 pages
6h 51m
English
Wiley
Content preview from The Python Book

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The Quick Python Book, Third Edition

The Quick Python Book, Third Edition

Naomi Ceder, David Fugate
Serious Python

Serious Python

Julien Danjou

Publisher Resources

ISBN: 9781119573319Purchase Link