- Mastering Python Networking
- Eric Chou
- 154字
- 2021-07-02 21:42:33
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
>>>
推薦閱讀
- 樂高機器人設計技巧:EV3結構設計與編程指導
- Apache Spark 2 for Beginners
- Magento 2 Development Cookbook
- Mastering LibGDX Game Development
- Magento 1.8 Development Cookbook
- D3.js 4.x Data Visualization(Third Edition)
- Spring+Spring MVC+MyBatis整合開發實戰
- Building RESTful Python Web Services
- HTML5秘籍(第2版)
- 小型編譯器設計實踐
- 快速入門與進階:Creo 4·0全實例精講
- MyBatis 3源碼深度解析
- 邊玩邊學Scratch3.0少兒趣味編程
- Java程序設計教程
- Python深度學習與項目實戰