Page 100 - KEC Khaitan C8.5 Flipbook
P. 100

LOGICAL OPERATOR


                  In Python, logical operators play a crucial role in decision-making by allowing multiple conditions
                  to  be evaluated  together.  They return a Boolean result  (True or False) based  on the  logical
                  relationship between the conditions. They help in forming complex logical expressions that control
                  the flow of execution in programs.




                      Operator         Name                Description             Example (x=2)          Output



                                                   It returns true if both
                         and            AND                                       (x < 5) and (x < 10)      True
                                                   operands are true.
                                                   It returns true if one of the
                          or             OR                                       (x < 5) or (x < 2)        True
                                                   operands is true.

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




                            RANDOM PACKAGE

                  The random  package  in Python  is  used  to  generate  random  numbers  and  perform  random
                  operations such as selecting random elements from a sequence, shuffling data, and creating

                  random values for simulations. It helps in creating unpredictable outputs, which can be useful for
                  games, simulations, and decision-making in programs.

                  The reason why we use the random package:
                     To generate random numbers for games like dice rolling or lotteries.
                     To shuffle a list of items randomly.

                     To select random choices from a list, like picking a lucky winner.
                  Some commonly used functions of the random package are as follows:

                     random.randint(a, b): Generates a random integer between a and b (inclusive). For example,

                  Program 1: To use the random.randint(a, b) function.

                      Program.py
                   File  Edit  Format    Run   Options   Window    Help

                    import random                                                     Output
                    print(random.randint(1, 10))
                                                                                   8






                   98   Premium Edition-VIII
   95   96   97   98   99   100   101   102   103   104   105