1. 实参是一个具体的参数值;
例:def MyFirstFunction(name):
函数定义过程中的name叫形参;
2. API:应用程序接口(API:Application Program Interface);
应用程序接口是一组定义、程序及协议的集合,通过 API 接口实现计算机软件之间的相互通信。API 的一个主要功能是提供通用功能集。
3. 不能随意在函数里面修改全局变量的值,会报错。如果要改,可以用global关键字来修饰参数;
4. 闭包(closure)
例:def FunX(x):
def FunY(y):
return x*y
return FunY(y)
称FunY(y)就是一个闭包;
5. nonlocal:不是局部变量;
6. lambda语法:
lambda 函数的参数:函数的返回值
注:使用lambda函数可以使代码更加精简;
7.两个牛逼的内置函数:
(1)filter() 过滤器
filter实际是把任何非true的东西过滤掉。
例:第一种表达方式:
def odd(x):
return x%2
temp=range(10)
show=filter(odd,temp)
list(show)
结果:1 3 5 7 9
第二种表达方式:
list(filter(lambda x:x%2,range(10)))
结果:1 3 5 7 9
(2)map() 映射
list(map(lambda x:x*2,range(10)))
结果:0,2,4,6,8,10,12,14,16,18
8.递归:
例:
def factorial(n):
if n==1:
return 1
else:
return n*factorial(n-1)
number=int(input(“请输入一个正整数:”))
result=factorial(number)
print(“%d的阶乘是:%d”%(number,result))
结果:
请输入一个正整数:5
输出:120
注:递归是有进去就要有回来。
“分治思想”
例:
(1)迭代:
def fab(n):
n1=1
n2=1
n3=1
if n<1:
print(“输入有误!”)
return -1
while(n-2)>0:
n3=n2+n1
n1=n2
n2=n3
n-=1
return n3
result=fab(20)
if result!=1:
print(“总共有%d对小兔子”%result)
结果:总共有6765对小兔子
(2)递归:
def fab(n):
if n<1:
print(“输入有误!”)
return -1
if n==1 or n==2:
return 1
else:
return fab(n-1)+fab(n-2)
result=fab(20)
if result!=1:
print(“总共有%d对小兔子”%result)
结果:总共有6765对小兔子
9. 汉诺塔
例:
def hanoi(n,x,y,z):
if n==1:
print(x,’-->’,z)
else:
hanoi(n-1,x,z,y) #将前n-1个盘子从x移动到y上
print(x,’-->’,z) #将最底下的最后一个盘子从x移动到z上
hanoi(n-1,y,x,z) #将y上的n-1个盒子移动到z上
n=int(input(“请输入汉诺塔的层数:”))
hanoi(n,’X’,’Y’,’Z’)
10.fromkeys
fromkeys(...) 创建并返回一个新的字典
dictname.fromkeys(S[, V])
@S key;@V value 可选参数
举例:
>>> dict1 = {}
>>> dict1.fromkeys((1, 2, 3))
{1: None, 2: None, 3: None}
>>> dict1.fromkeys((1, 2, 3), 'number')
{1: 'number', 2: 'number', 3: 'number'}
>>> dict1.fromkeys((1, 2, 3), ('one', 'two', 'three'))
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> dict1.fromkeys((1, 3), 'num')
{1: 'num', 3: 'num'} #还是返回新的字典,并不会修改dict1
11.字典中键、值、键值映射项的访问
>>> dict1 = dict1.fromkeys(range(10), '赞')
>>> dict1
{0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞', 5: '赞', 6: '赞', 7: '赞', 8: '赞', 9: '赞'}
>>> for eachKey in dict1.keys():
print(eachKey, end=' ')
0 1 2 3 4 5 6 7 8 9
>>> for eachValue in dict1.values():
print(eachValue, end=' ')
赞 赞 赞 赞 赞 赞 赞 赞 赞
>>> for eachItems in dict1.items():
print(eachItems, end=' ')
(0, '赞') (1, '赞') (2, '赞') (3, '赞') (4, '赞') (5, '赞') (6, '赞') (7, '赞') (8, '赞') (9, '赞')
12.get(...) 从字典中找到key的映射值value
举例:
>>> dict1 = dict.fromkeys(range(10), 'Yes!')
>>> dict1
{0: 'Yes!', 1: 'Yes!', 2: 'Yes!', 3: 'Yes!', 4: 'Yes!', 5: 'Yes!', 6: 'Yes!', 7: 'Yes!', 8: 'Yes!', 9: 'Yes!'}
>>> dict1.get(10)
>>> print(dict1.get(10))
None
>>> dict1.get(10, '木有')
'木有'
>>> dict1.get(9, '木有')
'Yes!'
13.clear() 清空一个字典(包括使用当前字典赋值的其他字典)
举例:
>>> dict1.clear()
>>> dict1
{}
14.copy() 拷贝一个字典(浅拷贝,不受字典修改影响)
>>> a = {1:'one', 2:'two', 3:'three'}
>>> b = a.copy()
>>> c = a #相当于C的指针,C++的引用,修改字典c的值会同时影响字典a
>>> a
{1: 'one', 2: 'two', 3: 'three'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> c
{1: 'one', 2: 'two', 3: 'three'}
>>> print(id(a), id(b), id(c))
2334673012680 2334672609672 2334673012680
>>> c[4] = 'four'
>>> c
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> a
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
15. pop(...) 给定一个键弹出一个值
popitem() 随机弹出一个项(映射关系的键和值)
举例:
>>> a
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> a.pop(2)
'two'
>>> a
{1: 'one', 3: 'three', 4: 'four'}
>>> a.popitem()
(1, 'one')
>>> a
{3: 'three', 4: 'four'}
16. update(...) 使用一个子字典去更新原字典
dictname1.update(dictname2)
举例:
>>> a
{3: 'three', 4: 'four', 5: '小白'}
>>> b = {'小白':'狗'}
>>> a.update(b)
>>> a
{'小白': '狗', 3: 'three', 4: 'four', 5: '小白'}
17. 集合 set
(1) 使用{}创建的没有映射关系的字典,成为集合类型,如num = {1, 2, 3, 4, 5}
(2) 集合中元素唯一,重复的数据会被自动清理掉
(3) 集合中元素无序,不能索引取到其元素的值
(4) 集合使用关键字 set([]) 来创建
(5) 集合支持 in 和 not in 来判断是否属于集合
举例:
>>> num = {}
>>> type(num)
<class 'dict'>
#set没有体现字典的映射
>>> num1 = {1, 2, 3, 4, 5}
>>> type(num1)
<class 'set'>
#set唯一性
>>> num2 = {1, 2, 3, 4, 2, 3, 5, 1, 5, 5}
>>> num2
{1, 2, 3, 4, 5}
#set无序性
>>> num2[2]
TypeError: 'set' object does not support indexing
#set关键字创建集合
>>> set1 = set([1, 2, 3, 4, 5, 5, 5, 3, 1])
>>> set1
{1, 2, 3, 4, 5}
#list实现set的唯一性
>>> num1 = [1, 2, 3, 4, 5, 5, 3, 1, 0]
>>> temp = []
>>> for each in num1:
if each not in temp:
temp.append(each)
>>> temp
[1, 2, 3, 4, 5, 0]
#简化: 实现set的唯一性,并且会把set的无序性变为有序
>>> num1
[1, 2, 3, 4, 5, 5, 3, 1, 0]
>>> num1 = list(set(num1))
>>> num1
[0, 1, 2, 3, 4, 5]
18. add(...) 往集合中加入元素
remove(...) 从集合中删除指定元素
举例:
>>> num2
{1, 2, 3, 4, 5}
>>> num2.add(6)
>>> num2
{1, 2, 3, 4, 5, 6}
>>> num2.remove(4)
>>> num2
{1, 2, 3, 5, 6}
19. frozenset(...) 将集合设置为不可变集合,frozen:冰冻的,冻结的
举例:
>>> num3 = frozenset([1, 2, 3, 4, 5])
>>> num3
frozenset({1, 2, 3, 4, 5})
>>> num3.add(6)
AttributeError: 'frozenset' object has no attribute 'add'
集合内建方法(整理出来):
http://bbs.fishc.com/forum.php?mod=viewthread&tid=45276&extra=page%3D1%26filter%3Dtypeid%26typeid%3D403