一步一步学python(六) - 抽象


1、string转数字

import locale

locale . atoi( str )

2、创建函数

函数是可以调用(可能包含参数),执行某种行为并返回一个值

>>>import math

>>>x = 1

>>>y = math.sqrt

>>>cllable( x )

false

>>>callable( y )

True

使用def定义一个函数

def hello( name ):

return ' Hello,' + name + ' ! '

这样就定义了一个hello函数

3、记录函数

def square( x )

' calculates the square of the number x .'

return x*x

文档可以按如下方式访问

square._doc_

'calculates the square of the number x.'

内建函数help很有用。在交互式解释器中可以得到函数的信息

help(square)


4、关键字参数和默认值、

5、收集参数

def print ( *param )

参数前的*把将所有的值放在一个元组

6、反转过程

7、作用域

除了全局作用域,每个函数都会创建一个作用域

8、递归

函数调用自身称为递归



评论关闭