Page 476 - ComputerScience_Class_11
P. 476

12.2.6 Membership Operator
              Membership operators are used to test whether a particular value is a part of a sequence such as a list, tuple, string
              or set. They help determine the presence or absence of an element within a collection. The result of a membership
              operation is always True or False.
              Python provides two membership operators:

                Operator                                            Description
                     in      Evaluates to True if the specified element is found in the sequence.
                   not in    Evaluates to True if the specified element is not found in the sequence.

              Program 9: To demonstrate the use of Membership operators.
                   Program 9.py

                File  Edit  Format   Run   Options   Window    Help

                # Assigning a list to variable numbers
                numbers = [1, 2, 3, 4, 5]

                # Checking if 3 is present in the list
                print("3 in numbers:", 3 in numbers)


                # Checking if 6 is not present in the list
                print("6 not in numbers:", 6 not in numbers)


                # Assigning a string to variable text
                text = "Python"


                # Checking if character ‘P’ is in the string
                print("’P’ in text:", ‘P’ in text)

                # Checking if character ‘z’ is not in the string
                print("’z’ not in text:", ‘z’ not in text)




                  Output
                3 in numbers: True
                6 not in numbers: True
                ‘P’ in text: True
                ‘z’ not in text: True




              12.2.7 Bitwise Operator
              Bitwise operators are used to perform operations on individual bits of integer numbers. These operators first convert
              numbers into their binary (0 and 1) form and then perform the required operation bit by bit. The result of a bitwise
              operation is an integer value.

              Bitwise operators are mainly used in low-level programming, data manipulation and situations where direct control
              over bits is required.






                  474  Touchpad Computer Science (Ver. 3.0)-XI
   471   472   473   474   475   476   477   478   479   480   481