Let's use def in python

Who is the target of this article

・ People who do not understand what the function in the program means ・ People who do not know how to use def

Who is a function?

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.

Let's actually write the code! !!

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

Let's use def in python
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use profiler in Python
Let's use python janome easily
Use let expression in Python
Use Measurement Protocol in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Let's find pi in Python
Let's use the open data of "Mamebus" in Python
Use fabric as is in python (fabric3)
Let's run "python -m antigravity" in python
How to use SQLite in Python
Let's try Fizz Buzz in Python
Use rospy with virtualenv in Python3
[Introduction to Python] Let's use pandas
How to use Mysql in python
Use Python in pyenv with NeoVim
Let's see using input in python
How to use ChemSpider in Python
How to use PubChem in Python
[Introduction to Python] Let's use pandas
Use OpenCV with Python 3 in Window
[Introduction to Python] Let's use pandas
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
[Introduction to Python] How to use class in Python?
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Use print in a Python2 lambda expression
Meta-analysis in Python
Unittest in python
Easily use your own functions in Python
Let's use pytube
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
[Introduction to Python] Let's use foreach with Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Easy way to use Wikipedia in Python
Plink in Python
Constant in python
Don't use \ d in Python 3 regular expressions!
Lifegame in Python.
FizzBuzz in Python
How to use __slots__ in Python class