SoFunction
Updated on 2024-10-30

Python Usage Graphic Details

I. Python sys module

"sys" is "system". It's a system module.The module provides interfaces for users to access variables used and maintained by the python interpreter itself, as well as a number of functions, of which argv is the one we'll cover today.

Two,

In the last article we talked about referencing modules, so sys is the equivalent of a module, and argv is a function within that module.

The "argv", or "argument value", is a list object that stores the "command line arguments" that are supplied when a python script is invoked on the command line. ".

The focus here is on understanding this statement, which we illustrate with an example:

Let's look at a simple example first:

import sys
print([0])

Let's look at the output:

Here we will name the file and put it in our system directory and run it under cmd:python

Let's look at the output; the

You can see the result: the name or full path of the called script file

Here's what needs to be emphasized:argv[0] is the filename or full path of the called script.

Then let's change the program and try again:

import sys
print([1])

Here we change the code to argv[1]

At this point, let's type python hello and see what happens:

See this, is not some of you guys understand, [] is actually a list.

So let's try another one, and since it's a list, we'll let it output from the third:

import sys
print([2:])

Look at the results of the run:

This time our input is 1 2 3 4 5, we expect the output to start from the third element of the list, and the result is very much what we expect, and at this point, we have been thoroughly understood.

Summary:

[] is a list

[0] is the name or full path of the script file being called

The elements after [1:] are what we enter from outside the program, not from the code itself, and to see its effect, we have to save the program, run it externally and give the parameters, which is why we run it inside cmd.

Elevate:

Now that we've figured out the basic usage of [], let's test it again:

import sys
for index,arg in enumerate():
    print("The %dth parameter is: %s" % (index, arg))

This time enter: a b c d e

We can see that the output is the name of the script file and the parameters we entered, in that order.

To this point this article on the use of Python in the article is introduced to this, more related to the use of Python content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!