2019년 9월 6일 금요일

Python[2 day]: input( ), int( ), float( ) functions

   Sometimes, you would like to input any value into Python interpreter while a Python program is running. For this, Python provides you with input function.
   The syntax of input function is very simple and just type the command of x=input( ). In ( ), you can insert your favorite message, however your message should be enclosed by the quotation marks in ( ). Then, Python interpreter will display your message on a monitor and wait your input.


   Since the integer of 5 is stored in the variable of x by the command of x=input( ), you can try to add other integer to the variable of x.


   However, you will see the error message as seen in the above figure, which is resulted from the fact that the data of 5 stored in the variable of x is not integer.
   Since Python interpreter recognizes the data stored by the input function as string, it is not possible to apply a mathematical operation to the stored data. In the above example, your input data of 5 is not integer, but string. Whether or not the data stored in a variable can be checked by the type( ) function in Python. If you use type( ) function, Python inform you of the class of data. Class is the classification of data type.


   In Python, any real number can be converted to the corresponding integer number by using the function of int( ), and vise versa by using the function of float( ).


   Then, if Python interpreter receives the data from input( ) function and converts it to an integer or a real number, it may be possible that any mathematical operation can be applied to the variable having the converted numeric data.

Before testing this example, check your Python version first. Under the version of 3.7, this trial would show an error message. 


   Python version used in this post is 3.7.4. The contents of this post will not applied to the earlier version of Python.

댓글 없음:

댓글 쓰기