Sunday, September 22, 2019

Python tutorial 5 | Logical Operators

This is the fifth of Python Tutorials for Beginners. You can see the other Python tutorials in this blog here. This Python beginner tutorial explains logical operations in Python with examples. It also shows operator precedence within logical operators and between Python comparison operators and Python logical operators. Please view the Python video tutorial 5 or read on...

Python programming language has three logical operators. The logical operators in Python are not operator, and operator and or operator. Now, there are two Boolean values, which are True and False.

The not operator results in the other Boolean value. If a condition is True, the not operator converts it to False. If the condition is False, the not operator converts it to True. The and operator needs two conditions. If both the conditions are True, the and operator gives the result as True. Else, the and operator gives the result as False. The or operator also needs two conditions. If both the conditions are False, the or operator gives the result as False. Else, the or operator gives the result as True. If a logical expression has multiple operators, the Python logical operators have different operator precedence. The not operator has the highest priority, then the and operator and finally, the or operator.

Now, let us see these logical operators with examples.

You can try these Python commands at the Python prompt. Note that comments follow the # symbol (they are only for your understanding).
>>> not 1 == 2               # this Python statement has multiple operators. It has the comparison operator == and the logical operator not. The priority of comparison operators is higher so 1 == 2 is evaluated first, resulting in False. not converts it to True.
>>> 10 > 1 and 100 > 10               # this Python statement with two logical expressions echoes True
>>> not 1 < 10 and  10 < 100              # the operator precedence within logical operators gives a higher priority to not. This Python statement with multiple operations echoes False because the not converts the first condition to False.
>>> 10 > 1 or 10 > 100               # echoes True because or operator needs any condition to be True in order to give the result as True

You can see many examples of logical operators in my Python video tutorial 5. Thank you.

5 comments:


  1. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future...
    Best Software Development Company
    Software Development Company

    ReplyDelete
  2. Nice explanations of the software development basics, it's good to know that! A friend of mine has implemented a company which is the ERP software in Hyderabad right now, she provides cloud based ERP software in Hyderabad, so I hope it goes well for her.
    Best Regards

    ReplyDelete
  3. Hiii...Thanks for sharing Great info...Nice post...Keep move on...
    Python Training in Hyderabad

    ReplyDelete
  4. Data science is one of the top course in today's career. Your content will going to helpful for all the beginners who are trying to find Types of Operators in Python Programming Language. Thanks for sharing useful information. keep updating.

    ReplyDelete