4 Poses of Python "HelloWorld", You Know a Few

python.exe is a normal program

And all other commands, Qiaoxia on the command line python and press Enter when the operating system to the PATH path searched python.exe and executed. From this point of view, it is not different from other programs. For example: notepad Notepad explorer will open and Explorer will open.

The difference is that it python is a pure command line program, which does not have a familiar window interface.

Note that the executable .exe file name suffix just naming style for Windows systems, enter the command time is omitted. In the Linux system and executable files are not .exe suffix, python the command style is biased Linux-style, so the python mentioned later when a direct say python instead python.exe.

Python parameters

When execute command-line programs often require you can bring parameter (argument) . E.g:
The implementation of a single notepad, open a new untitled Notepad, if executed notepad hello.py, will open the specified text file (of course, the current path must have a named hello.pyfile, otherwise notepad there will prompt you).

notepad create file
python Is an interpreter program is its ability to explain the implementation of Python code.
So we can pass Python code as a parameter to it. The most common is to pass a file to it:

python hello.py

For example, we just use Notepad to create a hello.py file:

python execution script-helloworld’s first pose

Here’s .py the file extension is not necessary, not necessarily like Notepad can only edit the .txt file. The suffix is just for easy identification by the operating system and users. This is a Python source file.

Execute without parameters python, because there is no content to explain, you enter the interactive mode.
In interactive mode, the prompt >>> back waiting for user input, one for each input, an interpretation performed.

You must already be familiar with helloworld in interactive mode, so I wo n’t count it here. Let ’s talk about it another day REPL!

Strictly speaking, no arguments do not mean there is no content , but rather the content source from the standard input (stdin) . In fact, it is equivalent to:

python -

It is standard practice for Linux programs to use a dash to indicate standard input. The so-called standard input can be thought of as coming from user input for those who are not familiar with the operating system. The relative, naturally the standard output (stdout) . We use the print() function is the result printed on the standard output.

stdout And stdin may be a vertical line | in series form a pipe(pipe) , it is possible to write the following this alternative helloworld:

C:\Users\Davy>echo print('helloworld')|python
helloworld


helloworld 2

Note that , in the windows system, where echo the back can not be quoted, and in the Linux system you must add a layer of quotes:

[root@davycloud ~]# echo print('helloworld')|python
-bash: syntax error near unexpected token `('
[root@davycloud ~]# echo "print('helloworld')"|python
helloworld

Python options

In Python installation article, we used python --version to test python program is normal.
Such use - or -- special parameters passed to the program call option(the Option) , one bar (that is, minus sign -) followed by a letter, known as a short option, -- with full word back, called the long options.

Note that the option parameter is a special format in the following py helpful information, it is collectively args

Some options have only short or long format, and some options have both short and long. E.g. --version corresponding short option -V (uppercase V).
In addition to --version the print version, as well as through --help or -h print the help information is relatively common practice:

python --help

After the encounter unfamiliar command, can be executed first --help try. But it is worth reminding, Windows systems use traditional commands are /? presentation options.

The following options are more useful in the python interpreter:

-c option

-c cmd: program passed in as string

C:\Users\Davy>python -c "print('helloworld')"
helloworld


helloworld 3

Also can be multi-line statements, with a semicolon ;delimited on the line:

C:\Users\Davy>python -c "import sys;print(sys.version)"
3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)]

-i option

-i: inspect interactively after running script

Enter the interactive mode after executing the script.

This option is useful in the case of learning, here is a small example for everyone.

For example, when you learn a function, you need to write a multi-line statement to define a function. Obviously it is more appropriate to write it in a file.

But then practice test this function, if you write it in the file, then every time:

Edit-Save-Run

Each run starts from the beginning, which is more troublesome (there is no animation because of the trouble):

Ran twice

This time we can use python -i test.pyto run scripts automatically enters interactive mode after the end of the script. At this point, all objects defined in the script can still be accessed. It’s as if you continue to write code after the script:

-m option

-m mod: run library module as a script

Run the module as a script.

For example, the famous line of code starts an HTTP server:

python -m http.server


http.server

In the article Installing Python, we have already introduced the folder location of the module, so the above command can also be used to run the script directly:

python C:\Users\Davy\AppData\Local\Programs\Python\Python38\Lib\http\server.py
Serving HTTP on :: port 8000 (http://[::]:8000/) ...

It should be targeted to .py the specific location of the file, this is different on each person’s environment, it is clear than python -m module more than trouble.

It can be seen here that module typing commands on the command line has the same effect, in fact, their principles are similar. module Also you need to have a path variable, holding a list of search paths.

This path variable is stored in the sys module:

python -c "import sys;print(sys.path)"

For more details, learning later to module discuss further details related to the contents of the time.

Usage of py.exe

After installation is complete, we use py --list to display the installed python list version. Here brief introduction of py the use of the command.
Similarly, the use -h or --help to get help:

py --help

Print information is divided into two parts, in front of py their help, followed by python help.

usage:
py [launcher-args] [python-args] script [script-args]

Launcher arguments:

-2     : Launch the latest Python 2.x version
-3     : Launch the latest Python 3.x version
-X.Y   : Launch the specified Python version
     The above all default to 64 bit if a matching 64 bit python is present.
-X.Y-32: Launch the specified 32bit Python version
-X-32  : Launch the latest 32bit Python X version
-X.Y-64: Launch the specified 64bit Python version
-X-64  : Launch the latest 64bit Python X version
-0  --list       : List the available pythons
-0p --list-paths : List with paths

In addition to that we have seen the bottom -0 and --list, other parameters are used to select the python interpreter. Not only can you distinguish between version numbers, you can also choose between 32-bit or 64-bit. It is worth noting that the version number includes only the first two digits.

Without the selection version parameter, it actually corresponds to the default python interpreter. All other parameters that are not in the above format are also used as parameters of the python interpreter.

So, knock on the command line py and python the effect is the same (each key can be less knock four times, and dramatically improve efficiency, ✌️), even if put on the version parameter, is also very convenient:

C:\Users\Davy>py -0
Installed Pythons found by py Launcher for Windows
 -3.8-64 *
 -2.7-64

C:\Users\Davy>py -2 --version
Python 2.7.17

C:\Users\Davy>py --version
Python 3.8.1

To demonstrate py the capabilities and specially installed python 2.7, and this time without adding to PATH

The question is, pip which on Scrip thow to do in order?

This situation, combined with the previously described -m options on it:

C:\Users\Davy>py -2 -m pip --version
pip 19.2.3 from C:\Python27\lib\site-packages\pip (python 2.7)

It’s the same when installing the package:

py -2 -m pip install

Almost forgot, the 4th pose of helloworld: python -m hello
hello
helloworld 4

Summary

This paper describes the python basic usage interpreter, demonstration by helloworld code passed in four ways: file, string, stdin, module. It also describes how to use the windows py start to run different versions of python.

#python #programming #helloworld

4 Poses of Python "HelloWorld", You Know a Few
9.55 GEEK