The path to python: wupeiqi

Source: Internet
Author: User

The path to python: wupeiqi

1. Import Module

Import Module name

Function in the form Module name import module

You can put the module in the same directory or in the site-packages directory.

1 import sys2 print (sys. path) # print the environment variable 3 print (sys. argv) # print the Relative Path 4 5 import os6 export _res = OS. system ('dir') # Run the command without saving the result. 7. Export _res = OS. popen ('dir '). read () 8 OS. mkdir ('new _ dir') # create a single directory
View Code

 

2. pyc File

When the Python program runs, the compilation results are kept in the pycodeobject in the memory. When Python ends, the Python Interpreter

Pycodeobject is written to the pyc file. When the Python program runs for the second time, the program first finds the pyc file in the hard disk and

Compare the generation time of the component. If the pyc time is the latest time, load the component directly. Otherwise, repeat the above process.

 

3. Data Type

Number (integer, floating point number)

String

Boolean 1 true 0 false

Bytes type. The binary data is of the bytes type.

1 msg = 'Love me Beijing Tiananmen Square '2 print (msg. encode (encoding = 'utf-8 ')) 3 msg = B '\ xe7 \ x88 \ xb1 \ xe6 \ x88 \ x91 \ xe5 \ x8c \ x97 \ xe4 \ xba \ xac \ xe5 \ xa4 \ xa9 \ xe5 \ xae \ x89 \ xe9 \ x97 \ xa8 '4 print (msg. decode (encoding = 'utf-8 '))
View Code

 

4. Ternary operations

1 a,b,c = 1,3,52 d = a if a > b else c3 print (d)
View Code

 

5. List

1 names = ['zhangyang', 'zhaoyi', 'shaoyun', 'jiachen', 'menglingjun', 'lilin'] 2 print (names [0: 3]) # Slice 3 print (names [: 3]) # Slice 4 print (names [-1]) # Slice 5 print (names [-3:]) # Slice 6 print (names [: 2]) # Slice 7 print (names [:-1]) # Slice
View Code
1 # append 2 names. append ('yangrui ') 3 # insert 4 names. insert (2, 'yangrui ') 5 # modify 6 names [2] = 'yangrui' 7 # Delete 8 names. remove ('yangrui ') 9 del names [1] 10 names. pop (1) 11 # index 12 names. index ('yangrui ') 13 # count 14 names. count () 15 names. count ('yangrui ') 16 # clear list 17 names. clear () 18 # reverse 19 names. reverse () 20 # Sort 21 names. sort () 22 # merge list 23 names. extend (['yangrui '])
View Code
1 # copy the list. For details, refer to copy 2 names = ['hangyang', 'zhaoyi', 'shaoyun', 'jiachen', 'menglingjun', 'lilin ', ['yangrui '] 3 names2 = names. copy () 4 print (names, names2) 5 6 import copy 7 # copy list, deep copy 8 names2 = copy. deepcopy (names) 9 10 names [3] = 'tomo' 11 names [-1] [0] = 'jack' 12 print (names) 13 print (names2) 14 15 # loop list 16 for I in names: 17 print (I)
View Code
1 import copy 2 # shortest copy, 3 method 3 person = ['name', ['saving', 100] 4''' 5 p1 = copy. copy (person) 6 p2 = person [:] 7 p3 = list (person) 8 ''' 9 p1 = person [:] 10 p2 = person [:] 11 p1 [0] = 'Tom '12 p2 [0] = 'Jane '13 p1 [1] [1] = 5014 print (p1) 15 print (p2)
View Code

 

6. yuanzu

Yuanzu is similar to the list, but cannot be changed. It can only be used for slicing and querying, also known as a read-only list.

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.