An ad-free and cookie-free website.
Videos can also be accessed from our Full Stack Playlist 3 on YouTube.
Python relational operators for math and text | Python for Beginners (5:00)
Welcome. Today's question: How do you use relational operators for math in Python?
I'm Paul, and someone 30-years ago said a phrase that stayed with me. He said, "it's all relative" and this helped me in my life and my career, especially when I advanced from Excel to Python.
So here we're not going to talk about Einstein's theory, or anything abstract, but instead demonstrate how we compare things in math using Python's operators.
As a side note, these operators work for text as well, so yes they are important.
Besides the six operators in Python, we'll also see how they relate to operators in Excel because that's where many of us first used a computer for math. Are they the same? We'll see.
(Commands in Linux)
(Operators in Python)
In the next video, we'll leave math for a bit to cover text.
Let's start with notes here in Linux (with
You may be learning Python on your Mac or Windows box. Here we use the Linux command line.
Python operates the same, mine just sits in a different place.
So the point of relational operators is to evaluate two things
(called operands) and return the answer
False. That's it. That's the whole
Falses are used to code logic, or the
conditional flow of a program, or just to evaluate math like we're
Let's get practice with each of the six in another window.
Open Python (with
python3) and we will
keep it super simple here. The answers are obvious.
1 less than
True. That's easy.
Number 2. Is
1 greater than
Let's go back and jog our memories for a second.
Okay, number 3. Is
1 less than or
Number 4. Is
1 greater than or equal to
The answer is obvious right? We just need to memorize how it's input.
Let's head back for a second.
Number 5 evaluates whether two things (operands) are equal.
We will see the single
= sign in later
videos, but when you see two, like this
==, translate it as: are the left and
right sides equal? So let's try it out.
Here it is obviously
In Number 6, the exclamation equals combo
!= means: are they not equal
to each other?
1 does not equal
Pretty straightforward, huh? So let's jump over to Excel because you may have learned relational operators there.
As you can see, I duplicated the same table, adding a column in Excel, and answers we got in Python, so we can compare if they work the same way, and we'll do that in the Excel formula column.
|is?||Python answer||Excel formula||Excel answer|
1 less than
2? And if this (the equal sign) looks
funny, it's because in spreadsheets we use an equal sign when
inputting formulas. Also, we don't want spaces here, like we did in
Here goes, so
Enter. Good, it matches.
Number 2. Is
=1>2? Like this
False, another match.
Number 3. Is
Number 4. Is
Number 5. Is
=1==2? Ah, Excel
anticipated this and gave us an alternative, offering one equal symbol,
=1=2, not two
==. (In Excel
instead use the if/then structure using
=if()for conditional logic).
Number 6. Is
=1!=2? Ah, again, we
have a difference here. The operator isn't the same. So let's note
similarities and differences, so it sticks in our brain.
For homework, build on what we learned in the last tutorial with this
False problem. It's long, but try it
first on paper before using Python 3.
Oh, another reason for comparing Excel to Python is to see if what you've already learned in one, carries over to the other.
The best coders remember where languages differ, like a spoken language, which explains why learning that second language is easier.
So our Playlist here is a journey in Data Science, and this can't be learned overnight.
You're welcome to join us as we build out this stack, so subscribe to get the latest video and next we will cover Text Strings.
Have a nice day.
See what's going on at our YouTube Channel. Also, so you don't miss new content follow us @factorpad on Twitter and at our no-spam email list.
A newly-updated free resource. Connect and refer a friend today.