0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】Noneオブジェクト

Last updated at Posted at 2025-02-08

概要

PythonのNoneは「値が存在しない」ことを明示する特殊なオブジェクトです。
他のプログラミング言語のnull/nilに相当し、変数の初期化や関数の戻り値として
広く利用されます。

特徴

  • Noneのデータ型はNonType型です。
    python
    a = None
    print(a)
    >>>
    None
    
    print(type(a))
    >>>
    <class 'NoneType'>
    
  • NoneはFalse(偽)として判定されます。
    python
    if None:
        print("True")
    else:
        print("False")
    >>>
    False
    

使用用途

  1. 変数の初期化
    未設定状態を明確に表示する。

    python
    user_data = None # 後でデータが代入される
    
  2. 関数の戻り値
    処理結果がない場合の戻り値として設定する

    python
    def fetch_data(data):
    # データ取得処理
        return data if data > 10 else None
    
    print(fetch_data(0))
    >>>
    None
    
  3. 関数のデフォルト引数
    引数がない場合の判定が容易になる

    python
    def feb(num,L=None):
        if L is None:
            L = []
        L.append(num)
        return L
    
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?