Book Image

The Python Apprentice

By : Robert Smallshire, Austin Bingham
Book Image

The Python Apprentice

By: Robert Smallshire, Austin Bingham

Overview of this book

Experienced programmers want to know how to enhance their craft and we want to help them start as apprentices with Python. We know that before mastering Python you need to learn the culture and the tools to become a productive member of any Python project. Our goal with this book is to give you a practical and thorough introduction to Python programming, providing you with the insight and technical craftsmanship you need to be a productive member of any Python project. Python is a big language, and it’s not our intention with this book to cover everything there is to know. We just want to make sure that you, as the developer, know the tools, basic idioms and of course the ins and outs of the language, the standard library and other modules to be able to jump into most projects.
Table of Contents (21 chapters)
Title Page
Credits
About the Authors
www.PacktPub.com
Customer Feedback
Preface
12
Afterword – Just the Beginning

Relational operators


Boolean values are commonly produced by Python’s relational operators which can be used for comparing objects. Two of the most widely used relational operators are Python's equality and inequality tests, which actually test for equivalence or inequivalence of values. That is, two objects are equivalent if one could use used in place of the other. We'll learn more about the notion of object equivalence later in the book. For now, we'll compare simple integers.

Let's start by assigning — or binding — a value to a variable g:

>>> g = 20

We test for equality with == as shown in the following command:

>>> g == 20
True
>>> g == 13
False

For inequality we use !=:

>>> g != 20
False
>>> g != 13
True

 

Rich comparison operators

We can also compare the order of quantities using the rich comparison operators. Use < to determine if the first argument is less than the second:

>>> g < 30
True

Likewise, use > to determine if the first is greater than the second:

>>> g > 30
False

You can test less-than or equal-to with <=:

>>> g <= 20
True

We can use the greater-than or equal-to with >= ,shown as follows:

>>> g >= 20
True

If you have experience with relational operators from other languages, then Python's operators are probably not surprising at all. Just remember that these operators are comparing equivalence, not identity, a distinction we'll cover in detail in coming chapters.