Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 110 - 2615_KEC
P. 110

    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 reverses the result and     not [(x < 5) and
                             not            NOT        returns false, if the result is   (x < 10)]              False
                                                       true or vice versa.

                                                       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.

                                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.

                          Program1.py
                       File  Edit  Format    Run   Options   Window    Help

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

                         random.choice(sequence): Picks a random item from a list or a sequence. For example,

                      108   Premium Edition-VII

| 105 106 107 108 109 110 111 112 113 114 115 |