Faster Learning Tutorials

# Python relational operators for math and text

Relational or comparison operators in Python evaluate to True or False and provide logic to programs.
1. It's all relative - Program logic requires that we compare one operand relative to another.
2. Math & text - Understand that logic of equality works with both numbers and strings.
3. Six operators - Review Python comparison operators.
4. Python vs. Excel - Compare operators with Excel.
5. Next: text strings - Introduce text strings in Python.
###### Paul Alan Davis, March 15, 2017
Updated: August 11, 2018
##### Learn faster by typing these examples in your Python Interpreter.

~/ home  / tech  / full stack  / python relational operators

## Learn Python logical operators for program flow

Beginner

### Video Tutorial

Python relational operators for math and text | Python for Beginners (5:00)

Videos can also be accessed from our Full Stack Playlist 3 on YouTube.

### Code Examples and Video Script

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)

• `python3`
• `less`

(Operators in Python)

• `<`
• `>`
• `<=`
• `>=`
• `==`
• `!=`
• `exit()`

In the next video, we'll leave math for a bit to cover text.

#### Step 1 - Relational Operators

Let's start with notes here in Linux (with `less`).

paul@fullstack:~\$ less notes/video0024.txt

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.

Python relational operators for math (Video 24) -------------------------------------------- | | Python | | | # | Symbol | Meaning | | | | | -------------------------------------------- | 1 | < | less than | -------------------------------------------- | 2 | > | greater than | -------------------------------------------- | 3 | <= | less than or equal to | -------------------------------------------- | 4 | >= | greater than or equal to | -------------------------------------------- | 5 | == | equal to | -------------------------------------------- | 6 | != | not equal to | -------------------------------------------- notes/video0024.txt (END)

#### Step 2 - Logical Operators Work With Math and Text

So the point of relational operators is to evaluate two things (called operands) and return the answer `True` or `False`. That's it. That's the whole point.

These `True`s and `False`s are used to code logic, or the conditional flow of a program, or just to evaluate math like we're doing here.

#### Step 3 - Six Python Relational or Conditional Operators

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.

##### Python less than operator

Is `1` less than `2`?

paul@fullstack:~\$ python3 Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 1 < 2 True

True. That's easy.

##### Python greater than operator

Number 2. Is `1` greater than `2`?

>>> 1 > 2 False

False.

Let's go back and jog our memories for a second.

Python relational operators for math (Video 24) -------------------------------------------- | | Python | | | # | Symbol | Meaning | | | | | -------------------------------------------- | 1 | < | less than | -------------------------------------------- | 2 | > | greater than | -------------------------------------------- | 3 | <= | less than or equal to | -------------------------------------------- | 4 | >= | greater than or equal to | -------------------------------------------- | 5 | == | equal to | -------------------------------------------- | 6 | != | not equal to | -------------------------------------------- notes/video0024.txt (END)
##### Python less than or equal to operator

Okay, number 3. Is `1` less than or equal to `2`?

>>> 1 <= 2 True

True.

##### Python greater than or equal operator

Number 4. Is `1` greater than or equal to `2`?

>>> 1 >= 2 True

The answer is obvious right? We just need to memorize how it's input.

Let's head back for a second.

Python relational operators for math (Video 24) -------------------------------------------- | | Python | | | # | Symbol | Meaning | | | | | -------------------------------------------- | 1 | < | less than | -------------------------------------------- | 2 | > | greater than | -------------------------------------------- | 3 | <= | less than or equal to | -------------------------------------------- | 4 | >= | greater than or equal to | -------------------------------------------- | 5 | == | equal to | -------------------------------------------- | 6 | != | not equal to | -------------------------------------------- notes/video0024.txt (END)
##### Python equal to

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.

>>> 1 == 2 False

Here it is obviously `False`.

##### Python not equal to

In Number 6, the exclamation equals combo `!=` means: are they not equal to each other?

>>> 1 != 2 True >>> exit()

Obviously `1` does not equal `2`, so `True`.

#### Step 4 - Conditional Operators in Python vs. Excel

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<2 TRUE =1<2 TRUE
1>2 FALSE =1>2 FALSE
1<=2 TRUE =1<=2 TRUE
1>=2 FALSE =1>=2 FALSE
1==2 FALSE =1=2 FALSE
1!=2 TRUE =1<>2 TRUE
##### Less than in Excel

So is `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 Python.

Here goes, so `=1<2` and `Enter`. Good, it matches.

##### Greater than in Excel

Number 2. Is `=1>2`? Like this and `False`, another match.

##### Less than or equal in Excel

Number 3. Is `=1<=2`?

`True`. Good.

##### Greater than or equal in Excel

Number 4. Is `=1>=2`?

`False`. Good.

##### Equal in Excel

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).

##### Greater than or equal in Excel

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

For homework, build on what we learned in the last tutorial with this `True` or `False` problem. It's long, but try it first on paper before using Python 3.

Homework Problem: What does each side evaluate to? Will it return True or False? 6 / 3 // 3 - (1 + 2) ** 2 == 10 % 3 - 1 - 3 ** 2 / 1 1) Start with a pencil and paper 2) Verify with Python notes/video0024.txt (END)

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.

#### Step 5 - Next: Python Relational Operators

So our Playlist here is a journey in Data Science, and this can't be learned overnight.

• Client : HTML, CSS, JavaScript
• Software : Python Scientific Stack
• Data : PostgreSQL, MySQL
• OS : Linux (command line), Debian

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.

## What's Next?

See what's going on at our YouTube Channel. Subscribe here.

• To access all tutorials, click Outline.
• To learn the basics of math in Python, click Back.
• To see 9 rules for strings and variables, click Next.

~/ home  / tech  / full stack  / python relational operators

Keywords:
python relational operators
python operators
python not equal
python not operator
python comparison operators
python not
python not equal to
python equality operators
python negation
python compare
python logical operators
python versus excel