Page 115 - Touchpad_Plus_V3.2_Class 6
P. 115

Operator     Description                                  Example (x=2)                   Output

                     and       It returns true, if both operands are true. (x < 5) and (x < 10)              TRUE

                      or       It returns true, if one of the operands is   (x < 5) or (x < 2)               TRUE
                               true.

                     not       It reverses the result, returns false, if the  not [(x < 5) and (x < 10)]     FALSE
                               result is true or vice versa.


                 Program 3: To perform all the logical operators.

                                                             Program3.py
                   File  Edit  Format  Run    Options   Window    Help


                  #Program to compute the difference between two numbers entered by the user
                  a = True
                  b = False

                  print (a and b)
                  print (a or b)
                  print (not a)
                  print (not b)




                 The result of the above program will be:
                                                                Output

                  False
                  True
                  False
                  True




                 Relational Operators

                 Relational operators are used to compare the value of the two operands and returns True or
                 False accordingly. The relational operators are described in the following table:


                                                                                             Example
                  Operator      Name                       Description                                      Output
                                                                                          (x=8 and y=6)

                     ==         Equal      It checks if the values of two operands             x == y       FALSE
                                           are equal or not. If yes, then the condition
                                           becomes true.






                                                                                         Introduction to Python      113
   110   111   112   113   114   115   116   117   118   119   120