Page 38 - Code & Click - 7
P. 38

Experiential Learning
              Lab Activity 1
             Program in Python to accept data from the user and display it.
             1. Type the code as shown in the image in the Python Editor.














             2. Press the F5 key to execute the code. Sample output of the program is given.


            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.
            The common data types used in Python are described in the table given below:
             Data Type                                  Description                                   Examples

             Integer      Represents whole numbers that can be positive, negative, or zero.  125, -32, 0
                          Mathematical operations are possible on these values.
             Float        Represents numbers with fractional or decimal parts. Can be positive,  45.67, -11.5
                          negative, or zero. Also known as Floating point numbers.
             String       Represents one or more letters or characters enclosed within single  “Antriksh”,
                          quotes ‘ ’ or double quotes “ ”. Can contain any alphabet, digit, or  ‘@’, “Hello
                          symbol. Mathematical operations are not permitted on these values. World”
             Boolean      Represents logical values in the form of True and False. The value 0  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
             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.


            36
   33   34   35   36   37   38   39   40   41   42   43