Python Fundamentals について
Python初学者向けの基礎学習スキルです。構文、データ構造、オブジェクト指向、標準ライブラリを扱い、実践課題で知識を応用できます。
主な機能
- 01Pythonの構文とデータ型
- 02条件分岐、ループ、関数
- 03オブジェクト指向の基礎
- 04標準ライブラリとファイル操作
- 05仮想環境とpipの管理
活用シーン
- 01PEP 8に沿ったコードの練習
- 02pathlibを使ったファイル整理ツールの作成
- 03JSONを使った連絡先管理の作成
- 04テキスト分析とCSV統計の出力
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-python --skill python-fundamentalsインストール後、Claude Code で/python-fundamentalsを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
assets/config.yamlreferences/GUIDE.mdscripts/helper.py詳細
Python の基礎
概要
このスキルでは、構文、データ型、制御構造、関数、オブジェクト指向プログラミング、標準ライブラリなど、Python プログラミングの基礎要素を扱います。
学習目標
- PEP 8 のガイドラインに従って、読みやすく Python らしいコードを書く
- Python のすべてのデータ構造(リスト、タプル、辞書、セット)を習得する
- オブジェクト指向プログラミングの概念を理解し、実装する
- Python の標準ライブラリを効果的に使いこなす
- pip/venv で仮想環境とパッケージを管理する
主なトピック
1. Python の構文とデータ型
- 変数と命名規則
- 数値型(int、float、complex)
- 文字列と文字列メソッド
- ブール論理と None
- 型ヒント(PEP 484)
- F 文字列とフォーマット
コード例:
# Type hints and f-strings
def greet_user(name: str, age: int) -> str:
return f"Hello {name}, you are {age} years old!"
# Using the function
message = greet_user("Alice", 30)
print(message) # Hello Alice, you are 30 years old!
2. 制御フローと関数
- 条件文(if/elif/else)
- ループ(for、while)
- リスト/辞書/セットの内包表記
- 関数のパラメーターと引数
- ラムダ関数
- デコレーターの基礎
コード例:
# List comprehension with conditional
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_squares = [x**2 for x in numbers if x % 2 == 0]
print(even_squares) # [4, 16, 36, 64, 100]
# Decorator example
def timing_decorator(func):
import time
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"{func.__name__} took {end - start:.4f} seconds")
return result
return wrapper
@timing_decorator
def slow_function():
import time
time.sleep(1)
return "Done!"
3. オブジェクト指向プログラミング
- クラスとインスタンス
- 継承とコンポジション
- ポリモーフィズムとダックタイピング
- カプセル化(パブリック/プライベート)
- 特殊メソッド(init、str、repr など)
- 抽象基底クラス
コード例:
from abc import ABC, abstractmethod
class Vehicle(ABC):
def __init__(self, brand: str, model: str):
self.brand = brand
self.model = model
@abstractmethod
def start_engine(self) -> str:
pass
def __str__(self) -> str:
return f"{self.brand} {self.model}"
class Car(Vehicle):
def __init__(self, brand: str, model: str, doors: int):
super().__init__(brand, model)
self.doors = doors
def start_engine(self) -> str:
return f"{self} engine started with {self.doors} doors"
# Usage
car = Car("Toyota", "Camry", 4)
print(car.start_engine()) # Toyota Camry engine started with 4 doors
4. 標準ライブラリ
- ファイル操作(open、read、write)
- pathlib によるパス処理
- 日付/時刻操作のための datetime
- collections(Counter、defaultdict、namedtuple)
- itertools と functools
- json と csv モジュール
コード例:
from pathlib import Path
from datetime import datetime, timedelta
from collections import Counter
import json
# Path operations
data_dir = Path("data")
data_dir.mkdir(exist_ok=True)
config_file = data_dir / "config.json"
config = {"app_name": "MyApp", "version": "1.0.0"}
# Write JSON
with open(config_file, "w") as f:
json.dump(config, f, indent=2)
# Read JSON
with open(config_file, "r") as f:
loaded_config = json.load(f)
# Date operations
today = datetime.now()
next_week = today + timedelta(days=7)
formatted = today.strftime("%Y-%m-%d %H:%M:%S")
# Counter for frequency analysis
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
word_count = Counter(words)
print(word_count.most_common(2)) # [('apple', 3), ('banana', 2)]
実践演習
プロジェクト 1:ファイル管理ツール
拡張子ごとにファイルを整理するコマンドラインツールを作成します。
要件:
- 入力としてディレクトリパスを受け取る
- ファイルを再帰的に検索する
- 拡張子ごとにファイルをグループ化する
- ファイルを整理したフォルダーに移動する
- サマリーレポートを生成する
主なスキル: pathlib、ファイル I/O、os モジュール
プロジェクト 2:連絡先管理システム(OOP)
オブジェクト指向の原則を使用して、連絡先管理システムを作成します。
要件:
- 名前、メールアドレス、電話番号を持つ Contact クラス
- 連絡先を管理する ContactBook クラス
- CRUD 操作(作成、読み取り、更新、削除)
- 検索とフィルター機能
- データを JSON ファイルに保存する
主なスキル: OOP、JSON シリアライズ、データ構造
プロジェクト 3:テキストアナライザー
テキストファイルの統計とパターンを分析します。
要件:
- 単語の出現頻度分析
- 文字数のカウント(スペースあり/なし)
- 単語の平均長
- 最もよく使われる単語(ストップワードを除外)
- 結果を CSV にエクスポートする
主なスキル: 文字列操作、collections.Counter、CSV
評価基準
- PEP 8 に準拠した Python コードを書く
- 状況に応じた適切なデータ構造を使用する
- 適切な OOP の原則に従ってクラスを実装する
- 標準ライブラリのモジュールを効果的に活用する
- 仮想環境と依存関係を管理する
- 例外を適切に処理する
- 明確な docstring とコメントを書く
リソース
公式ドキュメント
- Python.org - Python の公式ドキュメント
- PEP 8 - Python のスタイルガイド
- Python Tutorial - 公式チュートリアル
学習プラットフォーム
- Real Python - 総合的なチュートリアル
- Python for Everybody - 無料コース
- Automate the Boring Stuff - 実践的な Python
ツール
次のステップ
Python の基礎を習得したら、次のトピックに進みましょう。
- Django Framework - Web アプリケーションを構築する
- Pandas Data Analysis - データを扱う
- Pytest Testing - 包括的なテストを書く
- Asyncio Programming - 非同期 Python
コメント
Markdown 対応