初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。
問題679
def contains_value(lst, value):
return value in lst
# 呼び出し例
lst = [3, 5, 7, 9]
print(contains_value(lst, 5)) # 出力: True
問題680
def find_index(lst, value):
return lst.index(value) if value in lst else -1
# 呼び出し例
lst = [10, 20, 30, 40]
print(find_index(lst, 30)) # 出力: 2
問題681
def count_occurrences(lst, value):
return lst.count(value)
# 呼び出し例
lst = [1, 2, 2, 3, 2]
print(count_occurrences(lst, 2)) # 出力: 3



コメント