Page 50 - Computer - 7
P. 50

DATA TYPES IN PYTHON

          Each variable holds some data or value. Data type refers to the type of data that is stored in a variable.
          Data types are also used to distinguish between values of different types.
          Python supports several data types. The common data types used in Python are described in the table

          given below:

           Data Type                                    Description                                     Examples


                        Represents  whole  numbers  that  can  be  positive,  negative,  or  zero.
           Integer                                                                                   125, -32, 0
                        Mathematical operations are possible on these values.

                        Represents numbers with fractional or decimal parts. Can be positive,
           Float                                                                                     45.67, -11.5
                        negative, or zero. Also known as Floating point numbers.

                        Represents  one  or  more  letters  or  characters  enclosed  within  single
           String       quotes ‘ ’ or double quotes “ ”. Can contain any alphabet, digit, or symbol.   “Antriksh”, ‘@’,
                        Mathematical operations are not permitted on these values.                   “Hello World”

                        Represents  logical  values  in  the  form  of  True  and  False.  The  value  0
           Boolean                                                                                   True, False
                        represents False, while the value 1 represents True.


          Data Type Conversion

          By default, the input( ) function in Python converts all types of data typed by the user into string data.
          Even integers and floating point numbers are converted to string data type. To perform mathematical

          operations on such data, we need to convert it into integer or float data type. Similarly, we may need to
          convert numbers into a string to prevent mathematical operations on the value. The int( ), float( ), and
          str( ) functions help us convert one type of data into another.

             •  int( ) function: This function converts a string into integer data. Its syntax is:

                <variable> = int(string variable)
             •  float( ) function: This function converts a string into float data. Its syntax is:


                <variable> = float(string variable)
             •  str( ) function: This function converts a number into string data. Its syntax is:

                <variable> = str(numeric variable)


          Mind Fog       ERROR



             We can convert numeric data from one type into another. When we convert float data into integer, the
             decimal or fractional part is lost. Thus, int(23.5) will return 23. When we convert integer data into float, a
             decimal part is added to it. Thus, float(15) will return 15.0.




               48
   45   46   47   48   49   50   51   52   53   54   55