For Non-boolean types :
x=10
y=20
print(x
and y)#x is non zero (True), which returns 2nd arg: y(20)
print(x
or y)#x is non zero(True), which returns 1st #
arg: x(10)
print(not
x)
"""output
20
10
False
"""
x=1
#non zero: True
y=2#True
z=3#True
print(x
and y)# print(True and 2): 2
print(x<y
and y<z) #print(1<2 (True) and 2<3(True):True
if(x<y
and y<z): #1<2(True) and
2<3(True):True
print("Hyd")
else:
print("Vij")
"""output
2
True
Hyd
In
the above statement, compound condition x<y and y<z, this is a
combination of two conditions, when ‘and’ is used, the total condition will
become ‘True’, if both the conditions are ‘True’ result is also ‘True’, since
both the conditions are True, we will get “Hyd” as output.
Note2:
x=1,y=2,z=3
Here
x>y i.e 1>2 (False) but y<z i.e 2<3(True) when using ‘or’ operator
if anyone condition is True, it will take the total compound conditions as
True.
0
means False, Non-zero means True
Lets
take x=1 and y=2
Operator
|
Example
|
Meaning
|
Output
|
and
|
X and y
|
If x is ‘false’, it returns x, otherwise, it returns y
|
1 and 2= 2
|
Or
|
X or y
|
If x is false, it returns y, otherwise, it returns x
|
1 or 2= 1
|
Not
|
not x
|
If x is false, it returns True, otherwise False
|
Not True=False
|
Expected
output: by using logical operators and,or not
200
100
False
x=100
y=200
print(x
and y)
print(x
or y)
print(not
x)
Example4:
10
and 20 result 20
0
and 20 result 0
If
first argument is zero then result is zero otherwise result is non-zero
Note:
empty string is treated as False.
#empty
string is treated as 'False'
print("ravi"
and "rajendra") #rajendra
print(""
and "Sitha") #false so returns 1st argu: #empty string
print("kiran"
and "")#True and false i.e empty
print(""
or "rajesh) #1st is false so returns 2nd i.e #rajesh
print("Hyd"
or "") #1st True so returns 1st “Hyd
print(not
"") # not False i.e True
print(not
"Vij") #not True i.e False
output:
rajendra
rajesh
Hyd
True
False
Logical
Boolean operators:
Let's take a=True, b=False
Opearators
|
Example
|
Meaning
|
Output
|
And
|
a and b
|
If both a and b are True, then it returns True
|
False
|
Or
|
a or b
|
If either a or b is True, then it returns True, else false
|
True
|
Not
|
not x
|
If x is True, it returns False
|
False
|
Example program:
a=True
b=False
print(a and a)#True
print(a and b)#False
print(b and b)#False
print(a or a)#True
print(a or b)#True
print(b or b)#False
print(not a)#False
print(not b)#True
"""output
True
False
False
True
True
False
False
True
"""
Related Youtube Video: https://youtu.be/6z3kUjBEq6Q
Next Topic: Assessment 6
Assignment operators
Previous Topic:Logical Operators
No comments:
Post a Comment