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
| Operator | Description | Example |
|---|---|---|
| and Logical AND | If both the operands are true then condition becomes true. | (a and b) is true. |
| or Logical OR | If any of the two operands are non-zero then condition becomes true. | (a or b) is true. |
| not Logical NOT | Used 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)
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)
| Operator | Description | Example | Try it |
|---|---|---|---|
| and | Returns True if both statements are true | x < 5 and x < 10 | |
| or | Returns True if one of the statements is true | x < 5 or x < 4 | |
| not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
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)
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. )
EXERCISEPlease do the work in your registers, click the photo and submit on git@stcolumbas.edu.in
- What are logical operators? Where are they used?
- Differentiate between AND and OR logical operators.
- What is the purpose of the NOT logical operator?
- What do you mean by "Precedence of operators"? How does it impact and expression in Python?
- 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))
good morning ma'am
ReplyDelete-ARTHAM
Good morning mam
ReplyDeleteWe have a class at 12. why are u early? I am testing something
ReplyDeleteMa'am do we have zoom
ReplyDeleteClass today
MAAM I WAS CHECKING IF THE CONTENT IS HERE
ReplyDeletegood morning mam
ReplyDelete-Ashish
Pls try the "TRY IT" option . but before that understand the working of the Operators.
ReplyDeleteok mam ,mam the zoom class will take place today ?
Delete-Ashish
pls respond otherwise I will understand that nobody is online
ReplyDeleteI have tried it ma'am
Deletemam i have try the options
DeleteGood morning ma'am
ReplyDeleteBrian
Good morning ma'am
ReplyDeleteGood afternoon ma'am
ReplyDeleteRahul
Are we clear about the topic?
ReplyDeleteyes mam
ReplyDeleteGood Morning Ma'am
ReplyDelete-Clement
do we understand the output of AND / OR and NOT operators?
ReplyDeleteYes ma'am
Deletegood morning ma'am
ReplyDeleteTejas
you are very late
Deletepls tell me answer for the following:
ReplyDeletea=true
b=false
c= NOT(a or b)
print(c)
This comment has been removed by the author.
Deleteall 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
ReplyDeleteGood morning ma'am
ReplyDelete-Geet
Geet we are half way through the class.
DeleteNO ANSWERS??????
ReplyDeleteI couldn't understand!
ReplyDeletepls go step by step.
Deletefirst find out the result for a OR b
what will be the result of
ReplyDeletea OR b
ans -true a or b
Delete-Ashish
Ma'am i have already done this work in the register, do i need to copy it again? Please advise.
ReplyDeleteRegards
-Geet
NO. Pls understand and answer the question given
DeletePls ma'am can you show the steps how to solve it
ReplyDeletea=true
ReplyDeleteb=false
so, a OR b = true OR false= false
Do u understand???
Ma'am, i didn't understand the last question of the exercise and the question given in the blog
ReplyDelete-Geet
thats why said, understand first. U can write later
DeleteI understand ma'am
ReplyDeleteThank you maam
ReplyDeletea=true
ReplyDeleteb=false
so,
a OR b = true OR false
which will be equal to false
Then NOT(false)= TRUE
thus the result is TRUE
Ok got it maam
DeleteMa'am, i understood the question given in the blog but did not understand the last question of the exercise
ReplyDelete-Geet
Geet pls read the solution step by step
Deletei understood mam, mam i have written true as reply
ReplyDelete
ReplyDeleteI 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