Basic Python operation 2nd: Function (argument)

#Python basic operation

Introduction

Review the basic operations of Python only for the parts that you are uneasy about. As the second step, we describe the function.

Functions in Python

python


def function name(argument):
Processing content

Although defined in this way, I personally didn't pay much attention to the arguments. Here, we will deepen our understanding of functions by paying attention to the arguments.

argument

There are two main types of arguments. -Dummy argument: Argument declared by the function -Actual argument: Argument to be passed to the function

example.py


#Function definition
def main(a):
    return a+1

#Use of functions
main(x)

In the above code, a is a formal argument and x is an actual argument.

Passing to formal arguments

When using a function, the actual argument is specified, but it means that the value is passed to the formal argument.

How to deliver?

There are two types of delivery methods. -Pass by value: Copy the actual argument to the formal argument (the value of the actual argument is not changed) -Pass by reference: Refers to the address of the actual argument (the value of the actual argument can be changed)

Arguments in Python

Passing by reference is basically used However... It behaves differently depending on the variable in the actual argument. It can be further divided into two according to its operation.

immutable type: cannot be rewritten in a function

✓ Integers, floating point numbers, character strings, tuples, etc.

mutable type: can be rewritten in a function

✓ List, dictionary type (dict), set (set), etc.

Verification

I verified whether the variable used for the actual argument is really rewritten by the following code.

def_arg.py


"""
2020/12/13
@Yuya Shimizu

Function arguments
"""
#Define function
def func_num(a):
    a -= 1
    return a

def func_char(a):
    a += " Hello"
    return a

def func_list(a):
    a[0] -= 1
    return a

def func_dict(a):
    a["1"] -= 1
    return a

##############immutable type
#Actual argument: Integer
x_int = 1
func_num(x_int)
print("Actual argument", x_int)

#Actual argument: Floating point number
x_float = 1.5
func_num(x_float)
print("Actual argument", x_float)

#Actual argument: string
x_char = "abc"
func_char(x_char)
print("Actual argument", x_char)

##############mutable type
#Actual arguments: list
x_list = [1, 2, 3]
func_list(x_list)
print("Actual argument", x_list)

#Actual argument: dictionary type(dict)
x_dict = {"1":10, "2":20, "3":30}
func_dict(x_dict)
print("Actual argument", x_dict)

inspection result

Certainly, the data of immutable type actual arguments did not change even after the function was executed, and the mutable type actual arguments were affected by the function execution.

Impressions

I've been using functions, but I'm not aware that there are such rules for arguments, and I'm refreshed to realize that it was one of the causes of errors I didn't understand until now.

References

Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms Written by Toshikatsu Masui, Shoeisha

Recommended Posts

Basic Python operation 2nd: Function (argument)
[python] Callback function (pass function as argument)
python function ①
[Python] function
python function ②
Python basic operation 1st: List comprehension notation
Python basic operation 3rd: Object-oriented and class
RF Python Basic_01
python enumerate function
[python] vector operation
Python> function> Closure
[Python] Generator function
Basic Python writing
Python OS operation
Python3 basic grammar
Python> function> Inner function
RF Python Basic_02
Python function decorator
Basic operation list of Python3 list, tuple, dictionary, set
Basic operation of Python Pandas Series and Dataframe (1)
[Python] Operation of enumerate
Python basic course (12 functions)
Python I'm also basic
Python basic grammar / algorithm
About function arguments (python)
Python default argument trap
Python basic course (2 Python installation)
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Basic sorting in Python
Function execution time (Python)
Python basic course (9 iterations)
Basic operation of pandas
Python default argument behavior
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Basic operation of Pandas
Python3 cheat sheet (basic)
Python basic grammar (miscellaneous)
Python Basic Course (Introduction)
Today's python error: invalid keyword argument for this function
Python basic memorandum part 2
Python print function (sequel)
Python: About function arguments
python basic on windows ②
Python directory operation summary
Time floor function (Python)
Installing Python 3 on Mac and checking basic operation Part 1
Python basic course (13 classes)
Basic Python command memo
Python basic grammar note (4)
Python basic grammar note (3)
Basic knowledge of Python
Python basic grammar memo
OpenCV basic code (python)
Python decorator operation memo
Python basic memo --Part 1
python memorandum super basic
Python basic course (8 branches)
Python basic if statement
[python] Array slice operation
Python Basic Course (3 Python Execution)