Boolean Expressions

Boolean expression is an expression that is either true or false. The following example shows the use of the operator ==, which compares two operands and produces True if they are equal and False otherwise:
>>> 5 == 5 
True 
>>>5 == 6 
False 
True and False are special values that belong to the type bool, they are not strings. To prove this let’s apply type method:
>>>type(True)

>>>type(False)


The == operator is one of the comparison operators; the others are given in the following table.
Operator
Comment
x != y
# x is not equal to y
x >  y
#x is greater than y
 x < y
# x is less than y
x >= y
#x is greater than or equal to y
x <= y
# x is less than or equal to y
x is y
# x is the same as y
X is not y
# x is not the same as y

Although these operations are probably familiar, the Python symbols are different from the mathematical symbols.
IMPORTANT: A common error is to use a single equal sign (=) instead of equal sign (==). One equal sign is an assignment operator and two equal signs are comparison operator.

Here are some examples of using Boolean operators
>>>  x= 10 
>>>y= 20 
>>> x != y 
True 
>>>x > y
False 
>>>x < y 
True 
>>>x >= y
False 
>>>x <= y 
True 
>>> x is y 
False 
>>>x is not y 
True 

Nema komentara:

Objavi komentar