Python basics ③

Basic knowledge of Python ③. This is my study memo. Please do not excessive expectations.

function

-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

-argument When you call a function, you can pass a value to the function and call that value an argument

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

-scope Variables defined in formal parameters and functions can only be used in functions

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?

-Initial value of argument You can also set an initial value for the argument

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

-Multiple returns Multiple returns can be used in combination with conditional branching

Example


def hello(name):
    if name == 'The guests':
        return 'Please tell me your name'
    print(name + 'Welcome!')
print(hello(Aki))

module

-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

Python basics ⑤
Python basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of Python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Python basics ② for statement
Python
Basics of Python scraping basics
Python basics 8 numpy test
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
Basics of python: Output
python: Basics of using scikit-learn ①
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Basics of Python × GIS (Part 1)
kafka python
python + lottery 6
Python Summary
Built-in python
Paiza Python Primer 5: Basics of Dictionaries
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
python tips
Linux basics
python function ①
Python memo
ufo-> python (3)
install python
Python Singleton
NumPy basics
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python