リキャップ
メニューを表示するにはスワイプしてください
おめでとうございます。このPythonコースの最終セクションを修了しました!関数の仕組みや、食料品店の運営管理など現実世界のシナリオにどのように応用できるかについて、貴重な知識を身につけました。
ここで、学習内容を簡単に振り返ります。
組み込み関数
Pythonでいくつかの重要な組み込み関数(sum()、max()、min()、float()、int()、sorted()、zip()など)を学びました。これらの関数は、合計の計算やデータ型の変換など、一般的な作業を簡単にします。
1234# Using sum() to calculate the total cost prices = [2.99, 1.99, 3.49, 2.50] total_cost = sum(prices) print(f"Total cost: ${total_cost}")
ユーザー定義関数
独自の関数を作成してロジックをカプセル化し再利用する方法を学習。たとえば在庫補充の計算など。複雑なプログラムでコードを整理し効率化するために重要なスキル:
1234567# Defining a function to calculate restocking needs def restock_quantity(current_stock, desired_stock): restock_qty = desired_stock - current_stock return max(restock_qty, 0) restock_needed = restock_quantity(10, 25) print(f"Restock needed: {restock_needed} units")
戻り値のない関数
値を返さずに処理を実行する関数について学習。たとえばデータ構造の更新や結果の直接出力など。このタイプの関数は、既存データの変更やユーザーへの即時フィードバックに有用:
123456789# Function to update inventory without returning a value def update_inventory(inventory, items_sold): for product, quantity in items_sold.items(): inventory[product] -= quantity print(f"Updated {product} stock: {inventory[product]} units") inventory = {"Milk": 50, "Bread": 30} items_sold = {"Milk": 5, "Bread": 10} update_inventory(inventory, items_sold)
デフォルト引数とキーワード
デフォルト引数やパラメータキーワードを使用することで、関数を柔軟かつさまざまな状況に適応できるようにする高度なテクニックを学習。これらのテクニックにより、関数の汎用性が向上。
1234567891011def calculate_final_cost(items, tax_rate=0.07): subtotal = sum(items.values()) tax = subtotal * tax_rate total = subtotal + tax return total products = {"Milk": 2.99, "Bread": 1.79, "Eggs": 3.49} # Passing a dictionary as a single argument final_total = calculate_final_cost(products) print(f"Final total with tax: ${final_total}")
1. 次の組み込み関数のうち、商品の価格リストから最小値を見つけるために使用するものはどれですか?
2. return文のない関数を定義し、その関数を呼び出した場合、どうなりますか?
3. 次の文は正しいですか:calculate_discount(100) を呼び出すと、引数が1つしか指定されていないためエラーになる(関数は2つの引数が必要)?
4. 次の関数を discount パラメータを指定せずに呼び出した場合、discount のデフォルト値は何ですか?
すべて明確でしたか?
フィードバックありがとうございます!
セクション 6. 章 8
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 6. 章 8