Python Basic Course (14 Modules and Packages)

module

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

Normal module

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.

Built-in module

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.

Module 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.

package

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.

scipy.PNG

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

Python Basic Course (14 Modules and Packages)
Python packages and modules
Understand Python packages and modules
Modules and packages in Python are "namespaces"
Python basic course (12 functions)
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Python basic course (9 iterations)
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Python Basic Course (Introduction)
Python basic course (13 classes)
Python basic course (8 branches)
Python Basic Course (3 Python Execution)
Organize python modules and packages in a mess
Python basic course (10 inclusion notation)
Python installation and basic grammar
Python Basic Course (5 List Tuples)
Python (Python 3.7.7) installation and basic grammar
Python Basic Course (1 What is Python)
Get an abstract understanding of Python modules and packages
MIDI packages in Python midi and pretty_midi
Java and Python basic grammar comparison
Introductory Python Modules and conditional expressions
Python virtual environment and packages on Ubuntu
[Python] Package and distribute your own modules
Python basic operation 3rd: Object-oriented and class
Python Basic Course (at the end of 15)
Python basic course (4 numeric type / character string type)
Differences between Ruby and Python (basic syntax)
RF Python Basic_01
Basic Python writing
Python3 basic grammar
RF Python Basic_02
Julia Quick Note [22] Calling Python functions and Python modules
Python3 Engineer Certification Basic Exam-Notes and Problem Trends-
[Python / Chrome] Basic settings and operations for scraping
Basic operation of Python Pandas Series and Dataframe (1)
Python basics basic course CSV processing (functions and classes, part 1 CSV is read and written)
[python] Compress and decompress
Python I'm also basic
Python basic grammar / algorithm
Python and numpy tips
[Python] pip and wheel
Basic sorting in Python
Batch design and python
[python] class basic methods
Vue-Cli and Python integration
Python --Explanation and usage summary of the top 24 packages
Ruby, Python and map
Python3 cheat sheet (basic)
Python basic grammar (miscellaneous)
python input and output
Using Python #external packages
Python and Ruby split
Python basic memorandum part 2
python basic on windows ②
Python basic memo --Part 2
Installing Python 3 on Mac and checking basic operation Part 1
Basic Python command memo
Python basic grammar note (4)