2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

引数のデフォルト値

def greet(name="World"):
    print(f"Hello, {name}")

greet("John")
=> Hello, John

greet()
=> Hello, World

位置変数とキーワード引数

位置引数 ... 関数に引数を順番に渡す方法
キーワード引数 ... 引数に対応するパラメータ名を指定 (順番が違っても処理できる)

def introduce(name, age):
    print(f"私は{name}です、年齢は{age}です")

# 順番が違う
introduce(22, "マキマ")
=> 私は22です年齢はマキマです

introduce(age=20, name="デンジ")
=> 私はデンジです年齢は17です

可変長引数

関数で扱う引数の数が可変の場合

# *args => タプルとして扱われる
def print_args(*args): # タプル
    for arg in args:
        print(arg)

print_args("apple", "banana", "cherry", "melon")

# **kwargs => 辞書として扱われる
def print_kwargs(**kwargs):
    for key, value in kwargs.items():
        print(f"key: {key}, value: {value}")

print_kwargs(apple="green", banana="yellow", cherry="red")
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?