Learning Python: Boolean Operators

When working with boolean, Python has the following operators:

  • and
  • not
  • or

If you have variables that have the values of either true or false, these operators work like AND, NOT, and OR.

a = true
b = false

not a #False
a and b #False
a or b #True

NOTE: OR returns the first non-falsy[1] value. When using AND, the second argument is evaluated only if the first argument is true.

  1. A falsy value is a value which when evaluated will return false. For example, some falsy values are: 0, false,"", [].

This blog post was originally published on my blog Communicode, where I write about different tech topics.

Avatar for Muhammad Asfour

Written by Muhammad Asfour

I am a Full Stack Developer with over 5 years of experience. I have worked with different tech stacks such as: Groovy/Java, PHP, .NET/.NET Core (C#), Node.js, React, Angular, and Vue.


Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.