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

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
>>>
主站蜘蛛池模板: 黄山市| 南阳市| 绥宁县| 徐州市| 清新县| 武汉市| 丰镇市| 遂宁市| 南川市| 德州市| 南陵县| 屏南县| 佛教| 巴南区| 韩城市| 韶关市| 仙桃市| 南平市| 葫芦岛市| 西畴县| 绥宁县| 承德市| 雅安市| 桂林市| 永善县| 新乐市| 民和| 桃园市| 拉萨市| 合肥市| 元阳县| 吉安县| 六盘水市| 邢台县| 闽侯县| 江都市| 商都县| 大足县| 淄博市| 衢州市| 南京市|