官术网_书友最值得收藏!

Python functions

Most of the time when you find yourself reusing some pieces of code, you should break up the code into a self-contained chunk as functions. This practice allows for better modularity, is easier to maintain, and allows for code reuse. Python functions are defined using the def keyword with the function name, followed by the function parameters. The body of the function consists of Python statements that are to be executed. At the end of the function, you can choose to return a value to the function caller, or by default, it will return the None object if you do not specify a return value:

    def name(parameter1, parameter2):
statements
return value

We will see a lot more examples of function in the following chapters, so here is a quick example:

    >>> def subtract(a, b):
... c = a - b
... return c
...
>>> result = subtract(10, 5)
>>> result
5
>>>
主站蜘蛛池模板: 涞源县| 石嘴山市| 乌审旗| 曲松县| 喀什市| 土默特左旗| 舒兰市| 乐陵市| 新宁县| 罗平县| 济源市| 新密市| 洮南市| 鸡泽县| 文化| 滦平县| 商丘市| 四平市| 星座| 高碑店市| 绥棱县| 平山县| 嘉兴市| 大洼县| 宣恩县| 磐安县| 遵义市| 左权县| 林芝县| 许昌市| 三原县| 阿合奇县| 安福县| 石棉县| 盐池县| 天台县| 沅江市| 鲁甸县| 手机| 游戏| 三门县|