[Python of Hikari-] Chapter 08-03 Module (Import and use of standard library)

[Python] Chapter 08-03 Importing and using the standard library

Until now, I have imported modules and modules (packages) that I created. They also explained that modules created by others can also be imported.

In fact, there are other modules that come with Python when you install it, and this is called the ** standard library **. Also, some libraries were not installed with Python when it was installed. These are other third-party organizations, research groups, and personally created modules, which are called ** external libraries **. External libraries are described in the next section.

* </ font> Some libraries are composed of modules, and some are composed of packages.

First of all, I would like to explain the standard library in this section.

Various standard libraries

As mentioned above, the standard library is provided by Python. The following items mainly exist and are imported and used. (There are still other standard libraries)

module Use applications
math Processing related to calculations used in mathematics (log, sin,Functions such as cos are available)
random Used when you want to generate random numbers
csv Used for CSV file input / output
xml Used for XML file input / output
json Used for JSON file input / output
os Used when you want to operate the file directory etc.
datetime Used when you want to process the date and time
urllib Used when you want to get information from the Web
turtle Draw a graphic

There are other standard libraries as well. You can check the information about the standard library from the following URL. https://docs.python.org/ja/3/library/index.html

Among them, the turtle graphic was mentioned in Chapter 03.

I actually imported it as follows.

03-01-01.py


#Loading a program from the outside to run the turtle program
import turtle

#Make a turtle with the name taro.
taro = turtle.Turtle()

I won't go into turtle graphics in depth this time, but let's write a program using some other external libraries below.

Standard library (math module)

First, let's create a program to find the circumference and area of a circle using the ** math module **. The ** math module ** has details at the link below, so I would like to proceed with the creation and explain while referring to it. https://docs.python.org/ja/3/library/math.html

Create a file with the file name samp08-03-01.py </ font> in chap08 </ font> and use the following code Please write.

samp08-03-01.py


##Import math module
import math

##Function to find the circumference
def circumference_func(r):
    C = r * 2 * math.pi
    return C

##Function to find the area
def area_func(r):
    S = math.pow(r, 2) * math.pi
    return S

r = int(input('Enter the length r of the radius of the circle:'))

##Calling a function to find the circumference
print(f'Circumference length:{circumference_func(r)}')

##Calling a function to find the area
print(f'area:{area_func(r)}')

[Execution result] </ font> Enter the length r of the radius of the circle: 5 Circumference length: 31.41592653589793 Area: 78.53981633974483

I think there is no problem in finding the circumference and the area of the circle, but this time, I used the ** math.pi ** and ** math.pow () ** functions from the ** math module **. doing.

There are many functions in the math module at the link above, but let's take a look at each one.

[Quoted from the mathematical function pi manual]

math.pi Mathematical constant π = 3.141592 ... with as much precision as available (pi).

By setting ** math.pi **, that part becomes the circumference ratio (constant) of 3.1415 ...

[Quoted from the mathematical function pow manual]

math.pow(x, y) Returns x to the yth power. For exceptional cases, follow the C99 standard appendix'F' as much as possible. In particular, pow (1.0, x) and pow (x, 0.0) always return 1.0, even if x is zero or NaN. If both x and y are finite values, x is negative and y is not an integer, pow (x, y) is undefined and throws a ValueError. Unlike the built-in ** operator, math.pow () converts both arguments to a float type. Use ** or the built-in pow () function to calculate the exact power of an integer.

When calculating the area of a circle, r 2 </ sup> is used, so ** math.pow (r, 2) ** is used in the program.

If you know in advance what function to use when importing, you can enter the function name in the place to be imported as shown below to enter ** math.pi ** or ** math.pow (r). , 2) ** can be abbreviated as ** pi ** or ** pow (r, 2) **.

samp08-03-02.py


##Import math module
from math import pi, pow

##Function to find the circumference
def circumference_func(r):
    C = r * 2 * pi
    return C

##Function to find the area
def area_func(r):
    S = pow(r, 2) * pi
    return S

r = int(input('Enter the length r of the radius of the circle:'))

##Calling a function to find the circumference
print(f'Circumference length:{circumference_func(r)}')

##Calling a function to find the area
print(f'area:{area_func(r)}')

Standard library (random module)

Next, let's create a program by importing the ** random module ** that generates random numbers. The content is an Omikuji program that outputs one of Daikichi, Nakakichi, Kokichi, Kyou, and Kyou.

The ** random module ** has details at the link below, so I would like to proceed with the creation and explain while referring to it. Create a file with the file name samp08-03-02.py </ font> in chap08 </ font>, and use the following code Please write. https://docs.python.org/ja/3/library/random.html

samp08-03-03.py


##Import random module
import random

omikuji = random.randint(0, 4)

if omikuji == 0:
    print('Daikichi')
elif omikuji == 1:
    print('Nakayoshi')
elif omikuji == 2:
    print('Kokichi')
elif omikuji == 3:
    print('Bad')
else:
    print('Great villain')

[Execution result] </ font> Nakayoshi

  • Please note that the execution result changes with each execution.

I don't think there is a problem with the program, but this time I am using the ** random.randint () function **.

[Quoted from the randint function manual]

random.randint(a, b) Returns a random integer N such that a <= N <= b. An alias for randrange (a, b + 1).

This randint function returns an integer random number greater than or equal to ** a and less than b **. In the above execution result, the random number value happened to be 1, so "Nakayoshi" is output.

Finally

Among the standard libraries, this time I introduced the math module and the random module. The math module will be used more and more in the field of AI in the future, and the random module will be used by incorporating random number elements when creating simple games.

I will touch on other standard libraries (csv, datetime module, etc.) that I could not introduce this time and live.

Return to [Table of Contents Link]

Recommended Posts