1. Package
Import Module Name This package is first found in the current directory. Each module can be used __name__ to determine whether to run the program __main__ to create a package pack, set up a folder and then create __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 pretending to read all the rows, but when you use the data from the hard disk read out
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
existing in operation can be used
when traversing a sequence in python, we usually use the following method:For item in Sequence:process (item) If you want to take the location of an item, you can write this: for index in range (len (sequence)): Process (sequence[ Index]) Another good way is to use the Python built-in enumerate function: Enumerate (sequence,start=0) in the above function, sequence is an iterative object, which can be a list, a dictionary, a file object, and so on. Enumerate returns a tuple consisting of subscript and item:>>> seasons = [' Spring ', ' Summer ', ' Fall ', ' Winter '] >>> list (Enumerate ( Seasons)) [(0, ' Spring '), (1, ' Summer '), (2, ' Fall '), (3, ' Winter ')]>>> list (Enumerate (seasons, Start=1)) [(1, ' S Pring '), (2, ' Summer '), (3, ' Fall '), (4, ' Winter ')] The first example of the article can be written in this way: for Index,item in Enumerate (sequence):
Print Index,item
Which element would you like to find index which element has how many count increments append insert, sort kind Delete Pop (index) remove (ele)
For Iterating_var in sequence:
Do
If the number range (i;j: Step) is closed before I open I default to 0 steps default to 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 1000 length, with an initial value of 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 containing key-value pairs
Keys () VALUES ()
5. Function usage
From sys import ARGV #包, that is, module script,first,second,third = argv #script为文件名, followed by 3 parameters print Script,first,second, Third
The basic function uses Def print_two (*args): #if while for has a colon note that pointer parameter acceptance is not the same Arg1,arg2,arg3=argsprint "arg1:%r,arg2:%r,arg3:%r" % (ARG1,ARG2,ARG3) print_two ("argument", "Implument", "interface") ' <span style= ' font-size:12px; > Default parameters (from right to left), forcing the parameter m (x=3) function to define global variables the variable must be used after a function call. If the name is the same, the default return value is None, the return value can be any type with a tuple parameter, accept the return value function as a dictionary </span> ""
Multi-type pass-through value f (*t) to pass the data in the tuple to the parameter, if required, using the dictionary f (**d) value Redundancy def f (X,*args): args is a tuple def f (x,**kword): Processing redundant dictionary arguments
6.
Lambda expression
Lambda X,y:x*y return the value to the right of the Colon left argument
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