Introduction
As exist in the title of this article, it is an article which has the connection with the subject of Python programming language. Specifically, the subject is going into a more detail aspect in Python programming language. That detail aspect is about how to input in Python programming language. So, the process for performing input is in the focus on this article. Actually, there are two types of input process in this context. The first one is the input process in the form of a variable. And in the second one is an input process involving file.
How to Input in Python
So, this part will present a specific information about how to perform input in Python programming language using both in the form of variable, command prompt argument and also file. each of the form of input file will decide the available method or function available in Python programming language. In other words, it is using a different method or function exist in Python programming language for variable input, command prompt argument input and file input.
How to Input Variable in Python
The first type of input is a simple one. It is an input process allowing to input a variable to accept data directly from an available input device in Python programming language. In this context, just use a specific method as a mean to receive input variable in Python programming language. That is a method or a function with the name of ‘input()’. In that case, as an example, the following is the usage of ‘input()’ function to perform the variable input in Python programming language :
C:\>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> data_input = input(); 903.21 >>> print("This is a statement for printing the content of Data Input : ",data_input); This is a statement for printing the content of Data Input : 903.21 >>>
In the above execution process using ‘input()’ method or function, it display the input process of a variable. That variable in the above source code is ‘data_input’. In the execution of the ‘input()’ method or function, it receive an input for the variable with the name of ‘data_input’. In the end, in order to prove that the input has the right data from the entry process which is 903.21, it will print it in the end. The result is a success with the display of the data exist in variable ‘data_input’ available from the input or the entry process. For an additional reference, just read the article with the title ‘How to Input Variable in Python’ in this link.
How to Input Command Prompt Argument in Python
Another type of input is using an argument or a parameter which is available by retrieving it from a command prompt. So, the execution of the command prompt will have an additional argument or parameter as an input. Actually, in order to retrieve an argument through command prompt, there is a module with the name ‘sys’. It is a module consists of functions and variables that strongly interact with the interpreter. The module itself comes pre-loaded in Python and does not require any installation. Amongst the many functions and variables are sys.argv(). So, in order to get input from command prompt as an argument or a parameter, sys.argv() is used. It is very useful to gather command line user input.
C:\>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> input = sys.argv[0]; 10 10 >>> input = sys.argv[0]; "This is the first argument" 'This is the first argument' >>>
The above example is the execution of ‘sys.argv’ function exist in the ‘sys’ module. The execution of it will only retrieve the first index or ‘sys.argv[0]’ and pass it to a variable with the name input and then print it further. For another detail reference, just look at the article with the title of ‘How to Input Command Prompt Argument in Python’ in this link.
How to Input File in Python
The next input type is an input which is using file. In other words, it is requiring a file as an input rather than a normal data input from any input device. Actually, it is very simple to use an external file as an input. The following is the pattern for accepting input in the form of an external file :
open("file_name.file_extension")
So, just use a method for accepting file input. That method is ‘open()’ with a parameter as an argument where it is actually the name of the file with its extension as it exist in the above syntax pattern.