Snake_Byte[2]: Comparisons and Equality

Contrariwise, continued Tweedledee, if it was so, it might be, and if it were so, it would be; but as it isn’t, it ain’t. That’s logic!

Algebra, trigonometry and mathematical logic lessons by Janetvr | Fiverr
It’s all rational isn’t it?

First, i trust everyone is safe.

Second, i am going to be pushing a blog out every Wednesday called Snake_Bytes.  This is the second one hot off the press.  Snake as in Python and Bytes as in well you get it. Yes, it is a bad pun but hey most are bad. 

i will pick one of the myriads of python based books i have in my library and randomly open it to a page.  No matter how basic or advanced i will start from there and i will create a short concise blog on said subject.  For some possibly many the content will be rather pedantic for others i hope you gain a little insight.  As a former professor told me “to know a subject in many ways is to know it well.”  Just like martial arts or music performing the basics hopefully makes everything else effortless at some point.

Ok so in today’s installment we have Comparison and Equality.

I suppose more philosophically what is the Truth?

All Python objects at some level respond to some form of comparisons such as a test for equality or a magnitude comparison or even binary TRUE and FALSE.

For all comparisons in Python, the language traverses all parts of compound objects until a result can be ascertained and this includes nested objects and data structures.  The traversal for data structures is applied recursively from left to right.  

So let us jump into some simple snippets there starting with lists objects.  

List objects compare all of their components automatically.

%system #command line majik in Jupyterlab
# same value with unique objects
A1 = [2, (‘b’, 3)] 
A2 = [2, (‘b’, 3)]

#Are they equivalent?  Same Objects?
A1 == A2, A1 is A2
(True, False)

 So what happened here?  A1 and A2 are assigned lists which in fact are equivalent but distinct objects.  

So for comparisons how does that work?

  •  The ==  tests value equivalence

Python recursively tests nested comparisons until a result is ascertained.

  • The is operator tests object identity

Python tests whether the two are really the same object and live at the same address in memory.

So let’s compare some strings, shall we?

StringThing1 = "water"
StringThing2 = "water"
StringThing1 == StringThing2, StringThing1 is StringThing2
(True, True)

Ok, what just happened?  We need to be very careful here and i have seen this cause some really ugly bugs when performing long-chained regex stuff with health data.  Python internally caches and reuses some strings as an optimization technique.  Here there is really just a single string ‘water’ in memory shared by S1, S2 thus the identity operator evaluates to True.

The workaround is thus:

StringThing1 = "i wish you water"
StringThing2 = "i wish you water"
StringThing1 == StringThing2,StringThing1 is StringThing2
(True, False)

Given the logic of this lets see how we have conditional logic comparisons.

I believe Python 2.5 introduced ternary operators.  Once again interesting word:

Ternary operators ternary means composed of three parts or three as a base.

The operators are the fabled if/else you see in almost all programming languages.

Whentrue if condition else whenfalse

The condition is evaluated first.  If condition is true the result is whentrue; otherwise the result is whenfalse.  Only one of the two subexpressions whentrue and whenfalse evaluates depending on the truth value of condition.

Stylistically you want to palace parentheses around the whole expression.

Example of operator this was taken directly out the Python docs with a slight change as i thought it was funny:

is_nice = True
state = "nice" if is_nice else "ain’t nice"

Which also shows how Python treats True and False.

In most programming languages an integer 0 is FALSE and an integer 1 is TRUE.

However, Python looks at an empty data structure as False.  True and False as illustrated above are inherent properties of every object in Python.

So in general Python compares types as follows:

  • Numbers are compared by the relative magnitude
  • Non-numeric mixed types comparisons where ( 3 < ‘water’) doesn’t fly in Python 3.0  However they are allowed in Python 2.6 where they use a fixed arbitrary rule.  Same with sorts non-numeric mixed type collections cannot be sorted in Python 3.0
  • Strings are compared lexicographically (ok cool word what does it mean?). Iin mathematics, the lexicographic or lexicographical order is a generalization of the alphabetical order of the dictionaries to sequences of ordered symbols or, more generally, of elements of a totally ordered set. In other words like a dictionary. Character by character where (“abc” < “ac”)
  • Lists and tuples are compared component by component left to right
  • Dictionaries are compared as equal if their sorted (key, value) lists are equal.  However relative magnitude comparisons are not supported in Python 3.0

With structured objects as one would think the comparison happens as though you had written the objects as literal and compared all the components one at a time left to right.  

Further, you can chain the comparisons such as:

a < b <= c < d

Which functionally is the same thing as:

a < b and b <= c and c < d

The chain form is more compact and more readable and evaluates each subexpression once at the most.

Being that most reading this should be using Python 3.0 a couple of words on dictionaries per the last commentary.  In Python 2.6 dictionaries supported magnitude comparisons as though you were comparing (key,value) lists.

In Python 3.0 magnitude comparisons for dictionaries are removed because they incur too much overhead when performing equality computations.  Python 3.0 from what i can gather uses an in-optimized scheme for equality comparisons.  So you write loops or compare them manually.  Once again no free lunch. The documentation can be found here: Ordering Comparisons in Python 3.0.

One last thing.  There is a special object called None.  It’s a special data type in Python in fact i think the only special data type.  None is equivalent to a Null pointer in C.  

This comes in handy if your list size is not known:

MyList = [None] * 50
Print (MyList)
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

The output makes me think of a Monty Python skit. See what I did there? While the comparison to a NULL pointer is correct the way in which it allocates memory and doesn’t limit the size of the list it allocates presets an initial size to allow for future indexing assignments. In this way, it kind of reminds me of malloc in C.  Purist please don’t shoot the messenger. 

Well, i got a little long in the tooth as they say.  See what i did again?  Teeth, Snakes and Python.

See y’all next week.

Until Then,



Muzak To Blog By: various tunes by : Pink Martini, Pixies, Steve Miller.

Book Review: How To Read A Book

Reading is a basic tool in the living of a good life.

Mortimer Adler
My copy of HTRAB

First, i hope everyone is safe.

Second, i decided to write this blog based on two unrelated events: (1) as of late during the pandemic i have been reading commentary “online” of varying degrees such as “I want to read more books” and “Can you recommend some books to read?”  (2) i was setting out to write a technical blog on a core machine learning subject and whilst putting together the bibliography i realized i wasn’t truly performing or rather obtaining “level four reading” as outlined in the book i am going to review and by definition recommend.

i also am an admitted bibliomaniac and even more so nowadays an autodidact. i contracted the reading bug from my mother at an early age. i read Webster’s Dictionary twice in grade school. Still to this day she sends me the first edition or rare books to read. Thanks, Mom.

This is the stack of references.

As part of this book review and hopefully subsequent blog on a technical subject within machine learning, i decided to read the book a third time.  Which for this blog and review is an important facet.

As i was thinking about the best way to approach this particular book review i was pondering reading and books in general.  Which i came yet again to the conclusion:

There is much magic and wonder in this world.  Reading to me is a magical process.

It’s a miracle a child learns to speak a language!  It’s a miracle we can read! It’s even more of a miracle given the two previous observations that Humans are such astounding language generators (and authors)! 

One section of many in my library office

What an astonishing thing a book is. It’s a flat object made from a tree with flexible parts on which are imprinted lots of funny dark squiggles. But one glance at it and you’re inside the mind of another person, maybe somebody dead for thousands of years. Across the millennia, an author is speaking clearly and silently inside your head, directly to you. Writing is perhaps the greatest of human inventions, binding together people who never knew each other, citizens of distant epochs. Books break the shackles of time. A book is proof that humans are capable of working magic.

Carl Sagan

i consider a  book a “dual-sided marketplace” for magical access.

“How To Read a Book” by Mortimer J. Adler was originally published in 1940 with a re-issue in 1972.  The 1970s were considered the decade of reading in the United States.

In the 70’s the average reading level in the United States for most content speeches, magazines, books, etc was the 6th grade.  It is now surmised the average reading level and reading content is hovering around the 4-5th grade.

This brings us to the matter at hand the book review. 

The book’s author ​​Mortimer Jerome Adler (December 28, 1902 – June 28, 2001) was an American philosopher, educator, encyclopedist, and popular author. As a philosopher, he worked within the Aristotelian and Thomistic traditions. He taught at Columbia University and the University of Chicago, served as chairman of the Encyclopædia Britannica Board of Editors, and founded his own Institute for Philosophical Research.  

There is no friend as loyal as a book.

Ernest Hemmingway

The book states upfront that there is great inequality in reading with respect to understanding and that understanding must be overcome.  Reading for understanding is not just gaining information.  There is being informed and there is being enlightened when reading a book.  How To Read A Book (HTRAB) is precisely concerned with the latter.  If you remember what you read you have been informed. If you are enlightened from a book you know what the author is attempting to say, know what he means by what they say, and can concisely explain the subject matter. Of course, being informed is a prerequisite to being enlightened.

Professor Adler mentions in the book via Montaingne where he speaks of “An abecedarian ignorance that precedes knowledge, and a doctoral ignorance that comes after it.” 

Let’s first deal with Abcederian:  It essentially means dealing with alphabetized, elementary, rudimentary, or novel levels.  Ergo the novice ignorance arrives first.  The second are those who misread books.  Reading a ton of books is not reading well.  Professor Adler calls them literate ignoramus.  The said another way there are those that have read too widely and not well. 

Widely read and well-read are two vastly different endeavors.

The Greek word for learning and folly is sophomore. From google translate sophomore in Greek: δευτεροετής φοιτητής

This book pulls no punches when it comes to helping you help yourself.

HTRAB is in fact just that how to gain the most out of a book.  The book explains the four levels of reading:

  1. Elementary reading, rudimentary reading, basic reading or initial reading.  This is where one goes from complete illiteracy to at the very least being able to read the words on the page.  
  2. Inspectional reading.  This is where one attempts to get the most out of a book in a prescribed about of time.  What is this book about?
  3. Analytical reading places heavy demands on the reader.  It is also called thorough reading, complete reading or good reading.  This is the best reading you can do.  The analytical reader must ask themselves several questions of inquiry, organizational and objective natures.  The reader grasps the book.  The book at this point becomes her own.  This level of reading is precisely for the sake of understanding.  Also you cannot bring your mind from understanding less to understanding more unless you have skill in the area of analytical reading. 
  4. Syntopical Reading is the highest level.  It is the most complex and systematic reading and it makes the most demands on the reader.  Another name for this level is comparative reading.  When reading syntopically the reader accesses and reads many books placing them in relation to one another where the reader is able to construct and an analysis of knowledge that previously did not exist.  Knowledge creation and synthesis is the key here.

This is what i realized i wasn’t doing with respect to the machine learning blog.  Yes, i ranked and compared the references against one another but did i truly synthesize a net new knowledge with respect to my reading?  

Tools of The Art of Reading

The HTRAB goes on to dissect the processes of each of these four steps and how to obtain them and then move on to the next level.  This reminds me of a knowledge dojo a kind of belt test for readership.

The book also goes on to discuss how to not have any predetermined biases about what the book is or is not.  This is very important i have fallen prey to such behaviors and cannot emphasize enough you must proceed into the book breach with a clear mind

Further, the author took their valuable time to write the book and you took the cash and time to obtain the book. 

Give the respect the book deserves.

Some books are to be tasted, others to be swallowed, and some few to be chewed and digested.

Francis Bacon

The book goes in-depth about how we move from one level of reading to eventually synoptical reading and the basis for this is reading over and over whilst at every read the book is anew and alive with fresh edible if you will information.

To read and to ruminate is derived from the cow.  From Wikipedia we have:

“Ruminants are large hoofed herbivorous grazing or browsing mammals that are able to acquire nutrients from plant-based food by fermenting it in a specialized stomach prior to digestion, principally through microbial actions.”

So to chew the cud or re-chew if you will over and over – ruminating upon the subject matter. The book states in most cases that it takes three reads to obtain synoptical reading levels.  Thus my comment previously about reading this a third time.  Amazingly it clicked.

The folks who need self-help books don’t read them and those that don’t need them do read them.


The book further explains how to read everything from mathematics to theology.  With very precise steps.  

i recommend this book over and over to folks and i usually get online comments like “so meta’ or “LOL”.  In-person i get raise eyebrows or laughter.

This is not a laughing matter oh dear reader.

The two following lectures deal with HRTAB from the son of someone who worked directly with Professor Adler. His name is Shaykh Hamza Yusuf. Professor Yusuf is an American neo-traditionalist Islamic scholar and co-founder of Zaytuna College. He is a proponent of classical learning in Islam and has promoted Islamic sciences and classical teaching methodologies throughout the world. He is a huge proponent of HRTAB and recommends it in many of his lectures and uses it as a basis for his teachings in many forms. In no shape or form am I endorsing any religious or political stance with posting these videos. i am only posting for the information-rich and amazing lectures alone. He covers several areas of academics as well as several areas of religion and even pop-fad behaviors with respect to reading.

Here are both parts of the lecture:

Get the book to learn how to arrive at chewing and digesting your beloved books to the level of syntopical nirvana.  Your mind and others’ minds will thank you for it. Here is a link on Amazon:

Until then,

#iwishyou water.

Be safe.


Muzak To Blog by:  Cheap Trick Albums.  I had forgotten how good they were.