When reading the concise Python tutorial, the 8th and 14 chapters have the use of the SYS module, One of the notes about SYS.ARGV is a little confused, let oneself ponder for a long time, later only dawned, here to share what I understand, in order to facilitate and I have the same confused Python learners can better understand.
The following code comes from a concise tutorial on Python 8.1:
Example 8.1 using the SYS module
#!/usr/bin/python
# Filename:using_sys.py
Import Sys
print ' The command line arguments is: ' For I in SYS.ARGV:
Print I
print ' \n\nthe PYTHONPATH is ', Sys.path, ' \ n '
(source file: code/using_sys.py)
Output
$ python using_sys.py We are arguments
The command line arguments is:
using_sys.py
We
Is
Arguments
The PYTHONPATH is ['/home/swaroop/byte/code ', '/usr/lib/python23.zip ', '/usr/lib/python2.3 ', '/usr/lib/python2.3/ Plat-linux2 ', '/usr/lib/python2.3/lib-tk ', '/usr/lib/python2.3/lib-dynload ', '/usr/lib/python2.3/site-packages ', ' /usr/lib/python2.3/site-packages/gtk-2.0 ']
Combined with book reading, this module of the explanation I do not do too much narration, the book explained is very detailed and simple, the level is limited, difficult to transcend. Say SYS.ARGV, first of all it is a tuple; secondly, the first element of the tuple must be the program name itself, such as the program name here is "using_sys.py", then the output of sys.argv[0] is "using_sys.py"; then sys.argv[1: ] element is the user input content, and separated by a space, the output is marked with red content is actually the user type of content, for in loop output SYS.ARGV[0:4], respectively, corresponding to "using_sys.py", "we", "is", "arguments ”。
Summary: SYS.ARGV is the first element of its own program name, the user types the contents of the subsequent elements of the tuple, and the subsequent elements with a space to indicate that the element input is complete, continue to enter the next element content, press Enter is the tuple input end.
Description of SYS.ARGV in Python (a confusing solution for reading the Concise Python tutorial)