DEV Community

Vicki Langer
Vicki Langer

Posted on • Updated on

 

Charming the Python: Boolean & Operators

If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.


Boolean & Operators

  • Boolean
  • Operators
    • Arithmetic
    • Comparison
    • Logical

Boolean

True or False are the only possibilities with a boolean. However, you can use them for yes/no questions and many other things too.

is_dog = False
is_coding = True
is_programmer = True
is_cat = False
is_human = true  # this is true, but will error because it isn't capitalized
Enter fullscreen mode Exit fullscreen mode

True and False are constants with the values 1 & 0

True == 1
False == 0
Enter fullscreen mode Exit fullscreen mode

Operators

Operators are the constructs which can manipulate the value of operands. -reference

+ - * / % // **  # math or arithmetic operators
== != < > <> >= <=  # comparison operators
and or not  # logical operators
Enter fullscreen mode Exit fullscreen mode

Arithmetic Operators

I gave a bunch of examples in the post below.

Comparison Operators

you can compare numbers, words, lists, and more

Operator What it is Example
== equals 2 == 2
!= does not equal 2 != 7
> greater than 9 > 2
>= greater than or equal to 9 >= 9 or 9 >= 7
< less than 4 < 6
<= less than or equal to 4 <= 7

Logical Operators

and or not are useful for making decisions

dog_needs_exercise = True
is_awake = True

if dog_needs_exercise and is_awake:
    print("Take dog for a walk")
Enter fullscreen mode Exit fullscreen mode
dog_hungry = False
is_dinner_time = True

if dog_hungry or is_dinner_time:
    print("Feed dog")
Enter fullscreen mode Exit fullscreen mode
# getting paid this week?

a = "go to work"
b = "get work done"

if a and b:
    print("You get paid")  # because you did both

if a or b:
    print("You won't get paid")  # because you didn't do both
Enter fullscreen mode Exit fullscreen mode

Series based on

Top comments (9)

 
vickilanger profile image
Vicki Langer

As mentioned before, True/False is actually 1/0. So, are you saying we should be using and & or to evaluate to 1/0?

I’ve tried to find examples that don’t lead to a Boolean outcome and I’m at a loss.

I read the python docs this morning and it refers to these as Boolean operators.

Collapse
 
vickilanger profile image
Vicki Langer

Thanks for letting me know.

I've added a note about the values.

I know and & or don't directly evaluate to True & False. I was trying to simplify the example, but I think I oversimplified it. I'll come up with something that doesn't come across that way. I wanted to make sure if a beginner comes across the post that it's not using anything too advanced.

 
vickilanger profile image
Vicki Langer • Edited

So, part of my problem understanding your original post was that I had never seen assert before. I've read up on it and now I think I can make sense of this.

If I'm now understanding correctly, and & or statements seem similar to if, else, and elif statements.

I'm thinking something like this is a simple enough example.

dog_hungry = False
is_dinner_time = True

if dog_hungry or is_dinner_time:
    print("Feed dog")
 
vickilanger profile image
Vicki Langer

I definitely don’t want it to be incorrect. The only example I keep thinking of is a while loop, but that’s a bit more advanced than the current notes.

Join us at DEV
Yes, this is technically an “ad”, but really we just want to ask if you want to join DEV. We have 900k+ developers reading, posting, and enjoying community, and would love to have you.   Create an account and continue your coding journey.