python笔记_函数

来源:互联网 发布:金达莱花 网络歌手 编辑:程序博客网 时间:2024/05/21 16:04



%s : 把右边的"变量"带到字符串中,并把"变量的值"放到%s的位置上(%d   %r 同理)

1
2
print'%s,%s'%('x','y')#x,y
print'x%sz'%'y'#xyz

问 : %r 和 %s 有什么不同?

答: %r用来做调试比较好,因为他会显示变量的原始数据,而%s和其他的符号则是向用户显示输出的

问 : 平常应该有%s 还是 %r ?

答 : 应该用%s , 只有想获取某些东西的调试信息时才能用到%r . %r 给你的是"程序员原始版本"

"%s是给用户显示 , %r是调试专用,他显示的是"原始表示""


  len() : 以"数"的形式返回你传递的字符串的长度

1
2
3
a='abc'
b=len(a)
printb#3

 

函数

def  创建函数

1
2
3
4
deffunction(params):#def():
block#
returnexpression/value#
#return,,
    ;return,NONE,return
    ,""NONE.

 

 

1
2
3
4
defhello():
print"helloworld"#return
printhello()#()
#helloworldNone

 

1
2
3
4
5
defhello():
print"helloworld"
return#return
    ,return
printhello()
#helloworldNone

 

1
2
3
4
defname(a,b):
returna+b#return
printname(1,2)
#3

形参:形参称为"形式参数",在用def关键字定义函数时,函数名后边的(括号)里的变量成为"形参"

实参:实参称为"实际参数",在调用函数时,提供的值或者变量成为"实参"

 

1
2
3
4
5
6
defadd(a,b):#(a,b)
returna+b
add(1,2)#(1,2)
x=2
y=3
add(x,y)#(x,y)

全局变量与局部变量

在python中,函数的每个层次会生成一个符号表,里层能调用外层,而外层不能调用里层,当外层和里层有同名变量时,外层变量会被里层变量屏蔽掉.

1
2
3
4
5
6
7
8
9
defname():
x=2
count=2#countwhile
whilecount>0:#0
x=3
printx
count=count-1#=-1
name()
#33count

局部变量: 函数里面定义的所有"变量"为局部变量,函数体外不得调用

1
2
3
4
5
6
7
8
defname():
x=2
returnx+3
printname()
#namex,name
    .name
z=x-3
printz
#namex,!

全局变量: 在函数外边定义的,作用域时整个文件的"变量",成为全局变量,可以在函数内部调用,但是如果在函数内部改变全局变量的话,必须使用 global 关键字声明!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
x=2#x
    ,
defname():
printx#x
defname_1():
globalx#globalx
x=1.5
returnx+1.5
printx
name()#printname()2None
printname_1()
#223.0

关键字参数:通过参数名字来匹配的,不需要按照参数定义时的位置来传递的参数叫关键字参数

1
2
3
4
5
6
7
defhello(a,b):
printa
printb
hello('hello','world')#:helloworld
hello('world','hello')#:worldhello
#python
    ,,

下面这些代码就是用"关键字参数"

1
2
3
4
5
6
defhello(a,b):
printa
printb
hello(a='hello',b='world')#:helloworld
hello(b='world',a='hello')#:helloworld
1
2
3
4
5
6
7
8
9
10
11
12
defhello(a='hello',b='world'):
printa+b
hello()#helloworld
hello(a='hello')#helloworld
hello(b='world')#helloworld
hello('hello')#helloworld!
hello('world')#worldworld!
hello(a='world')#worldworld!
hello(b='hello')#hellohello!
#,
#,,

默认参数需要注意!

    在重复调用函数时默认形参会继承之前依次调用结束后该形参的值,下面例子

1
2
3
4
5
6
7
defhello(a,L=[]):
L.append(a)
printL
hello('hello')#:['hello']
hello('hello')#:['hello','hello']
hello('hello')#:['hello','hello
    ,'hello']

收集参数: 需要传递多个参数,但是无法确定参数的个数时,可以使用收集参数

               使用收集参数只需要在参数前面加上 * 或者 ** 即可

 

1
2
3
4
5
6
7
8
defname(name,*name_1):
printu'%s'%name
forname_1inname_1:
printname_1
name('xiaoming')#xiaoming
name(u'',u'')#
name(u'',u'',u''
    )#

* 和 ** 都表示能接收任意多个参数

不同的是 * 表示将没有匹配的值放在同一个元组中, ** 表示将没有匹配的值放在同一个字典中

1
2
3
4
5
6
defhello(a,**d):
print'a=%d'%a
forxind:
printx+'=%d'%d[x]
hello(1,b=2,c=3)

for循环 

语法:    for...in...循环:依次把列表或元组的每个元素迭代出来

1
2
foriterating_varinsequence:#forin
statements(s)#

实例1:

1
2
3
4
5
6
7
8
9
fornamein'Python':
printu':',name
#:P
#:y
#:t
#:h
#:o
#:n

实例2:

1
2
3
4
5
6
7
8
#:namess#:names
names=[u'',u'',u'']#name=[u''
    ,u'',u'']
fornameinnames:#fornameinname:
printu':',name#printu'
    :',name
#,SS?
#:#:
#:#:
#:#:

 


 


0 0