day-0Python basis refining notes

Notes in class 
standard library without having to install, can be used directly import libraries, Python comes with
third-party libraries, you need to download and install in order to use
the module acquaintance
1, sys module
import SYS
Print (sys.path) # print environment variables
when you import the module, first look in the current directory, and then to find the site-package,
look to go global environment variable, if the name of the module are not imported, the error
print (sys.argv) # print script relative path
print (sys.argv [2] ) Get user # incoming third parameter

2, os module
Import OS
the os.system ( "the dir") does not save the results of Run #
cmd_res = os.popen ( "dir") . read () # after the command execution save results
os.mkdir ( "test_dir") # create a directory
3, .pyc is what stuff
Python is interpreted compiled after a first language, first find pyc file is executed, there is a direct call,
will compile the results does not exist save into memory, after the run is finished, save it to a file pyc
4, data types
4-1 digit
integer, long integer, float, complex complex
Long: Python long integer bit width is not specified, but it is not infinite
scientific notation: = 5E4 = 50000 * 10 ** 4. 5
4-2 Boolean
1 or 0
True or False
4-3 string
4 -4bytes data type
code converter (String <-> bytes)
encode ( 'utf-8') into a binary coded, prior to encoding of utf-8
decode ( 'utf-8') by decoding utf-8
for example:
encoding: 'aa'.encode (' UTF-. 8 ')
B' \ XE2 \ .... '
:'. decode decoder B '\ XE2 \ .... (' UTF-. 8 ')
' AA '
. 5, ternary operator
6, hexadecimal
hexadecimal representation, H suffix or prefix OX
. 7, the list
name = [ 'AA', 'BB', 'CC', 'dd']
name [-2:] # Remove all the penultimate
name [: 2] # taken from a first index to all elements 2
increases
name.append ( "ee") # additional
name.insert (2, "ff") # ff inserted into the rear subscript 1 is
modified
name [2] = 'gg'
Delete
name.remove ( 'gg') # delete the specified element
del name [1] # standard press delete
name.pop () # do not specify the last pop
name.pop (1) # 1 popup labeled elements of
lookup element position
name.index ( 'aa' )
Clear list
name.clear ()
to sort
name.sort () # default ASCII sort
name.extend (list) # merge list
import copy
shallow copy (copy memory address) and deep copy (a copy is completely independent)
copy.copy ( ) and copy.deepcopy ()
list loop
for in list I:
Print (I)
step sections
print (name [0: -1: 2]) # jumping sections Step Size 2

8-tuple
List = [ 'A', 'B', 'C']
for I in the enumerate (List):
Print (I )
# print list and index values
(0, A)
(. 1, B)
(2, C)
. 9, the string
name = 'Colby'
name.capitalize # initials
>> the Colby
name.center (50, '- ') # 50 to print characters, not both ends with a length' - 'filled
name.ljust (50,' - ') # 50 to print characters on the left with a length enough' - 'filled
name.rjust (50,' - ') # print 50 characters, with the right length is not enough' - 'padded
>> --- --- Colby
name.endwith (' by ') # determine what characters to the end
>> True
name.expand () # tab key number of characters converted into
name.find ( 'y') # Returns the index where the character
>> 4
name.rfind ( 'c') # find the rightmost specified character
name = >> 'Colby {name}, Age {}'
name.format (name = 'Colby', Age = 22 is)
name.format_map ({ 'name': 'Colby', 'Age': 22 is})
Incoming the value of the variable will default to the name to
name.isalnum () # determine whether the Arabic numerals and characters
name.isalpha () # determine whether plain English characters
name.isdecimal () # judge decimal
name.isdigit () # determine whether an integer
name.isidentifier () # determine whether it is a valid identifier variables
name.islower () # determine whether lowercase
name.isupper () # determine whether the capital
name.isnumeric () # determine whether an integer
join usage
Print ( ','. the Join ([ 'a', 'B', 'C'])
>> a, B, C # list turn into characters, separated by commas
name.lower () # to lower case
name.upper () # transfer uppercase
name.strip () # remove newline ends
name.lstrip () # remove the left line feed
name.rstrip () # remove the right line feed
random password - encryption and decryption (the character string to be used to encrypt the mess)
P = str.maketrans ( 'abcdefg', '1234567') abcdefg mapped to the # 1234567
Print ( ' colby'.translate (P)
>> 3ol2y
name.replace ( 'C', 'C') # string replacement
name.split ( ',') # partition according to the specified character string as a list
name.splitlines () # press newline separated as windows and Linux method
name.swapcase () # invert case
name.title () # hump
name.zfill () # 0 filled with
10, the dictionary
increases
info [ 'age'] = 10
deleted
del info.get ( "name") # Safely Remove
modify
info [ 'age'] = 10
Find
info.get ( "name") # Returns presence, absence of None
info [ 'name']
combined update
= {B 'name': 'Colby', 'Age': '29', 'height': '75Kg'}
info.update () # updated exists, does not exist new
info.values () # print all value
info.keys () # prints the key
info.setdefault ( 'name', 'age ') # exists is displayed, a new value does not exist
multi-stage operation of nested dictionaries
info [ 'person'] [ ' age' ] [. 1] = 90
info.item () # field will be transferred as a list
initDict dict.fromkeys = ([6,7, 8], [. 1, { "name": "Colby"}, 444])
# dictionary initialization
the same memory address # value reference, note that modification is equal to modify All
cycles dictionary
# efficient wording recommended
for I in info:
Print (I, info [I])
low # efficiency is not recommended
for k, in info.items v ():
Print (k,V)
. 11, the set of set
intersection & intersection returns the set
And set | Union returns a collection
set difference - difference returns a collection
symmetric difference ^ symmetric_difference does not exist in the two sets
subsets issubset True or False
superset isupperset True or False
collection added elements add a
set of a plurality of elements to add update
set to remove elements remove
strings, lists, dictionaries, determines whether the character set is present inside
for STR in / list / sET / dict:
Print (I)





Guess you like

Origin www.cnblogs.com/pythonzhao/p/11710841.html