Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 組み込み関数 | セクション
データ分析のためのPython基礎
セクション 1.  32
single

single

book組み込み関数

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

Python関数の世界へようこそ!この章では、Python開発者にとって不可欠なツールである、強力な組み込み関数のいくつかを紹介します。

まずは、Alexがこれらの重要な組み込み関数の使い方を実演する様子を見てみましょう。

組み込み関数とは?

組み込み関数は、Pythonにあらかじめ用意されている関数であり、追加の機能を記述することなくコード内で利用できます。これらの関数は、計算やデータ操作などの一般的な作業を効率的かつ簡潔に行うために設計されています。

Python開発者にとって、組み込み関数を使いこなすことは、クリーン効率的かつ簡潔なコードを書くための鍵となります。

Pythonには多くの組み込み関数があります。すでに print()len()range()type() などに触れたことがあるでしょう。ここでは、さらに一般的によく使われる組み込み関数を見ていきます:

  • sum(): イテラブル(リストなど)のすべての要素を加算し、合計値を返す関数。数値データの処理に特に便利。
123
checkout = [2.99, 5.49, 3.99] total = sum(checkout) print(total)
copy
  • max() および min(): それぞれイテラブル内の最大要素と最小要素を返す — 比較や極値の特定に最適;
123
freezer_temperatures = [38, 32, 41, 34, 40] print(max(freezer_temperatures)) print(min(freezer_temperatures))
copy
  • float(): 数値または数値を表す文字列を浮動小数点数(小数を含む数値)に変換
123456789
price1 = "3.99" price2 = 12 # Convert prices to float price1_converted = float(price1) price2_converted = float(price2) print(f"Price #1 is ${price1_converted} and is of type {type(price1_converted)}") print(f"Price #2 is ${price2_converted} and is of type {type(price2_converted)}")
copy
  • int(): 数値または数値を表す文字列を整数に変換。整数値を扱う場合や入力データを整数に変換する際に便利
12345678
price = 3.99 quantity = "4" # Calculate the total cost total_cost = int(quantity) * price print(f"The total cost for {quantity} items is ${total_cost}") print(f"Converting the total cost to an integer results in ${int(total_cost)}")
copy
Note
注意

浮動小数点値を整数に変換すると、小数部分は単純に切り捨てられます(値が切り捨てられます)。

  • sorted(): イテラブル(リスト、タプル、辞書など)から新しいソート済みリストを返します。sort()メソッドとは異なり、sorted()元のデータを変更せず、より多くの型に対応しています。
123456
fruit_prices = {"cherries": 3.99, "apples": 2.99, "bananas": 1.49} # Sorting the dictionary keys alphabetically sorted_prices = sorted(fruit_prices) print(sorted_prices)
copy
  • zip(): 2つ以上のイテラブル(例:リスト)をタプルの単一イテラブルに結合し、それぞれのイテラブルから要素を組み合わせる。
123456789
products = ["apple", "banana", "cherry"] prices = [0.99, 0.59, 2.99] stock = [50, 100, 25] # `zip()` combines the 3 lists into a series of tuples # `list()` converts the zip object into a list product_info = list(zip(products, prices, stock)) print("Product information:", product_info)
copy
タスク

スワイプしてコーディングを開始

価格と数量が文字列として保存されている辞書から商品データを処理。各商品の総売上を計算し、要約統計量を生成。

  • forループを使用してproducts辞書を反復処理。各反復で、商品名(キー)とその関連値(価格と数量を含むリスト)の両方にアクセス。これにより、各商品とそのデータを個別に処理可能;
  • 各商品について:
    • 価格float型に変換;
    • 販売数量int型に変換;
    • それらを掛け合わせて、その商品の総売上を算出;
    • 総売上をtotal_sales_listに追加。
  • sum()を使用して、すべての売上の合計を計算。
  • 合計値をtotal_sum変数に代入。
  • min()max()を使用して、最小および最大売上値を取得。
  • 最小値をmin_sales変数に代入。
  • 最大値をmax_sales変数に代入。

出力要件

  • 各商品について、次の形式で出力:
    Total sales for <product>: $<total_sales>
  • すべての商品を処理後、次を出力:
    • Total sum of all sales: $<total_sum>
    • Minimum sales: $<min_sales>
    • Maximum sales: $<max_sales>

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

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

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

セクション 1.  32
single

single

AIに質問する

expand

AIに質問する

ChatGPT

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

some-alt