Pharmaceutical company researchers summarized functions in Python

Introduction

Here, we will explain "functions" for Python beginners. It is supposed to use Python3 series.

Creating a function

Use def when creating a function. The line following the def function name (formal argument 1, formal argument 2, ...): starts writing with indentation for 4 single-byte spaces. Write the return value as return. Be careful not to make the function name the same as the built-in function.

function_1.py


def hello_python(): #There is no need for formal arguments.
    return 'Hello, Python!'


def average(a, b): #Specify two formal arguments.
    return (a + b) / len([a, b])


def greeting(word, n = 3): #Specify the default value of the formal argument (second argument in this example).
    return (word * n)

Function call

After creating the function, call the function name where you want to execute it. Just creating a function does not mean that it will be executed.

function_2.py


print(hello_python())

print(average(1, 3))

print(greeting('Hello!', n = 2))
print(greeting('Bye!')) #If the second argument is not specified, the default value will be used.

Variable scope

Variables defined inside a function (local variables) cannot be used as the same object outside the function. Variables defined outside the function (global variables) can be used inside the function, but if you want to reflect the changes inside the function, you need to specify that they are global variables when defining the function.

function_3.py


def add_one(n):
    a = 1
    return n + a


print(add_one(2))
print(a) #An error occurs because the variable a is not defined.

function_4.py


a = 1


def add_one(n):
    return n + a


print(add_one(2))
print(a) #No error occurs.

function_5.py


a = 5


def add_one(n):
    a = 1 #Local variables
    return n + a


print(add_one(2))
print(a) #Changes in the value of variable a within the function are not reflected.

function_6.py


a = 5


def add_one(n):
    global a
    a = 1 #Global variables
    return n + a


print(add_one(2))
print(a) #Changes in the value of variable a within the function are reflected.

Summary

Here, I explained the basics of "functions" in Python. Particular attention should be paid to the scope of variables.

Reference materials / links

What is the programming language Python? Can it be used for AI and machine learning?

Recommended Posts

Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers have summarized the operators used in Python
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
Pharmaceutical company researchers summarized Python's data structures
How to install Python for pharmaceutical company researchers
Overriding library functions in Python
Python functions learned in chemoinformatics
[python] Manage functions in a list
Using global variables in python functions
Dynamically define functions (methods) in Python
Python functions
[Python3] Dynamically define global variables in functions
Easily use your own functions in Python
New features in Python 3.4.0 (3)-Single-dispatch generic functions
[Tips] Easy-to-read writing when connecting functions in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
A note on optimizing blackbox functions in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Practice applying functions and global variables in Python
Plink in Python
Constant in python
#Python basics (functions)
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
[Beginner] Python functions
StepAIC in Python
Get files, functions, line numbers running in python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python