Page 482 - ComputerScience_Class_11
P. 482

Output

                Result 1: 20
                Result 2: 30
                Result 3: 512
                Result 4: 7
                Result 5: False
                Result 6: 22.0





                  12.6 TYPE CONVERSION
              Type conversion in Python is the process of changing a value from one data type to another, ensuring that operations
              and calculations work correctly. It allows a value to be treated as a different data type so that operations can be
              performed correctly. For example, you might convert an integer to a float or a string to an integer. Python supports
              two main types of type conversion:
              •  Implicit Conversion
              •  Explicit Conversion



              12.6.1 Implicit Conversion
              Implicit Conversion is the automatic conversion of one data type into another data type by Python. It is also known as
              automatic type conversion.

              When two different data types are used in an expression, Python automatically converts the smaller data type into the
              larger data type to avoid data loss.

              Program 16: To demonstrate Implicit Type Conversion in Python.

                   Program 16.py
                File  Edit  Format   Run   Options   Window    Help

                # Assign an integer value to variable a
                a = 12
                # Assign a float value to variable b
                b = 4.5
                # Python automatically converts integer ‘a’ to float
                # because float has higher priority than integer
                result = a + b
                # Display the values
                print("Value of a:", a)
                print("Value of b:", b)
                # Display the result
                print("Result after addition:", result)
                # Display the data type of result
                print("Data type of result:", type(result))







                  480  Touchpad Computer Science (Ver. 3.0)-XI
   477   478   479   480   481   482   483   484   485   486   487