[015] Introduction to Python Basics: Short Circuiting, Logical Operators

Koay Yong Cett
4 min readAug 22, 2020
Photo by Alexandre Debiève on Unsplash

Short Circuiting is quite simple.

Before looking into short circuiting, lets see few coding first:

  • Here, we get true because both of these because both of this is evaluated as true.
  • Now, we use if block and the ‘and’ keywords. The ‘and’ keywords required both the variable(is_Friend , is_User) to be true in order for the best friends words to be printed out.

Now, short circuiting can be done when we use something like ‘or’ which we haven’t seen before.

  • The ‘or’ keywords will enable the best friends line of words to be printed out when both the variable (is_Friend , is_User) is true.
  • The or will says that either one of the variables/conditions are true then run the code.

Well, which operation (or/and) is more faster in term of performance. Is there any difference. As a matter of fact, it is because of short circuiting.

  • When I use ‘or’ here and because I only care if either one of these is true. The Python Interpreter will skip the second one when the first variable /condition is True. This operation is related to the short circuiting.
  • Therefore, the ‘or’ operation is faster and short circuiting happen.

However, this depend on what operation that you need. If you need to check if both the condition is true or not, then you have to use ‘and’. Well, short circuiting is also happening for the and keywords as well.

  • The is_User is also ignored when the first condition (is_Friend) is False and the short circuiting happen. This is because there is no way both of these conditions are true.

Logical Operators:

This term is very useful when it comes to conditional logic. There is a lot of the operators and let’s show some of them here:

  • This is a logical operator that you may seen in a math class.
  • Here is another math logical operator. Why can’t we use only one equal(=)? This is because equal(=) is used to assign a variable.
  • We are also able to use more than one logical operators in the bracket.
  • We have something like greater than or equal to.
  • We may also do exclamation mark with equal sign and referred as not equal to.
  • Then, we have another 3 useful logical operators here.
  • Not is and keyword but also a function that we can use as well.

Thanks for reading and I just write for fun 😃

--

--

Koay Yong Cett

A Bachelor CS student with major in Network Security (UniSZa). Every stories I shared is based on my personal opinion. Thanks you. Having my Internship now.