Create a function in Python

I'll keep a note of how to create a function in Python.

Definition

Let the function name be function.

def function():
    pass
    #No arguments

argument

Pass data (int, str, class, etc.) to the function. Let the argument (hikisu) name be args.

def function(args):
    #With arguments
    print(args)

def function2(args1,args2):
    #You can take as many arguments as you like
    print(args,args2)

def function3(args="Hello World"):
    #Initial value can be set for the argument
    #If there are no arguments at the time of the call, args will"Hello World"Enter
    print(args)

Return value

Returns the result of processing in the function.

def function(args):
    #There is a return value
    n = args * 2 #Double the argument and return
    return n

def function2(args,args2):
    a = args + args2
    b = args - args2
    return (a,b) #Return as tuple type

Any type can be returned

call

To call a function, you can call it with the function name ().

def function():
    print("Hello World")

function()
#------------
#Hello World
#------------

If there is no argument, you do not have to write anything in (). I get an error when I write it.

TypeError: function() takes 0 positional arguments but 1 was given

argument

Arguments are written in ().

def function(args):
    print(args)

function("Hello World")
#------------
#Hello World
#------------

TypeError occurs if no argument is set.

TypeError: function() missing 1 required positional argument: 'args'

Return value

The return value can be put in a variable.

def function(args):
    n = args * 2 
    return n

n = function(5)#Assign the return value of the function function to the variable n
print(n)
#--------
#10
#--------

Other

You can also call another function from within a function.

def function(args):
    n = function2(args,10)#Calling the function2 function from within the function function
    print(n)    

def function2(args,args2):
    n = args * args2
    return n

function(5)
#--------
#50
#--------

Recommended Posts

Create a function in Python
Create a dictionary in Python
Create a DI Container in Python
Create a binary file in Python
Create a Kubernetes Operator in Python
Create a random string in Python
Create a Python function decorator with Class
Create a Python module
Precautions when pickling a function in python
Create SpatiaLite in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
[GPS] Create a kml file in Python
Create a Python environment
Create a Vim + Python test environment in 1 minute
Draw a graph of a quadratic function in Python
Create a GIF file using Pillow in Python
To execute a Python enumerate function in JavaScript
Get the caller of a function in Python
I want to create a window in Python
Create a standard normal distribution graph in Python
How to create a JSON file in Python
Create a virtual environment with conda in Python
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Create a package containing global commands in Python
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Take a screenshot in Python
Use callback function in Python
ntile (decile) function in python
Create gif video in Python
Make a bookmarklet in Python
Create a python numpy array
Nonlinear function modeling in Python
Draw implicit function in python
Immediate function in python (lie)
Draw a heart in Python
Create a directory with python
Create a data collection bot in Python using Selenium
python function ①
[LINE Messaging API] Create a rich menu in Python
[Python] function
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Create a fake Minecraft server in Python with Quarry
python function ②
What does the last () in a function mean in Python?
[Python] What is a zip function?
Write a binary search in Python
Implement R's power.prop.test function in python
Create user authentication function in Airflow
Function argument type definition in python
Hit a command in Python (Windows)
Create a CSV reader in Flask
Included notation in Python function arguments
Create a python GUI using tkinter
Create a Python environment on Mac (2017/4)