1
0

Pythonで「関数で可変個数の引数を受け取る」の動作を確認してみた

Posted at

概要

Pythonで「関数で可変個数の引数を受け取る」の動作を確認してみました。
以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
def myfunc(num1, *tupple1):
    print(tupple1)

myfunc(10)
myfunc(10, "Hello")
myfunc(10, "Hello", 25)
myfunc(10, "Hello", 25, "Orange")
myfunc(10, "Hello", 25, "Orange", True)

def myfunc(category, *ranktuple):
    count = 1
    print(category)
    for val in ranktuple:
        print("No." + str(count) + ":" + val)
        count += 1
    print("\n", end='')

myfunc("Fruits", "Orange", "Melon", "Banana", "Apple")
myfunc("Mobile", "Android", "iPhone")

以下のコマンドを実行しました。

$ python3 sample.py 
()
('Hello',)
('Hello', 25)
('Hello', 25, 'Orange')
('Hello', 25, 'Orange', True)
Fruits
No.1:Orange
No.2:Melon
No.3:Banana
No.4:Apple

Mobile
No.1:Android
No.2:iPhone

まとめ

何かの役に立てばと。

1
0
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
1
0