Python3 エンジニア認定基礎試験 練習問題 Day22:組み込み関数(sum,max,min,sorted)

【本日のミッション】

Day21では「組み込み関数(len, type, range)」を学びました。
今日は 数値やリストを処理する便利な組み込み関数 を学んでいきましょう。


よく使う組み込み関数(数値・リスト編)

sum(合計を求める)

numbers = [1, 2, 3, 4]
print(sum(numbers)) 

出力結果

10

リストやタプルなどの合計を返します。


max(最大値を求める)

numbers = [1, 7, 3, 5]
print(max(numbers)) 

出力結果

7

最大の要素を返します。文字列にも使えます。


min(最小値を求める)

numbers = [1, 7, 3, 5]
print(min(numbers)) 

出力結果

1

最小の要素を返します。


sorted(並べ替え)

numbers = [3, 1, 4, 2]
print(sorted(numbers))
print(sorted(numbers, reverse=True)) 
print(numbers)

出力結果

[1, 2, 3, 4]
[4, 3, 2, 1]
[3, 1, 4, 2]

新しいリストを返し、元のリストは変更されません。


練習問題

次のコードを実行するとどうなるでしょうか?

numbers = [8, 3, 5, 2]

print(sum(numbers))
print(max(numbers))
print(min(numbers))
print(sorted(numbers))

選択肢

A)
18
8
2
[2, 3, 5, 8]
B)
18
5
2
[8, 5, 3, 2]
C)
17
8
3
[2, 3, 5, 8]
D)
エラーになる

Python3 エンジニア認定基礎試験 練習問題 Day22:組み込み関数(sum,max,min,sorted)

解答

A)
18
8
2
[2, 3, 5, 8]

■■■スポンサーリンク■■■

解説

numbers = [8, 3, 5, 2]

要素は [8, 3, 5, 2]

print(sum(numbers))

合計は 8 + 3 + 5 + 2 = 18

print(max(numbers))

最大値は 8

print(min(numbers))

最小値は 2

print(sorted(numbers))

昇順に並び替えると [2, 3, 5, 8]

👉 正解は

A)
18
8
2
[2, 3, 5, 8]

✅ ポイント

  • sum() → 合計を求める

  • max() → 最大値を求める

  • min() → 最小値を求める

  • sorted() → 並べ替え(新しいリストを返す)

リストの処理で頻出の関数なので、必ず押さえておきましょう!


次回予告

Day23では 「文字列と組み込み関数(len, str, int, float など)」 を学びます。
データ型変換や文字列処理に役立つ関数を整理しましょう!


参考

Python 3 エンジニア認定基礎試験 – Odyssey CBT
Python3 エンジニア認定基礎試験 出題範囲と学習プラン
Python3 エンジニア認定基礎試験 練習問題 Day1:変数の基本
Python3 エンジニア認定基礎試験 練習問題 Day2:変数の型(int型とstr型)
Python3 エンジニア認定基礎試験 練習問題 Day3:算術演算子と代入演算子
Python3 エンジニア認定基礎試験 練習問題 Day4:文字列の操作
Python3 エンジニア認定基礎試験 練習問題 Day5:比較演算子と論理演算子
Python3 エンジニア認定基礎試験 練習問題 Day6:if文(条件分岐)
Python3 エンジニア認定基礎試験 練習問題 Day7:ループ処理(for文・while文)
Python3 エンジニア認定基礎試験 練習問題 Day8:break文とcontinue文
Python3 エンジニア認定基礎試験 練習問題 Day9:リスト(list)の基本
Python3 エンジニア認定基礎試験 練習問題 Day10:リストの操作(append,remove,len など)
Python3 エンジニア認定基礎試験 練習問題 Day11:リストのスライス(部分取り出し)
Python3 エンジニア認定基礎試験 練習問題 Day12:タプル(tuple)の基本
Python3 エンジニア認定基礎試験 練習問題 Day13:辞書(dict)の基本
Python3 エンジニア認定基礎試験 練習問題 Day14:集合(set)の基本
Python3 エンジニア認定基礎試験 練習問題 Day15:関数の定義と呼び出し
Python3 エンジニア認定基礎試験 練習問題 Day16:関数のデフォルト引数とキーワード引数
Python3 エンジニア認定基礎試験 練習問題 Day17:可変長引数(*args, **kwargs)
Python3 エンジニア認定基礎試験 練習問題 Day18:関数のスコープ(ローカル変数とグローバル変数)
Python3 エンジニア認定基礎試験 練習問題 Day19:ネスト関数(関数の中の関数)
Python3 エンジニア認定基礎試験 練習問題 Day20:ラムダ式(無名関数)
Python3 エンジニア認定基礎試験 練習問題 Day21:組み込み関数(len, type, range など)
Python3 エンジニア認定基礎試験 練習問題 Day22:組み込み関数(sum,max,min,sorted)
Python3 エンジニア認定基礎試験 練習問題 Day23:文字列と組み込み関数(len,str,int,float)
Python3 エンジニア認定基礎試験 練習問題 Day24:組み込み関数(abs, round, pow)
Python3 エンジニア認定基礎試験 練習問題 Day25:組み込み関数(sorted の応用と key 引数)
Python3 エンジニア認定基礎試験 練習問題 Day26:組み込み関数(enumerate, zip)
Python3 エンジニア認定基礎試験 練習問題 Day27:組み込み関数(map, filter, reduce)
Python3 エンジニア認定基礎試験 練習問題 Day28:組み込み関数(any, all)
Python3 エンジニア認定基礎試験 練習問題 Day29:組み込み関数(isinstance,issubclass)
Python3 エンジニア認定基礎試験 練習問題 Day30:組み込み関数(callable, eval)
Python3 エンジニア認定基礎試験 練習問題 Day31:組み込み関数(dir, help)
Python3 エンジニア認定基礎試験 練習問題 Day32:組み込み関数(id, hash)
Python3 エンジニア認定基礎試験 練習問題 Day33:組み込み関数(repr, format)
Python3 エンジニア認定基礎試験 練習問題 Day34:組み込み関数(globals, locals)
Python3 エンジニア認定基礎試験 練習問題 Day35:組み込み関数(vars, callable)
Python3 エンジニア認定基礎試験 練習問題 Day36:組み込み関数(reversed, slice)
Python3 エンジニア認定基礎試験 練習問題 Day37:組み込み関数(getattr, setattr, hasattr, delattr)
Python3 エンジニア認定基礎試験 練習問題 Day38:組み込み関数(eval, exec, compile)
Python3 エンジニア認定基礎試験 練習問題 Day39:組み込み関数(zip の応用・辞書との組み合わせ)
Python3 エンジニア認定基礎試験 練習問題 Day40:モジュールの基本(import と from の違い)
Python3 エンジニア認定基礎試験 練習問題 Day41:as(別名インポート)と import の仕組み
Python3 エンジニア認定基礎試験 練習問題 Day42:標準ライブラリ(math モジュールの基本)
Python3 エンジニア認定基礎試験 練習問題 Day43:標準ライブラリ(random モジュールの活用)
Python3 エンジニア認定基礎試験 練習問題 Day44:標準ライブラリ(datetime モジュールの使い方)
Python3 エンジニア認定基礎試験 練習問題 Day45:標準ライブラリ(os と sys)

■■■スポンサーリンク■■■