・ People who do not understand what the function in the program means ・ People who do not know how to use def
Functions are different from what you learned in mathematics. In short, if you are writing a long program, you may use the same process multiple times. Therefore, by grouping the programs that are used repeatedly with ** def **, the programs can be calculated in a short and easy-to-understand manner. In other words, the function can be used at any time by calling the frequently used program by name in def. The ** print () ** function is an example that you often use. You need to write longer code to display the characters in the command, but thanks to the definition of it as a function on the python side, this is just displaying the characters you want to display in the parentheses of the print function, and on the console It is possible to display characters.
First, I will give you the completed code, so I will explain it while looking at it.
def say_hello():
print('hello')
say_hello()
At the beginning of the function, write ** def **, open a space, add your favorite name, and then ** (): ** to complete the function definition. In () you can declare something like a variable that you can use inside a function. This is called an argument. You can run the function without adding it here. Next is the explanation on the second line. By adding indentation, python can recognize that it is a program in the function. (When the function is executed, the place with this indent is executed.) Finally, the third line. Here, I am writing a program that executes a function. In other words, this code is an instruction to execute a function called ** say_hello . This is the basic way to write a function. ⒈ The definition of the function is [ def space Favorite name (): **] ⒉ [Open indent and write process] ⒊ [Function name to be executed ():] If there is an argument you want to pass in the parentheses, describe it in the parentheses. You can use the function only with this. Finally, let's apply the function. ![Screenshot 2020-08-26 22.41.15.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/581846/d69bfa0b-b760-f94e-237b- 62c9571b9f38.png)
Is the code
#Define to use an external library
import matplotlib.pyplot as plt
#Define a function to draw a graph
def RIM(axisX, axisY):
plt.plot(axisX, axisY);
#0 in the list~Order the number of 9s in a list.
x = list(range(10))
y = list(range(10))
RIM(x,y)
By using a function like this, you can easily write a process at any time.
Recommended Posts