-
Python高速化の鍵: cProfileでボトルネックを見つけてコードを改善 Pythonは使いやすさが魅力だが、実行速度が課題となるケースも
-
グローバル変数とは? その定義と基本的な使い方 Pythonプログラミングにおいて、変数のスコープは極めて重要な概念だ。その中でも、関数の内外を
-
前提 Python: 3.12 Pythonにおけるソートの基礎: sort() vs sorted() Pythonでソートを行うには、主にsort()とsorted()の2つの関数が利用できる。
-
前提 Python: 3.12 input()関数の基本的な使い方 input()関数の基本的な使い方は非常にシンプルだ。関数を呼び出すだけで、プログラムの実行が一時
-
前提 Python: 3.12 1. importの基本 まずは、importの基本的な使い方について確認しよう。 Pythonでは、以下のように記述することでモジュールを
-
前提 Python: 3.12 pipとは? Pythonパッケージ管理の強い味方 pipは、Package Installer for Pythonの略称であり、Pythonのパッケージをイン
-
前提 Python: 3.12 Jinja2: 3.1.3 参考 List of Control Structures — Template Designer Documentation — Jinja Documentation (3.1.x) Jinja2のloop変数とは Jinja2のloop変数は、for文などのループ処理内で自動的に生成
-
前提 Python: 3.12 参考 文字列メソッド — 組み込み型 — Python 3.12.3 ドキュメント Pythonの文字列メソッド Pythonを使いこなす上で、文字列処理は避けて通れない
-
前提 Python: 3.12 参考 urllib.request — Extensible library for opening URLs — Python 3.12.3 ドキュメント urllibとは? - Python標準のWeb通信ライブラリ urllibは、URL (Uniform Resource Locator) を扱うため
-
前提 Python: 3.12 基本 from pathlib import Path # カレントディレクトリからの相対パス file_path = Path("my_folder/my_file.txt") # ファイルが存在するか確認 if file_path.exists(): print("ファイルは存在します
-
前提 Python: 3.12 基本 import os.path file_path = os.path.join("data", "myfile.txt") if os.path.exists(file_path): print("ファイルは存在します") else: print("ファイルは存在しません")
-
前提 Python: 3.12 参考 argparse — Parser for command-line options, arguments and sub-commands — Python 3.12.3 ドキュメント Pythonでコマンドライン引数を扱うための標準モジュール argparseは、ユーザーフレン
-
前提 Python: 3.12 基本 with open(file_name, 'w', encoding='utf-8') as f: f.write('Sample') 参考 7.2. ファイルを読み書きする — 7. 入力と出力 — Python 3.12.3 ドキュメント モードの指定 ‘r’ 読み込み用に開く (デフォルト) ‘w’ 書き込み
-
前提 Python: 3.12 参考 re — Regular expression operations — Python 3.12.3 ドキュメント 基本 import re m = re.search('a.c', 'abcdef') m.group(0) よく使う特殊文字 . 任意の一文字にマッチ a.c は “abc”, “a1c” などにマッチ * 直前の文字の0回以上の
-
前提 Python: 3.12 参考 テキストシーケンス型 — str — 組み込み型 — Python 3.12.3 ドキュメント 定義 文字列を定義するには以下の記述方法がある。 シングルクォート' ダブルクォ
-
前提 Python: 3.12 基本 条件式の値が真(True)の間繰り返し実行する。 while 条件式: 処理 else: 処理 i = 0 while i < 5: if i == 2: i += 1 continue if i == 3: break print(i) i += 1 参考 8.2. while 文 —
-
前提 Python: 3.12 参考 range — 組み込み型 — Python 3.12.3 ドキュメント 4.3. range() 関数 — 4. その他の制御フローツール — Python 3.12.3 ドキュメント range関数とは range関数とは指定し
-
前提 Python: 3.12 参考 リスト型 (list) — 組み込み型 — Python 3.12.3 ドキュメント 初期化 l = ['aaa', 'bbb'] 要素を追加する リストの最後に要素を追加する l = ['aaa', 'bbb'] l.append('ccc') print(l) 実行結果 ['aaa', 'bbb', 'ccc'] 指定し
-
前提 Python: 3.12 参考 マッピング型 — dict — 組み込み型 — Python 3.12.3 ドキュメント 初期化 d = {'aaa': 1, 'bbb': 2} キーが存在するか確認する d = {'aaa': 1, 'bbb': 2} if 'aaa' in d.keys(): 処理 値を取得する d =
-
前提 Python: 3.12 基本 for i in range(5): print(i) else: 処理 for i in range(-5, 2): print(i) for i in range(5): if i == 2: continue if i == 3: break print(i) 参考 8.3. for 文 — 8. 複合文 (compound statement) — Python 3.12.3 ドキュメント else else節がある場合、ルー
-