問題一覧に戻る
中級モジュールとパッケージ
問題89: from...import文

from...import文を使うと、モジュールから特定の関数や定数だけを直接インポートできます。これによりモジュール名を付けずに関数を使用できますが、名前の衝突に注意が必要です。カンマで区切って複数の要素を同時にインポートすることも可能です。

# from...import文
random choice, randint

# インポートした関数を使用
fruits = ["apple", "banana", "orange"]
selected = (fruits)
number = (1, 10)

print(f"Fruit: {selected}, Number: {number}")