Description of SYS.ARGV in Python (a confusing solution for reading the Concise Python tutorial)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.