标签:led 表示 elf file als size 失败 sep new
1.定义和使用
1 def 函数名(形式参数):
2 ...
3 函数体
4 ...
5 返回值
函数的定义主要有如下要点:
(1).返回值
函数是一个功能块,该功能到底执行成功与否,需要通过返回值来告知调用者。
1 def 发送邮件():
2 发送邮件的代码...
3 if 发送成功:
4 return True
5 else:
6 return False
7
8 # 每次执行发送邮件函数,都会将返回值自动赋值给result
9 result = 发送邮件()
10 if result:
11 发送邮件成功
12 else:
13 发送邮件失败
(2).参数
函数的有三中不同的参数:
1 # ######### 普通参数 #########
2
3 # ######### 定义函数 #########
4 # name叫做函数print_name的形式参数,简称:形参
5 def print_name(name):
6 print name
7
8 # ######### 执行函数 #########
9 # ‘jkchan‘ 叫做函数print_name的实际参数,简称:实参
10 func(‘jkchan‘)
11
12 # 指定形参并传入实参,可以不按照顺序
13 def email(a,b,c):
14 函数体
15
16 email(1,2,3)
17 email(b=2,a=1,c=3)
1 # ######### 默认参数 #########
2
3 def drive(name, age = 18):
4 temp=name+"开车去东北"
5 return temp
6
7 # 指定参数
8 drive(‘zhangsan‘, 19)
9 # 使用默认参数
10 drive(‘zhangsan‘)
11
12 注:
13 如果不传参数,age值默认为18
14 没有默认值的参数必须传值,有默认值的参数可以不用传值
15 默认参数需要放在参数列表最后
1 # ######### 动态参数 #########
2
3 # ######### *args #########
4 def func(*args):
5 print args
6 # *,元祖,元祖的元素
7 # 执行方式一
8 func(11,33,4,4454,5)
9 # 为动态参数传入列表 *args,*列表
10 # 执行方式二
11 li = [11,2,2,3,3,4,54]
12 func(*li)
13
14 # ######### **kwargs #########
15 def func(**kwargs):
16 print args
17 # **,字典,字典的元素
18 # 执行方式一
19 func(name=‘wupeiqi‘,age=18)
20 # 为动态参数传入字典 *kwargs,**字典
21 # 执行方式二
22 li = {‘name‘:‘wupeiqi‘, age:18, ‘gender‘:‘male‘}
23 func(**li)
24
25 # ######### 结合 #########
26 def func(*args, **kwargs):
27 print args
28 print kwargs
2.内置函数
3.lambda表达式
对于简单的 if else 语句,可以使用三元运算来表示,对于简单的函数,也存在一种简便的表示方式,即:lambda表达式
1 # ###################### 普通函数 ######################
2
3 # 定义函数(普通方式)
4
5 def func(arg):
6 return arg + 1
7 # 执行函数
8 result = func(123)
9
10 # ###################### lambda ######################
11
12 # 定义函数(lambda表达式)
13
14 my_lambda = lambda arg : arg + 1
15 # 执行函数
16 result = my_lambda(123)
4.文件操作
open函数,该函数用于文件处理
操作文件时,一般需要经历如下步骤:
(1).打开文件
1 文件句柄 = open(‘文件路径‘, ‘模式‘, ‘编码‘)
打开文件时,需要指定文件路径,以何种方式打开文件(默认只读)和文件编码(默认utf-8),打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。
打开文件的模式有:
"+" 表示可以同时读写某个文件
"b"表示以字节的方式操作
注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型
(2).操作
1 class TextIOWrapper(_TextIOBase):
2 """
3 Character and line based layer over a BufferedIOBase object, buffer.
4
5 encoding gives the name of the encoding that the stream will be
6 decoded or encoded with. It defaults to locale.getpreferredencoding(False).
7
8 errors determines the strictness of encoding and decoding (see
9 help(codecs.Codec) or the documentation for codecs.register) and
10 defaults to "strict".
11
12 newline controls how line endings are handled. It can be None, ‘‘,
13 ‘\n‘, ‘\r‘, and ‘\r\n‘. It works as follows:
14
15 * On input, if newline is None, universal newlines mode is
16 enabled. Lines in the input can end in ‘\n‘, ‘\r‘, or ‘\r\n‘, and
17 these are translated into ‘\n‘ before being returned to the
18 caller. If it is ‘‘, universal newline mode is enabled, but line
19 endings are returned to the caller untranslated. If it has any of
20 the other legal values, input lines are only terminated by the given
21 string, and the line ending is returned to the caller untranslated.
22
23 * On output, if newline is None, any ‘\n‘ characters written are
24 translated to the system default line separator, os.linesep. If
25 newline is ‘‘ or ‘\n‘, no translation takes place. If newline is any
26 of the other legal values, any ‘\n‘ characters written are translated
27 to the given string.
28
29 If line_buffering is True, a call to flush is implied when a call to
30 write contains a newline character.
31 """
32 def close(self, *args, **kwargs): # real signature unknown
33 关闭文件
34 pass
35
36 def fileno(self, *args, **kwargs): # real signature unknown
37 文件描述符
38 pass
39
40 def flush(self, *args, **kwargs): # real signature unknown
41 刷新文件内部缓冲区
42 pass
43
44 def isatty(self, *args, **kwargs): # real signature unknown
45 判断文件是否是同意tty设备
46 pass
47
48 def read(self, *args, **kwargs): # real signature unknown
49 读取指定字节数据
50 pass
51
52 def readable(self, *args, **kwargs): # real signature unknown
53 是否可读
54 pass
55
56 def readline(self, *args, **kwargs): # real signature unknown
57 仅读取一行数据
58 pass
59
60 def seek(self, *args, **kwargs): # real signature unknown
61 指定文件中指针位置
62 pass
63
64 def seekable(self, *args, **kwargs): # real signature unknown
65 指针是否可操作
66 pass
67
68 def tell(self, *args, **kwargs): # real signature unknown
69 获取指针位置
70 pass
71
72 def truncate(self, *args, **kwargs): # real signature unknown
73 截断数据,仅保留指定之前数据
74 pass
75
76 def writable(self, *args, **kwargs): # real signature unknown
77 是否可写
78 pass
79
80 def write(self, *args, **kwargs): # real signature unknown
81 写内容
82 pass
83
84 def __getstate__(self, *args, **kwargs): # real signature unknown
85 pass
86
87 def __init__(self, *args, **kwargs): # real signature unknown
88 pass
89
90 @staticmethod # known case of __new__
91 def __new__(*args, **kwargs): # real signature unknown
92 """ Create and return a new object. See help(type) for accurate signature. """
93 pass
94
95 def __next__(self, *args, **kwargs): # real signature unknown
96 """ Implement next(self). """
97 pass
98
99 def __repr__(self, *args, **kwargs): # real signature unknown
100 """ Return repr(self). """
101 pass
102
103 buffer = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
104
105 closed = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
106
107 encoding = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
108
109 errors = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
110
111 line_buffering = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
112
113 name = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
114
115 newlines = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
116
117 _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
118
119 _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
(3).管理上下文
为了避免打开文件后忘记关闭,可以通过管理上下文,即:
1 with open(‘log‘,‘r‘) as f:
2 pass
如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。
在Python 2.7 及以后,with又支持同时对多个文件的上下文进行管理,即:
with open(‘log1‘) as obj1, open(‘log2‘) as obj2:
pass
5.递归
斐波那契数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368...
1 def func(arg1,arg2):
2 if arg1 == 0:
3 print arg1, arg2
4 arg3 = arg1 + arg2
5 print arg3
6 func(arg2, arg3)
7
8 func(0,1)
标签:led 表示 elf file als size 失败 sep new
原文地址:http://www.cnblogs.com/woyouyihujiu/p/7622875.html