Page 166 - Computer Science Class 11 Without Functions
P. 166

Name the operator:
                 1.  It yields True if any of the two operands evaluates as  True and False otherwise.
                 2.  It performs modulus and assignment.
                 3.  It checks whether x is equal to 5.
                 4.  It divides two numbers and yields the quotient as a float.
                 5.  It assigns a value to a variable.
                 6.  It evaluates to False if the operands on either side of the operator denote the same data type.
                 7.  Binary operator with highest precedence.



        7.8 Type Conversion

        Type conversion is the process of converting the value of one data type to another. This is done when an expression
        is formed using data of multiple data types. Type Conversion can be achieved in two ways – explicitly and implicitly.

        7.8.1 Implicit Type Conversion

        Implicit type conversion, also known as coercion, takes place when a data value is converted from one data type to
        another by the Python interpreter, without explicit type conversion by the programmer. Consider the following example:
         >>> num = 20
         >>> num1 = 15.5
         >>> sum = num + num1    # int object num is converted to float implicitly.
         >>> print(sum)
              35.5
         >>> type(sum)
              <class 'float'>
        Note that sum is a float value, even though num is of type int.

        7.8.2 Explicit Type Conversion

        Explicit conversion, also called typecasting, takes place when the programmer gives such instruction. The syntax for
        explicit conversion is
        <transform_type> (<expression>)
        It would convert the expression to transform_type. For example,
         >>> percentile = 99.99
         >>> int(percentile)
              99
        Consider program 7.1 given below.

         Program 7.1 Write a program to accept a number from user and display  its square.

          01 # Objective: To accept a number and display its square.
          02 num = input("Enter a number :")
          03 square  = num * num
          04 print("The square of the entered number is ", square)
        Sample Output:

         >>> Enter a number :  5
              Traceback (most recent call last):
                  File "C:/Users/ADMIN/numSquare.py" line 3, in <module>
                  square = num*num
              TypeError: can't mutiply sequence by non-int of type 'str'


         164   Touchpad Computer Science-XI
   161   162   163   164   165   166   167   168   169   170   171