Thursday, December 8, 2016

Reading Input in Python

       Reading input is easy in python compared to any other language,it has an inbuilt function that is input().In python 2 there are two different input functions those are raw_input() and normal input(),the main difference in between these two is the input format,if you specify the input in form of string using input() then it will represent it as a string in python2.

Example:  In Python 2,

              >>>value=input('ENTER INPUT:')
             ENTER INPUT:10
              >>>value
              10
              >>>value=input('ENTER INPUT:')
             ENTER INPUT:'10'
              >>>value
             '10'

           In the above example we can see that when we are using input() it is capturing the data format which we have given,we have given 10 in first as number so it took it as an number.For second time we have given '10' and it is considering it as an string.
           Now you came to know the usage of input(),let us see an example of raw_input() function
              >>>rawvalue=raw_input('ENTER INPUT:')
             ENTER INPUT:10
              >>>rawvalue
              '10'
           When we use raw_input() function it will consider the input given as string irrespective of the format,this can be clearly understood by the above example.

      But in python 3,the raw_input() function is removed and the input() works like raw_input() in python3.So if we use input() function in python 3 it will consider the input value as string.

Example:In  Python 3,

              >>>value=input('ENTER INPUT:')
             ENTER INPUT:10
              >>>value
              '10'
              >>>value=input('ENTER INPUT:')
             ENTER INPUT:'10'
              >>>value
             "'10'"
       If we try to use raw_input() in python 3 it will show the following error,

              >>> value=raw_input()
                 Traceback (most recent call last):
                 File "<pyshell#4>", line 1, in <module>
                 value=raw_input()
                 NameError: name 'raw_input' is not defined