コーディングを学ぶことは、チュートリアルを読むだけではなく、実際に手を動かして練習することです。この投稿では、初心者にぴったりの 20 の簡単な演習を紹介します。それぞれには例と Python の解答が付いているので、すぐに試すことができます。
1. 「Hello, World!」を表示する
この演習の目的: 画面にメッセージを出力する最も簡単なプログラムを書くことです。これにより、コードを実行して目に見える結果を得る方法がわかります。
使用するもの: print() 関数 — 入力はなく、出力のみです。
サンプル出力
Hello, World!
Python の解答
print("Hello, World!")
2. 簡単な計算機
この演習の目的: ユーザーから 2 つの数字を取得し、それらの合計、差、積、商を計算します。これにより、入力処理と基本的な算術を学ぶことができます。
使用するもの: input() で数字を取得し、float() で変換し、算術演算子 (+ - * /) と print() で結果を表示します。
サンプル入力
8
2
サンプル出力
Addition: 10.0
Subtraction: 6.0
Multiplication: 16.0
Division: 4.0
Python の解答
a = float(input("Enter first number: ").strip())
b = float(input("Enter second number: ").strip())
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b if b != 0 else "Infinity")
3. 奇数または偶数チェッカー
この演習の目的: 整数が奇数か偶数かを判断します。
使用するもの: 剰余 %, 条件式
サンプル入力
7 is odd
サンプル出力
7 is odd
Python
n = int(input().strip())
print(f"{n} is {'even' if n % 2 == 0 else 'odd'}")
4. 数当てゲーム
この演習の目的: コンピュータがランダムな数字を選び、ユーザーが正解するまで推測を続ける。高い/低いのヒント付き。
使用するもの: random.randint(), while ループ, if/elif/else.
サンプル実行
Guess (1-10): 5
Too low
Guess (1-10): 8
Correct!
Python の解答
import random
secret = random.randint(1, 10)
while True:
g = int(input("Guess (1-10): ") or 0)
if g < secret:
print("Too low")
elif g > secret:
print("Too high")
else:
print("Correct!")
break
5. 乗法表
この演習の目的: 指定された数字の 1~10 の乗法表を印刷します。
使用するもの: for ループ, f-strings.
サンプル入力
3
サンプル出力
3 x 1 = 3
...
3 x 10 = 30
Python の解答
n = int(input().strip())
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
6. 回文チェッカー
この演習の目的: テキストが前から読んでも後ろから読んでも同じかどうかを確認します(大文字小文字や空白/句読点は無視します)。
使用するもの: 文字列のクリーンアップ, .lower(), スライス [::-1].
サンプル入力
RaceCar
サンプル出力
RaceCar is a palindrome
Python の解答
s = input().strip()
clean = ''.join(ch.lower() for ch in s if ch.isalnum())
print(f"{s} is {'a palindrome' if clean == clean[::-1] else 'not a palindrome'}")
7. フィボナッチ数列
この演習の目的: 最初の n 個のフィボナッチ数を表示します。
使用するもの: 最後の 2 つの値を追跡するための変数, for ループ, リストの結合.
サンプル入力
6
サンプル出力
0 1 1 2 3 5
Python の解答
n = int(input().strip())
a, b = 0, 1
seq = []
for _ in range(n):
seq.append(str(a))
a, b = b, a + b
print(' '.join(seq))
8. 階乗計算機
この演習の目的: n! (1 から n までの積) を計算します。
使用するもの: for ループの乗算 (または再帰).
サンプル入力
5
サンプル出力
120
Python の解答
n = int(input().strip())
f = 1
for i in range(2, n+1):
f *= i
print(f)
9. 単語カウンター
この演習の目的: 文中の単語数をカウントします。
使用するもの: str.split(), len().
サンプル入力
I love programming
サンプル出力
3
Python の解答
s = input().strip()
print(len(s.split()))
10. To-Do リスト (ミニ CLI)
この演習の目的: 簡単なループメニューでタスクを追加、表示、削除します。
使用するもの: リスト, while ループ, if/elif/else.
サンプル実行
1
Finish homework
2
1 Finish homework
Python の解答
tasks = []
while True:
print("1) Add 2) View 3) Remove 4) Exit")
c = input("> ").strip()
if c == "1":
tasks.append(input("Task: ").strip()); print("Added.")
elif c == "2":
if tasks:
for i, t in enumerate(tasks, 1):
print(i, t)
else:
print("No tasks.")
elif c == "3":
i = int(input("Index: ") or 0)
if 1 <= i <= len(tasks):
tasks.pop(i-1); print("Removed.")
else:
print("Invalid index.")
elif c == "4":
break
else:
print("Try 1-4.")
11. 文字列を逆にする
この演習の目的: 入力テキストの逆バージョンを出力します。
使用するもの: スライス [::-1].
サンプル入力
hello
サンプル出力
olleh
Python の解答
print(input().strip()[::-1])
12. 3 つの数字の中で最大のもの
この演習の目的: 3 つの数字を読み込み、最大のものを表示します。
使用するもの: input().split(), map(), max().
サンプル入力
4 9 2
サンプル出力
9
Python の解答
a, b, c = map(float, input().split())
print(max(a, b, c))
13. 温度変換 (C ↔ F)
この演習の目的: セルシウスから華氏、またはその逆をスケールの接頭辞に基づいて変換します。
使用するもの: 条件文, 算術.
サンプル入力
C 25
サンプル出力
77.0
Python の解答
scale, val = input().split()
x = float(val)
print(x * 9/5 + 32 if scale.upper() == 'C' else (x - 32) * 5/9)
14. 単利計算
この演習の目的: P, R, T を使って単利を計算します。
使用するもの: map(float, ...), 算術式 P*R*T/100.
サンプル入力
1000 5 2
サンプル出力
100.0
Python の解答
P, R, T = map(float, input().split())
print(P * R * T / 100)
15. 素数チェッカー
この演習の目的: n が素数かどうかを判断します。
使用するもの: √n までのループ, 除数で早期にブレーク.
サンプル入力
13
サンプル出力
prime
Python の解答
n = int(input().strip())
if n < 2:
print("not prime")
else:
i, ok = 2, True
while i*i <= n:
if n % i == 0:
ok = False; break
i += 1
print("prime" if ok else "not prime")
16. 数字の合計
この演習の目的: 入力されたすべての数字文字を合計します。
使用するもの: 文字列に対するジェネレーター式, sum().
サンプル入力
1234
サンプル出力
10
Python の解答
s = input().strip()
print(sum(int(ch) for ch in s if ch.isdigit()))
17. 母音のカウント
この演習の目的: 入力テキストに含まれる母音の数をカウントします。
使用するもの: .lower(), セットを使ったメンバーシップチェック.
サンプル入力
programming
サンプル出力
3
Python の解答
s = input().lower()
vowels = set('aeiou')
print(sum(ch in vowels for ch in s))
18. ジャンケン
この演習の目的: コンピュータと対戦し、勝ち/負け/引き分けを報告します。
使用するもの: random.choice(), タプルベースの勝利ルール, if/elif.
サンプル実行
rock
Computer chose: scissors
You win!
Python の解答
import random
user = input("rock/paper/scissors: ").strip().lower()
cpu = random.choice(["rock", "paper", "scissors"])
print("Computer chose:", cpu)
if user == cpu:
print("Tie!")
elif (user, cpu) in {("rock","scissors"), ("paper","rock"), ("scissors","paper")}:
print("You win!")
else:
print("You lose!")
19. リストの最小値と最大値
この演習の目的: 1 行で数字を読み込み、最小値と最大値を表示します。
使用するもの: split(), map(float), min(), max().
サンプル入力
5 9 1 7 3
サンプル出力
1 9
Python の解答
nums = list(map(float, input().split()))
print(min(nums), max(nums))
20. 基本的なパスワードバリデーター
この演習の目的: パスワードを検証します: 長さ ≥ 8, 1 つ以上の文字と 1 つ以上の数字を含むこと。
使用するもの: len(), any(), .isalpha(), .isdigit(), 論理演算.
サンプル入力
Hello123
サンプル出力
valid
Python の解答
pw = input().strip()
ok = (len(pw) >= 8 and any(ch.isalpha() for ch in pw) and any(ch.isdigit() for ch in pw))
print("valid" if ok else "invalid")