[Python] What is a formal argument? How to set the initial value

[Python] What is a formal argument? How to set the initial value

This is a memo.

What is a formal argument?

The name of the variable to put in parentheses when you define the function. The title is python, but the same name is used in other languages such as php.

#↓ ↓ ↓ Formal parameters: name, age

def birthday(name, age):
    print(f'{name}Mr.{age}Happy birthday')

Initial value of formal argument

Initial values can be set for formal arguments.

If there is no initial value, an error will occur if there are not enough arguments to pass. If there is an initial value, no error will occur.

▼ Function with initial value set

def birthday(name='TODOROKI', age="24"):
    print(f'{name}Mr.{age}Happy birthday')

#No arguments
birthday()
>Happy 24th birthday, TODOROKI


#Specify only one argument
birthday('AAA')
>Happy 24th birthday, AAA


#Specify which argument
birthday(age='31')
>Happy 31st birthday, TODOROKI

error


#No initial value of formal argument
def birthday(name, age):
    print(f'{name}Mr.{age}Happy birthday')


#Call with no arguments
birthday()
>TypeError: birthday() missing 2 required positional arguments: 'name' and 'age'

Error type: TypeError Content: Missing two arguments

Recommended Posts

[Python] What is a formal argument? How to set the initial value
How to get the last (last) value in a list in Python
What to do when the value type is ambiguous in Python?
[Introduction to Python] What is the difference between a list and a tuple?
What is the fastest way to create a reverse dictionary in python?
How to retrieve the nth largest value in Python
How to set up a Python environment using pyenv
[Introduction to Udemy Python3 + Application] 30. How to use the set
What is a python map?
[Introduction to Python] What is the recommended way to install pip, a package management system?
How to check in Python if one of the elements of a list is in another list
I want to initialize if the value is empty (python)
How to use the __call__ method in a Python class
Extract the value closest to a value from a Python list element
[Introduction to statistics] What kind of distribution is the t distribution, chi-square distribution, and F distribution? A little summary of how to use [python]
[Python] What is a zip function?
[Python] What is a with statement?
How to write a Python class
[Python] What is @? (About the decorator)
How to get the Python version
[python] What is the sorted key?
[Python] How to import the library
What is the python underscore (_) for?
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
How to determine the existence of a selenium element in Python
[Introduction to Python] What is the most powerful programming language now?
[Introduction to Python] How to split a character string with the split function
How to give and what the constraints option in scipy.optimize.minimize is
How easy is it to synthesize a drug on the market?
How to check the memory size of a variable in Python
How to judge that the cross key is input in Python3
How to get a string from a command line argument in python
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
[Python] How to get a value with a key other than value with Enum
How to utilize Python with Jw_cad (Part 1 What is external transformation)
How to send a request to the DMM (FANZA) API with python
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
[Python] What is pip? Explain the command list and how to use it with actual examples
[Pandas] What is set_option [How to use]
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
How to set the server time to Japanese time
[Python] How to invert a character string
How to get a stacktrace in python
What is "mahjong" in the Python library? ??
What is a dog? Python installation volume
[python] [meta] Is the type of python a type?
How to use is and == in Python
How to run a Maya Python script
What to do if there is a decimal in python json .dumps
What is the XX file at the root of a popular Python project?
[Python] Explains how to use the range function with a concrete example
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to get a value from a parameter store in lambda (using python)
How to fix the initial population with a genetic algorithm using DEAP
[Python] How to set the (client) window size inside the browser with Selenium
[Introduction to Python] How to write a character string with the format function
[Development environment] How to create a data set close to the production DB