Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 戻り値のない関数 | セクション
データ分析のためのPython基礎

book戻り値のない関数

メニューを表示するにはスワイプしてください

return文を持たない関数は、メッセージの表示データの変更、またはプログラム内でアクションの実行などのタスクを実行する再利用可能なセクションとしてコードを構造化したい場合に役立ちます。

Note
注意

Python では、すべての関数が値を返します。関数に明示的な return 文が含まれていない場合、自動的に None が返されます。

Alex が値を返さない関数の作成と使用方法をどのように示しているか見てみましょう:

return文のない関数の使い方を理解する最良の方法は、実際の例を見ることです。いくつかの例を見てみましょう。

コンソールへの情報出力

関数の目的が、特定のイベントや結果をユーザーに通知するためにコンソールへ情報を出力することだけの場合があります。

例えば、total_sales()関数では値を返す必要はありません。主な役割は計算を行い、その結果をすぐに表示することだからです。

1234567
# Prices of items sold today prices = [12.99, 23.50, 4.99, 8.75, 15.00] def total_sales(prices): print(f"Today's total sales: $", sum(prices)) total_sales(prices)
copy
Note
注意

データや関数の宣言順序は重要ではありません。唯一の重要なルールは、関数は呼び出される前に定義されていなければならないということです。

データ構造の変更

開発者は、リストや辞書などのデータ構造を変更する関数を、値を返さずに作成する必要がある場合がよくあります。

例えば、update_inventory() 関数は items_sold に基づいて在庫数を調整します。この関数は**inventory 辞書を直接変更する**ため、何も返す必要はありません。

12345678910111213141516171819202122232425
# Define the function that adjusts inventory levels def update_inventory(inventory, items_sold): # Iterate over each item in the dictionary for product, quantity_sold in items_sold.items(): # Decrease the inventory by the quantity sold for each product inventory[product] -= quantity_sold # Inventory dictionary inventory = { "apples": 50, "bananas": 75, "oranges": 100 } # Items sold dictionary items_sold = { "apples": 5, "oranges": 15 } # Update the inventory based on items sold update_inventory(inventory, items_sold) # Display the updated inventory print("Updated inventory:", inventory)
copy

別の関数の呼び出し

特定の条件を監視し、必要に応じて他の関数を呼び出す関数を作成することは一般的です。

例えば、check_stock_levels() 関数は商品の在庫数が設定したしきい値を下回っているかどうかを確認します。もし下回っていれば、restock() 関数を呼び出して追加の在庫を注文します。

この方法では値を返す必要はなく、主な目的は補充プロセスを開始することです:

12345678910111213141516171819202122232425
# Dictionary representing the current stock of products inventory = { "apples": 17, "bananas": 75, "oranges": 2, "grapes": 50 } # Function to restock items that have low stock levels by adding a specified amount def restock(product, inventory, restock_amount): inventory[product] += restock_amount print(f"Restock order placed for {product}. New stock level: {inventory[product]} units.") # Function to check which items are below the stock threshold and trigger the `restock` function def check_stock_levels(inventory, threshold): for product, quantity in inventory.items(): if quantity < threshold: # If the stock is below the threshold, call the `restock` function to add 50 units restock(product, inventory, 50) # Checking the stock levels for all products in the inventory with a threshold of 30 units check_stock_levels(inventory, 30) # Display the final inventory after restocking print("Final inventory status:", inventory)
copy
question mark

Python では、すべての関数が値を返します。関数に return 文がない場合、何が返されますか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  36

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  36
some-alt