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

Modules

Modules are just files that contain Python statements and definitions. A file that contains Python code (for example, sample.py) is called a module and its module name would be sample. Using modules, we can break larger programs into small and organized ones. An important feature of a module is re-usability. Instead of copying the definitions of the most used functions in different programs, you can define them in the module and just import them whenever needed.

Let's create a module and import it. We will create two scripts: sample.py and add.py. We will import a sample module in our add.py. Now, save the following code as sample.py. Let's take a look with the following example:

sample.py
def addition(num1, num2):
result = num1 + num2
return result

Here, we have defined a addition() function inside a module named sample. The function takes in two numbers and returns their sum. Now we have created a module. You can import this in any Python program.

主站蜘蛛池模板: 海宁市| 武乡县| 佳木斯市| 通州区| 米泉市| 永靖县| 长寿区| 当涂县| 雅江县| 格尔木市| 孙吴县| 施秉县| 宁城县| 治多县| 七台河市| 朝阳市| 泗洪县| 双辽市| 芦山县| 茂名市| 礼泉县| 准格尔旗| 凭祥市| 无极县| 兴国县| 岚皋县| 虹口区| 五河县| 乌拉特后旗| 祁连县| 石阡县| 同心县| 晴隆县| 枣阳市| 大渡口区| 宁乡县| 龙口市| 绥宁县| 安陆市| 万荣县| 永登县|