-
range(start,end,step=1)
(start,end,step=1)
range(start,end)
(start,end)
range(end)
(end)
-
for iter_var in iterable_object
iter_var in iterable_object
suite_to_repeat
suite_to_repeat
- 可以明确循环次数:遍历一个数据集内的成员;在列表解析中使用;生成器表达式中使用
- iterable_object: String, List,Tuple, Dictionary,File
- 自定义函数的创建方式:
def function_name([arguments])
function_name([arguments])
“optional documentation string”
“optional documentation string”
function_suite
function_suite
- 默认参数:
- 函数的参数有默认值,如果未传入参数则使用默认值
- 传入参数则使用传入参数的值
- 默认参数一般放置在所有参数之后
- 传递函数:函数可以像参数一样传递给另外一个参参数,即function1(function2(x))
- 匿名函数lambada x:x+x #与ES6的箭头函数是类似的
#普通函数 python2的写法
def func(a,b,c):
func(a,b,c):
return a+b+c
return a+b+c
print func(1,2,3)
#lamanda函数
f = lambda a,b,c:a+b+c
= lambda a,b,c:a+b+c
print f(1,2,3)
-
几种类型的常用方法
-
序列
-
序列的标准类型运算
- 值比较:
<、>、<=、>=、==、!=
、>、<=、>=、==、!= - 对象身份比较:
is、is not
- 逻辑运算:
and、or、not
-
序列类型函数(使用时可直接查文档)
seq[start: end] 切片操作
判断元素是否存在序列中:in,not in
enumerate(), reversed(), sorted(), zip()
(), reversed(), sorted(), zip()
-
字符串
-
字符串类型方法(使用时可直接查文档)
join(), strip(), replace(), split(), translate(),startswith()
(), strip(), replace(), split(), translate(),startswith()
-
列表(数组)
-
列表类型常用方法
append(), count(), extend(), insert(), pop(), sort()
(), count(), extend(), insert(), pop(), sort()
-
元组
len(),max(),min(),tuple()将序列转换为元组