1. Package
Import Module Name This package is first found in the current folder. Each module is capable of using __name__ to infer whether the program is __main__ to create a package pack, establish a directory, and then create a __init__.pyimport pack.filenamepack.filename.f ()
Reference Kaneshige named Import pack as Newnamefrom cal Import * #这样使用不用加包名from sys import args #仅仅有args不用加包名
Pickle package The object into a file Out=open ("Myindex.dat", "WB") Pickle.dump (myindex,out) #存入a =pickle.load (out) #读出pprint The Pprint method in the package can visually print an object
2. File operation
A=open ("). ReadLines ()
A[4] #能够定位任何位置
Len (Open (' Data.sql '). ReadLines ()) #确定有多少行
Read is reading all the data into a string, ReadLine is reading a line, ReadLines is to read all the lines into a list, xreadlines is to pretend to read all the rows. But when you use it, you read the data out of the hard drive.
Fp.seek (offset[, whence]) to position the offset. Fp.read ([size]) can give an optional parameter size to specify the length of the read byte
From __future__ import With_statementwith open ('/path/to/file ', ' RB ') as FP: for line in fp: do_something_with ( Line) #速度更快
3. Presence, traversal
There is a use in operation to
when traversing a sequence in python, we typically use methods such as the following:The for item in Sequence:process (item) is assumed to be taken to the location of an item and can be written like this: for index in range (len (sequence)): Process (sequence[ Index]) There is also a better way to use the Python built-in enumerate function: Enumerate (sequence,start=0) in the above function, sequence is an iterative object, can be a list. Dictionary. File objects, and so on.
Enumerate returns a tuple consisting of subscripts and item:
>>> seasons = [' Spring ', ' Summer ', ' Fall ', ' Winter '] >>> list (enumerate (seasons)) [(0, ' Spring '), (1, ' S Ummer '), (2, ' Fall '), (3, ' Winter ')]>>> list (Enumerate (seasons, Start=1)) [(1, ' Spring '), (2, ' Summer '), (3, ' Fal L '), (4, ' Winter ')] The first example of the article can then be written like this: for Index,item in Enumerate (sequence):
Print Index,item
Which element would you like to find index which element has how many count add append insert, sort kind Delete Pop (index) remove (ele)
For Iterating_var in sequence:
Do
If the number range (i;j: Step) before closing I default feel 0 step default feel 1
Xrange
For I (iterator) in "Pathon" (sequence)
Iterators are content that is not subscript, use the For I int range (len ()) if subscript is used:
Traverse dictionary for K,v in Dict.items ():
Sum sum Len length
Use of struct arrays
Class Item:
def __init__ (self):
Self.name= "
self.size=10
Self.list=[]
A=[]
A.append (item ())
A[0].name= ' Cup '
A[0].size=8
Print Len (a)
Defines a length of 1000. The initial value is all 0
A = [0 for x in range (0, 1000)]
Defining a two-dimensional array
A=[[0 for x in range (+)] for Y in range (10)], A.append ([]) if using variable length
For I in range (0, ten): Arraylist.append ([]) for J in range (0, 5): Arraylist.append ([])
4. Dictionaries
Dictionary
Items () Returns a tuple that includes a key-value pair
Keys () VALUES ()
5. Function usage
The FROM sys import argv #包, which is the module Script,first,second,third = argv #script为文件名称. followed by a 3-digit print Script,first,second,third
The basic function uses the Def print_two (*args): #if while for both have a colon to note that the pointer is accepted differently arg1,arg2,arg3=argsprint "arg1:%r,arg2:%r,arg3:%r" % (ARG1,ARG2,ARG3) print_two ("argument", "Implument", "interface") ' <span style= ' font-size:12px; > Default arguments (Right-to-left), forcing the global variable in the X=3 function to be defined in the parameter must be used after the function call. If the duplicate name is changed, the default return value is None. The return value can be a random type with a tuple, accept the return value function as a dictionary </span> "
A multi-type pass-through value f (*t) passes the data in the tuple to the form, using the dictionary f (**d) value Redundancy def f (X,*args): args is a tuple def f (x,**kword): Handles redundant dictionary passes
6.
Lambda expression
Lambda X,y:x*y The left side of the colon returns the value to the right
reduce (f,l) functions f on the following list of reduce (lambda X,y:x*y,range (1,7)) 720The dictionary also looks at the effect of implementing function pointers and the effect of switch.
dict={' + ': Add, '-': sub};dict[' + '] () use or dict.get ("+") ()
7. Enter
Enter Raw_input () to the other type is int (raw_input ())raw_input ("Your name") put a hint in the message
Basic Python Content