Basic knowledge of Python ③. This is my study memo. Please do not excessive expectations.
-A function is a block of programs that summarizes a certain process.
-How to make a function
Function definition
def function name(): #Colon at the end of the line
Process to be executed
#Align the indents (4 half-width spaces)
Example
def hello(): #Colon at the end of the line
print('Hello World')
#Align the indents (4 half-width spaces)
hello()
#Output result → Hello World
Definition of the function that receives the argument
def function name(Formal argument): #Colon at the end of the line
Process to be executed
#Align the indents (4 half-width spaces)
Example
def hello(name): #Colon at the end of the line
print('Hello' + name)
#Align the indents (4 half-width spaces)
hello('Aki') #Aki is assigned to the formal argument name
#Output result → Hello Aki
Example
def hello(name):
print(name) #Can be used because it is within the scope of the variable name and within the function
print(name) #An error occurs because it cannot be used outside the scope of the variable name.
-Multiple arguments
The arguments are called " 1st argument, 2nd argument ... "
in order from the left.
Example
def hello(name, message): #Colon at the end of the line,Separate the arguments with a comma
print('Hello' + name + message)
#Align the indents (4 half-width spaces)
hello('Aki', 'energy?') # Akiが、仮引数nameに、energy?が、仮引数messageに代入される
#Output result → Hello Aki How are you?
Example
def hello(name, message = 'Good morning!'): #Colon at the end of the line
print(name + 'Mr.' + message)
#Align the indents (4 half-width spaces)
hello('Aki') #Aki is assigned to the formal argument name
#Output result → Good morning, Mr. Aki!
-Return value Return the processing result to the caller
Definition of the function that receives the argument
def function name(): #Colon at the end of the line
return Return value
#Return to caller
Example
def validate(hand):
if hand < 0 or hand > 2:
return False
#Return to caller
The nature of -return return not only returns the return value to the caller, but also has the property of terminating the processing in the function. Therefore, the processing of the function after return is not executed.
Example
def hello(name):
if name == 'The guests':
return 'Please tell me your name'
print(name + 'Welcome!') #Not executed because it is after return
Example
def hello(name):
if name == 'The guests':
return 'Please tell me your name'
print(name + 'Welcome!')
print(hello(Aki))
-A module is a file in which code is written.
-import
Modules can be loaded by using import
It can be read by writing " import module name "
The module name is the file name with the extension (.py) removed.
Example
#File name → sample.py
import sample
-How to use the module In the above, it just reads the file and does not do what is written inside By writing `" module name.function name () ", you can execute the inside look written in the module.
Example
#File name → sample.py
#Function name → validate(hand)
import sample
if sample.validate(hand)
-Also, Python already has some useful modules "Random" = module that generates random values "Math" = Module for complex operations "Datetime" = Module for manipulating date and time data
Recommended Posts