A file in which you write code so that it can be reused by other programs is called a "module". In addition to the usual modules you develop yourself using the Python language
There is a famous sequence called the Fibonacci sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, … Although it is a sequence of numbers, it has a certain rule.
0 + 1 = 1 1 + 1 = 2 1 + 2 = 3 3 + 5 = 8 5 + 8 = 13 …
As you can see, the sum of the Nth number and the N + 1st number is the N + 2nd number. Let's create a module fibonatti.py that contains a function to find the Fibonacci sequence up to the given argument.
fibonatti.py
def fibo(n):
result = []
a = 1
b = 1
while b < n:
result.append(b)
val = a + b
b = a
a = val
return result
if __name__ == "__main__":
print("{0}".format(fibo(100)))
It is written at the end of the program
python
"If this module is executed by itself (rather than being called by another program)"
It means that. Modules are called and used by other programs,
Of course, you need a test to test if it works properly.
If you call the fibo function in this if statement and confirm that it works correctly, the module is complete.
The program that used the created module in another program is as follows.
#### **`print_fibo.py`**
```python
import fibonatti
print("{0}".format(fibonatti.fibo(100)))
To load a module, write ** import ** * module file name * at the beginning. If you want to use it, you can use it with * module name.method name () *.
Python has many useful modules created by programmers around the world (including Python developers). By using them, you can easily create advanced and convenient programs. Thank you. One of the reasons Python is so popular is that it has many useful modules.
Here, we will introduce the math module, which is a convenient module. The math module provides numbers and functions for use in mathematical function calculations.
math_explain.py
import math
print("pi = {0}".format(math.pi))
print("floor(5.2) = {0}".format(math.floor(5.2)))
print("ceil(5.2) = {0}".format(math.ceil(5.2)))
The execution result is as follows.
pi = 3.14159265359 floor(5.2) = 5.0 ceil(5.2) = 6.0
pi returns the value of pi. floor (decimal) returns the largest integer less than or equal to the argument decimal. ceil (decimal) returns the smallest integer greater than or equal to the argument decimal. There are many other useful numbers and functions available.
I've used embedded modules in the programs introduced in the "Python Basics" series so far.
There are others, but these built-in modules do not require import.
I wrote import fibonatti earlier to use the Fibonacci function in the program, Importing a module into a file and making it available is called importing. I wrote ** import ** * module file name * to import one module, When importing multiple modules ** import module 1, module 2, ... ** You can import them separated by commas. You can also import only the specified members (members, that is, functions, constants, classes) in the module. Check and execute the following program.
import_part.py
from math import cos,sin,tan
print("{0}".format(cos(1)))
** from ** * Module name * ** import ** * Only members specified by members, members,… * can be imported. Members imported in this way can be called without giving a module name. In the above program, it can be called only by cos, not math.cos.
In Python, if a folder has ** \ _ \ _ init__.py ** (two "\ "), that folder can be treated as a "package". The contents of \ _ \ _ init_.py can be empty. Combine multiple modules with similar functions into one folder or classify them into subfolders You can make one multifunctional package.
The image above is a scipy package of modules for scientific computing installed on your PC. \ _ \ _ Init__.py is placed directly under the scipy folder, and subfolders are created for each purpose in the same hierarchy. Module files are stored in each folder.
The package import method is the same as the module import method.
import scipy
It can be imported with.
Here's how to import all the modules in a particular subfolder in a package.
import scipy.cluster
To import a specific module in a package, write ** import ** * package name (.subpackage name).module name *.
#### **`import scipy.special.specfun`**
Alternatively, you can import with ** from ** * package name * ** import ** * module name * just like a module.
from scipy.special import specfun
Next: [Python Basic Course (at the end of 15)](http://qiita.com/Usek/items/c44391198d33c55332d7)
Recommended Posts