Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Python Apprentice

You're reading from   The Python Apprentice Introduction to the Python Programming Language

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781788293181
Length 352 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Austin Bingham Austin Bingham
Author Profile Icon Austin Bingham
Austin Bingham
Robert Smallshire Robert Smallshire
Author Profile Icon Robert Smallshire
Robert Smallshire
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Getting started FREE CHAPTER 2. Strings and Collections 3. Modularity 4. Built-in types and the object model 5. Exploring Built-in Collection types 6. Exceptions 7. Comprehensions, iterables, and generators 8. Defining new types with classes 9. Files and Resource Management 10. Unit testing with the Python standard library 11. Debugging with PDB 12. Afterword – Just the Beginning
13. Virtual Environments 14. Packaging and Distribution 15. Installing Third-Party Packages

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.

You have been reading a chapter from
The Python Apprentice
Published in: Jun 2017
Publisher: Packt
ISBN-13: 9781788293181
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime