06 Python Lecture 06 Operators in Python SS
06 Python Lecture 06 Operators in Python SS
== Equal to x==10
!= Not Equal to x!=5
> Greater than x>5
< Less than x<5
>= Greater or Equal to x>=5
<= Less than or equal to x<=5
Operators in Python
Python Logical operators
Logical operators are used to combine conditional statements:
and age>18 and age<60 //returns True if both conditions are true.
or age>18 or age<60
not not(age>18 and age<60)
Boolean Expressions in Python
A boolean expression is an expression that is either true or false.
>>> 5 == 5
In the above example, == is used which compares the value of given
two operands and returns true if they are equal, else False.
Operators in Python
In Python, True and False are equivalent to 1 and 0, respectively.
>>> 0 == False
>>> 1 == True
>>> 2 == True
>>> bool(0) // it will cast the integer to its equivalent Boolean value: False
>>> bool(1) // it will cast the integer to its equivalent Boolean value: True
>>> bool(2) // it will cast the integer to its Boolean value: True
>>>bool(-2) // it will cast the integer to its Boolean value: True
Operators in Python
>>> True + (False / True) // Output?
In the above example, True + (False / True) can be expressed as 1 + (0/1) whose output is 1.0
Operators in Python
>>> 17 and True // Output?
the operands of the logical operators should be boolean expressions, but Python is not very strict. Any
nonzero number is interpreted as “true.”