Python-based modules, data types, and data type conversions

Source: Internet
Author: User

First, the module

1. Standard library

No need to install, directly into the module used.

Import SYS module:

Import Sysprint (Sys.path) #打印环境变量绝对路径print (SYS.ARGV) #打印当前脚本相对路径

Print a script The second parameter: print (SYS.ARGV [2])

Import OS module:

Import os# Cmd_res=os.system ("dir") #只执行命令, do not save the result Cmd_res=os.popen ("dir"). Read () #执行命令, and save the result print ("--->", cmd_res) Os.mkdir ("New_dir") #在当前目录下创建一个新目录

2. Third-party modules

A third-party module is a module that needs to be installed, and the program runs out of error when the module is not on a swing path. Workaround: (1) Modify the environment variable; (2) Copy the module to the current directory.

Use of third-party modules: Create a new Login program login.py, in the same directory created another program reference.py, write the statement: Import login, run the program can invoke the login module.

Second, what is PYC?

Some explanatory languages can be optimized for the entire program by the interpreter's optimization, thus exceeding the compiler language, such as Java, more efficiently. When you enter Python hello.py on the command line, you actually activate the Python interpreter and tell it that you want to start working, but the first thing you do before "explain" is the same as Java, which is compiled:

Java Execution statement: JavaC Hello.java

Run Result: Java Hello

(1) When Python executes the command, it saves the compiled result in the Pycodeobject in memory, and when the program finishes running, the Python interpreter writes Pycodeobject back to the PYc file.

(2) When the Python program executes for the second time, the program will look for the PYc file in the hard disk, and if found, load it directly, otherwise repeat the above process.

Therefore, the PYc file is actually a persistent way to save the pycodeobject.

(3) But if the Python program is changed when the problem comes "" In fact, each time the program is run, Python is to detect if there is no this file, if any, and then detect PYC and the source file update time which update, if the source code file time Update, then re-execute (1).

Third, the data type

1, Number: integer, floating-point type

int (integer), long (Long Integer), float (floating point), complex (plural)

Scientific counting method: 78.6e4=78.6*10**4=786000

2. Boolean type (True, False)

Ternary operation: result= value 1 if condition else value 2

That is, if the condition is true: result= value 1, if the condition is false: result= value 2.

Example: a,b,c=2,5,3

D=a if a>b else c

Iv. Conversion of data types

In Python3, binary unification is represented by the bytes type, and str and bytes are not mixed in any implicit manner. Therefore, cannot splice character channeling and byte packet, the two cannot manipulate each other, search each other.

Conversion between STR and bytes:

msg= "Mantis stalks" print (msg) print (Msg.encode (encoding= "Utf-8")) #字符串编码为二进制print (Msg.encode (encoding= "Utf-8"). Decode ( encoding= "Utf-8")) #二进制转换为字符串

Operation Result:

Mantis stalks B ' \xe8\x9e\xb3\xe8\x9e\x82\xe6\x8d\x95\xe8\x9d\x89 ' mantis stalks

Five, string manipulation

Character encoding and transcoding: Unicode performs encoding and decoding commands throughout the process as a middleware.

1. Conversion between GBK and UTF-8:

#-*-coding:utf-8-*-s= "Hello" S_gbk=s.encode ("GBK") #编码print (s) print (S_GBK)
Print ("-----GBK converted to utf-8-----")
Gbk_to_utf8=s_gbk.decode ("GBK"). Encode ("Utf-8") print ("UTF8", Gbk_to_utf8)

Operation Result:

Hello B ' \xc4\xe3\xba\xc3 '-----GBK converted to utf-8-----UTF8 B ' \xe4\xbd\xa0\xe5\xa5\xbd '

2. Example:

#-*-Coding:utf-8-*-s= "study hard"   #注意: At this time S is still the default Unicode form of the system, not affected by the text header coding:utf-8 S_to_gb2312=s.encode ("gb2312")  #所以可以直接编码为gb2312gb2312_to_utf8 =s_to_gb2312.decode ("gb2312"). Encode ("Utf-8")  #gb2312转换为UTF -8gb2312_to_ Str=s_to_gb2312.decode ("gb2312"). Encode ("Utf-8"). Decode ("Utf-8") #最终将utf-8 convert to string output print (s) print ("gb2312", s_to_ gb2312) Print ("Utf-8", Gb2312_to_utf8) print ("str", GB2312_TO_STR)

Operation Result:

Learn gb2312 B ' \xba\xc3\xba\xc3\xd1\xa7\xcf\xb0 ' utf-8 b ' \xe5\xa5\xbd\xe5\xa5\xbd\xe5\xad\xa6\xe4\xb9\xa0 ' STR study hard

Six, string manipulation

 

Print (". Join" ([' 1 ', ' 2 ', ' 3 ', ' 4 ')) #字符串连接s =str.maketrans ("Abcdefjk", "12345678") #两组字符串一一对应print ("Yank". Translate (s))  #用数字代替表示字母print (' Garden '. Replace (' g ', ' G ', 1))  #替换print (' Gardedn '. RFind (' d '))  #查找最右边的 " D "The index value of print (' 2+4+7+8 '. Split (" + "))  #将" + "is replaced with", "print (' 2+3 \ +5+4 '. Splitlines ())  #将换行符用逗号填充

Operation Result:

1234
Y1n8
Garden
5
[' 2 ', ' 4 ', ' 7 ', ' 8 ']
[' 2+3 ', ' +5+4 ']

Python-based modules, data types, and data type conversions

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.