Eight, module
Standard library (comes with) third-party libraries (download required)
Sys
Sys.path Environment variables
SYS.ARGV parameters
Os
Os.system (CMD) # Execute command, do not save result, 0 success not 0 failed
Os.popen (cmd). Read () #执行命令, output results
Os.mkdir (dir) # Create directory
The Os.walk () method is used to output the file name in the directory through the directory tree Walk, up or down.
Valid in Unix,windows.
Import OS
For root, dirs, files in Os.walk (".", Topdown=false):
For name in Files:
Print (Os.path.join (root, name))
For name in dirs:
Print (Os.path.join (root, name))
Nine, PYc
PYC pre-compiled bytecode file with low completion, updated with Python file time comparison
X. Python data types
Python2 Integer (based on the number of machine bits to determine the value range, 32-bit machine value range -2**31~2**31-1,64 bit machine value range -2**63~2**63-1), long integer type
Python3 no Long integer type
Boolean value TRUE or False
XI. Data Operations
+ - * /
% modulo-Returns the division remainder
* * Power
The integer part of the Fetch
The smallest unit that can be represented in a computer is a bits
The smallest unit that can be stored in a computer is a bits (bit)
8bit = byte (bytes)
1024byte = 1kbyte
1024kbyte = 1mbyte
1024MB = 1GB
1024GB = 1T
& and
| or
^ XOR is 0 different for 1
~ 256 After the inverse of the bitwise-Take
>> right Shift n-bit x/2**n
<< left shift N-bit x * 2**n
Ternary operations
A,b,c = 1,3,5
D = A If a > b else c
Hex (suffix bh prefix 0x) four-bit binary
12, Byte
Bytes Type--binary data type
Python2 that the bytes type is the same as the string type
Python3 text is always Unicode, represented by the STR type
A string can be encoded into a byte packet, and a byte packet can be decoded into a string.
Python3 data transfer must be converted to binary (socket)
String-encode-bytes
BYTE-decode-string
13. List and Yuan Zu
Names[1:3] # Slice
NAMES[-1:-3] # error, python default order from left to right
NAMES[-3:-1] # Right
Names[-2:]
Names.append (' x1 ')
Names.insert (1, ' x2 ')
NAMES[2] = ' x3 '
Names.remove (' x3 ')
Del Names[2]
Names.pop () deletes the last one by default
Names.pop (2) Delete the value of a location
Names.index ("x4")
Names.clear () Clear list
Names.reverse () reversal
Names.sort () sort special characters-numbers-uppercase-lowercase
Names.extend (NAMES2) merger
Ganso once created, cannot be modified--read-only list
14. Enumerate enumeration
List1 = ["This", "yes", "one", "test"]
For index, item in enumerate (LIST1):
Print Index, item
>>>
0 this
1 is
21 x
3 Testing
If you want to count the number of lines in a file, you can write:
Count = Len (open (filepath, ' R '). ReadLines ())
This method is simple, but may be slow, and cannot even work when the file is larger.
You can use enumerate ():
Count =-1
For index, line in enumerate (open (filepath, ' R ')):
Count + = 1
XV, copy
Three ways to light copy
1.names2 = Names.copy ()
2.
Import Copy
Copy.copy () copy one layer
3.
Names3 = names[:]
Names2 = copy.deepcopy (names1) deep copy clone
16. String method
Name= "Shopping List"
Name.center ("-") the name string is printed in the middle and the remainder is populated with "-" with a total of 50 characters
Name.format (Name= ' Li ', year=23)
Name.format_map ({' name ': ' Li ', ' Year ': 12})
Name.endswith ("Li") determines whether the string ends with Li
"A\tli". Expandtabs (10) Output ' A Li ', convert \ t to a long space
Name.find (' a ') find A, Find returns its index, cannot find return-1
' 9aA '. Isalnum () True to determine if it is an Arabic character, including letters and numbers
Isalpha () Determines whether it is a plain English character
Isdecimal () determines whether the decimal
IsDigit () determines whether an integer
Isidentifier () to determine whether a valid variable name
IsNumeric () to determine if there is only a number
"|". Join ([' Li ', ' Jack ', ' Rain '])
' Li|jack|rain '
Name.ljust (50, "*") string printed on the left, the remainder filled with "*", a total of 50 characters
Name.rjust () * * * * * Right
Strip () go on both sides of the space and enter
Translate characters that can be used to encrypt and decrypt
p = Str.maketrans ("abcdef", ' 123456 ')
Print ("Alex". Translate (p))
Replace () Replacement
Find () The subscript for a value
RFind () to find the subscript of a value's right-most value
Split ()
Splitlines () automatically identify line breaks for different systems by line break
Swapcase () Reverse case
Name.zfill (50) Not enough characters with 0 padding
17. Dictionaries
Dictionary unordered
Del Info[3]
Pop ("Key3")
SetDefault ()
Update ()
Info.items ()
Dict.fromkeys ([6,7,8], "test") #初始化一个key值为6 7 8 dictionary with a key value of "test"
More efficient, indexed way
For I in info:
Print I,info[i]
There is a process of converting data into a list, which is not recommended when the amount of data is large
For k,v in Info.items ():
Print K,v
The path of Python, day2