Getting Started with Python supplements

Source: Internet
Author: User
Tags python script

First, the Interpreter

When executing python/home/dev/hello.py in the previous step, it is clear that the hello.py script is executed by the Python interpreter.

If you want to execute a python script like executing a shell script, for example, you ./hello.py  would need to specify the interpreter at the head of the hello.py file, as follows:

123 #!/usr/bin/env pythonprint"hello,world"

So, execute:. /hello.py Can.

PS: Need to give hello.py execute permission before execution, chmod 755 hello.py

Second, Content coding

The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)

ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols.

It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode

Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more

UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...

Therefore, when the Python interpreter loads the code in the. py file, it encodes the content (the default ascill), if it is the following code:

Error: ASCII code cannot be expressed in Chinese

123 #!/usr/bin/env pythonprint "你好,世界"

Correction: It should be shown to tell the Python interpreter what code to use to execute the source code, i.e.:

1234 #!/usr/bin/env python# -*- coding: utf-8 -*-print "你好,世界" Third, comments

When the line stares: # is annotated content

Multiline Comment: "" "Annotated Content" ""

Figuring out the relationship between ASCII, Unicode, and UTF-8, we can summarize how the current computer system works with character encoding:

In computer memory, Unicode encoding is used uniformly, and is converted to UTF-8 encoding when it needs to be saved to the hard disk or when it needs to be transferred.

When editing with Notepad, the UTF-8 characters read from the file are converted to Unicode characters into memory, and when the edits are complete, the conversion of Unicode to UTF-8 is saved to the file:

When you browse the Web, the server converts dynamically generated Unicode content to UTF-8 and then to the browser:

So you see a lot of pages of the source code will have similar <meta charset="UTF-8" /> information, that the page is exactly the UTF-8 encoding.

Iv. executing script incoming parameters

Python has a large number of modules, which makes developing Python programs very concise. The class library includes three:

    • Python-supplied modules
    • Industry-Open Source modules
    • Modules developed by programmers themselves

Python internally provides a SYS module where SYS.ARGV is used to capture parameters passed in when executing a python script

123456 #!/usr/bin/env python# -*- coding: utf-8 -*-import sys printsys.argv 

Getting Started with Python supplements

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.