Python- Operators(Contd)-6(c)

Good Morning Dear students

I hope that you got enough time to take a review of the topics covered earlier. Don't forget these are linked topics.


By the end of this session you will be able to 
  • Understand the working of the various logical operators.
  • Predict the output of expressions involving Logical Operators.
  • Understand the precedence of operators in an expression and predict the output.
__________________________________________________________________________________
Python Logical Operators
There are following logical operators supported by Python language. Assume variable a holds 10 and variable b holds 20 then

OperatorDescriptionExample
and Logical ANDIf both the operands are true then condition becomes true.(a and b) is true.
or Logical ORIf any of the two operands are non-zero then condition becomes true.(a or b) is true.
not Logical NOTUsed to reverse the logical state of its operand.Not(a and b) is false.

  Logical operators are used to combine conditional statements:
(Try and predict the result in the following examples. To check whether you are right or not , click on "Try it" and see the result)                      
OperatorDescriptionExampleTry it
and Returns True if both statements are truex < 5 and  x < 10Try it »
orReturns True if one of the statements is truex < 5 or x < 4Try it »
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)Try it »

Example #1: Logical Operators in Python

x = True
y = False

# Output: x and y is False
print('x and y is',x and y)

# Output: x or y is True
print('x or y is',x or y)

# Output: not x is False
print('not x is',not x)

Python Operators Precedence

The following table lists all operators from highest precedence to lowest.

Sr.No.Operator & Description
1
**
Exponentiation (raise to the power)
2
~ + -
Complement, unary plus and minus (method names for the last two are +@ and -@)
3
* / % //
Multiply, divide, modulo and floor division
4
+ -
Addition and subtraction
5
>> <<
Right and left bitwise shift
6
&
Bitwise 'AND'
7
^ |
Bitwise exclusive `OR' and regular `OR'
8
<= < > >=
Comparison operators
9
<> == !=
Equality operators
10
= %= /= //= -= += *= **=
Assignment operators
11
is is not
Identity operators
12
in not in
Membership operators
13
not or and
Logical operators
(operators in RED are yet to be done. )
EXERCISE
Please do the work in your registers, click the photo and submit on git@stcolumbas.edu.in
  1. What are logical operators? Where are they used?
  2. Differentiate between AND and OR logical operators.
  3. What is the purpose of the NOT logical operator?
  4. What do you mean by "Precedence of operators"? How does it impact and expression in Python?
  5. Predict the output of the following python statements:
a = True
b = False
print(('a and b is',a and b))
print(('a or b is',a or b))                                            print(('not a is',not a))            

Comments

  1. We have a class at 12. why are u early? I am testing something

    ReplyDelete
  2. Ma'am do we have zoom
    Class today

    ReplyDelete
  3. MAAM I WAS CHECKING IF THE CONTENT IS HERE

    ReplyDelete
  4. Pls try the "TRY IT" option . but before that understand the working of the Operators.

    ReplyDelete
    Replies
    1. ok mam ,mam the zoom class will take place today ?
      -Ashish

      Delete
  5. pls respond otherwise I will understand that nobody is online

    ReplyDelete
  6. do we understand the output of AND / OR and NOT operators?

    ReplyDelete
  7. pls tell me answer for the following:
    a=true
    b=false
    c= NOT(a or b)
    print(c)

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  8. all to answer. I shall be taking attendance for the class based on the response for this answer.( whether right or wrong, dont worry ) pls try

    ReplyDelete
  9. Good morning ma'am
    -Geet

    ReplyDelete
  10. Replies
    1. pls go step by step.
      first find out the result for a OR b

      Delete
  11. Ma'am i have already done this work in the register, do i need to copy it again? Please advise.
    Regards
    -Geet

    ReplyDelete
    Replies
    1. NO. Pls understand and answer the question given

      Delete
  12. Pls ma'am can you show the steps how to solve it

    ReplyDelete
  13. a=true
    b=false
    so, a OR b = true OR false= false

    Do u understand???

    ReplyDelete
  14. Ma'am, i didn't understand the last question of the exercise and the question given in the blog
    -Geet

    ReplyDelete
    Replies
    1. thats why said, understand first. U can write later

      Delete
  15. a=true
    b=false
    so,
    a OR b = true OR false
    which will be equal to false

    Then NOT(false)= TRUE
    thus the result is TRUE

    ReplyDelete
  16. Ma'am, i understood the question given in the blog but did not understand the last question of the exercise
    -Geet

    ReplyDelete
  17. i understood mam, mam i have written true as reply

    ReplyDelete

  18. I stand corrected. PLS check
    a=true
    b=false
    so,
    a OR b = true OR false
    which will be equal to true

    Then NOT(true)= false
    thus the result is FALSE

    ReplyDelete

Post a Comment

Popular posts from this blog

HOLIDAY HW