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

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.

主站蜘蛛池模板: 河津市| 兴海县| 衡阳市| 科技| 枣强县| 泰来县| 安西县| 绍兴县| 久治县| 淮阳县| 巴彦县| 台中市| 武宁县| 扎赉特旗| 洪泽县| 晋江市| 南皮县| 武功县| 靖远县| 吉木萨尔县| 攀枝花市| 余江县| 霍城县| 明水县| 博罗县| 石嘴山市| 昌图县| 大悟县| 息烽县| 海原县| 兰州市| 晋宁县| 晴隆县| 吐鲁番市| 清水河县| 西平县| 乌海市| 盐亭县| 万盛区| 宜州市| 射洪县|