One. Python functions are divided into 3 types:
1. Built-in functions:python provides us with shortcuts
#1. VARs () All variables of the current module, commonly used include the following 3:
1. ' __doc__ ' indicates the contents of the comment for the current file, in the __doc__ variable
2. ' __name__ ' represents the main function of the current file, in the __name__ variable
3. ' __file__ ' represents the path of the current file, in the __file__ variable
#2. Reload ()---used for import again, without import
#3. POW (2,11)----represents 2 of the 11-time
#4. Common built-in functions
Len (): Receives a sequence that returns the length of the sequence
All (): Receives a sequence, judging this common sequence, all the values in the sequence are true, returns true, otherwise, once there is a false, false
For writing web interface, the user enters the user name, the password one kind of judgment
Any (): Receives a sequence, judging the sequence, the value of the sequence as long as there are 1 true is true, return true, otherwise is false, only for false
==========================>>> li=[' 1 ', ' ', ' 3 '] #列表里面有一个元素为空 >>> All (LI) #有一个为空就为假False >>> Any (LI) #只要有1个是真就为真True >>> lt=[' 1 ', ' 2 ', ' 3 '] >>> all (LT) #全部为真就为真True
#5. Conversion of ASCII, numeric, character
Ord () receives Acssi a character, returns the number ==========================>>> ord (' A ') 65CHR () receives the number, returns the Acssi character ====================== ========>>> chr ' A ' >>> chr ' > '
Oct ()
Bin ()
#6. Enumerate ()---the starting value of the specified number
==================================>>> for K,v in Enumerate (LI): #后面一个参数不指明, default starting from 0 ... print k,v ... 0 3>>> for K,v in Enumerate (li,2): #后面一个参数指明从2开始计数 ... print k,v ... 2 13 24 3
2. Custom Functions
1.def---The keyword that defines the function
2. Function name---Convenient to call this function by function name
3. Parameters are not automatically executed after---declaration, only after the call is executed
4. Parameters of the function
Divided into 3 kinds:
#1普通参数
#2默认参数
1. Default parameters can only be placed on the last side
2. The default parameter can have multiple
========================================================================eg:def Email (arg,subject= "jachy Alarm System"): # The following parameter is the default parameter, which can be used when the call is not available ... email (arg,subject= "jachy Business System") #调用默认参数一定要指明email (ARG) #调用
#3动态参数
1. can receive multiple parameters;
2. When the sequence is transmitted, the internal automatic construction of the tuple;
3. If you pass a sequence, you can use the * parameter to avoid the inner tectonic tuple
===================
def func (*arg): #*arg is a dynamic parameter that transforms a parameter into a tuple for use inside the function
Pass
==================
Eg:>>> def func (*arg): #动态参数的声明方式 ... print arg ... ------------------->>> func () >>> func (1) (1,) >>> func (1, "Alex") (1, ' Alex ') >>> Func (1, "Alex", 333) (1, ' Alex ', 333)
======= Dynamic parameter 1: Parameter plus 1 * ===========
>>> li=[1,2,3,4]>>> func (LI) ([1, 2, 3, 4],) >>> func (*li) #加 * Avoid construction again in internal tuples (1, 2, 3, 4) >>& Gt Func (11,22,33) (11, 22, 33)
======== Dynamic Parameter 2: parameter is 2 *=============
>>> def func (**kwargs): #2个 * In the internal structure dictionary ... print Kwargs ... >>> func (k1=123) {' K1 ': 123}>>> func ( k1=123,k2=321) {' K2 ': 321, ' K1 ': 123}-----------can also be passed directly to the dictionary--------->>> dic={"K1": 123, "K3":321}>>> Func (**dic) #先构造字典, and then passed in {' K3 ': 321, ' K1 ': 123}
Combination of two dynamic parameters on =========== =================
>>> def func (*args,**kwargs): ... print args ... print Kwargs ... >>> func (1, 2, 3) {}>>> F UNC (k1=123,k3=321) () {' K3 ': 321, ' K1 ': 123}>>> func (1,2,3,k1=123,k3=321) (1, 2, 3) {' K3 ': 321, ' K1 ': 12}
5. return value of function
3. Import functions
4. Main function ( in other words, a function is actually a block of code that is broken down by function)
Format:
===============================================if __name__ = = "__main_": #表示此文件是程序的主文件, then this function is the main function xx code xx_=========== =======================================
Two. How the file opens
1.obj_file=file (' path ', ' mode ')
2.obj_file=open (' path ', ' mode ') #推荐用这种方式
3. Way (or mode):
R (Default),
W, rewrite the file, while opening the file, erase the original file and rewrite it. If it does not exist, it will be created temporarily when it is opened.
A, written to the file appended to the end, readable, writable,
r+: Readable and writable
RB: Processing binary files
RU: Convert all the \n,\r in the file into \n,u can only be combined with R
4. Operation function
Obj.seek ()
Obj.seek (5)----means reading starting at 5th Byte
Obj.seek (0)----means reading starting at No. 0 byte
Obj.tell ()---Returns the position of the current pointer
Obj.truncate ()----truncated backwards based on the pointer position, modifying only the previous
Eg:obj_file=file (' path ', ' mode ') print Obj_file.tell () #读取文件指针位置, at this point in the 0obj_file.close () print Obj_file.tell () #读取文件指针位置, At this time at the end eg=============#!/usr/bin/env python#-*-coding:utf-8-*-file_obj=open (' Log ', ' r+ ') File_obj.seek (8) Print File_ Obj.tell () print file_obj.read () print File_obj.tell () file_obj.close () eg:=========file_obj=open (' Log ', ' r+ ') file_ Obj.write (' 111 ') #向文件最开始处向后写, the side writes the side covers the File_obj.truncate () #然后写完之后, starts from here to truncate, after all deletes file_obj.close ()
three, with keyword -to avoid opening the file after forgetting to close, you can through the management context, namely:
eg
With open (' Log ', ' R ') as F: <====>f =open (' log ', ' R ')
Repair
...
This way, when the with code block finishes executing, the internal automatically shuts down and frees the file resource.
After Python 2.7, with also supports the management of multiple file contexts simultaneously, namely:
Eg: used to modify Nginx configuration file with open (' Log1 ', ' R ') as Obj1, open (' log2 ', ' W ') as Obj2: #同时打开2个文件: log1,log2, one read, one write for line obj1: #读源配 File New_line=line.replace (' 10.4.3.1 ', ' 10.4.3.2 ') #将前面一个参数替换后面的一个参数 obj2.write (new_line) #将读取的内容写入到新的配置文件中pass
Python Basics 3