工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> Python

Python の型変換と型キャスト

Python の型変換と型キャスト

この記事では、型変換と型変換の使用について学習します。

Python で型変換を学習する前に、 Python データ型についての知識が必要です。


型変換

あるデータ型 (integer、string、float など) の値を別のデータ型に変換するプロセスは、型変換と呼ばれます。 Python には 2 種類の型変換があります。

<オール>
  • 暗黙の型変換
  • 明示的な型変換

  • 暗黙の型変換

    暗黙的な型変換では、Python はあるデータ型を別のデータ型に自動的に変換します。このプロセスはユーザーの関与を必要としません。

    データの損失を避けるために、Python が下位のデータ型 (integer) から上位のデータ型 (float) への変換を促進する例を見てみましょう。

    例 1:整数を浮動小数に変換する

    num_int = 123
    num_flo = 1.23
    
    num_new = num_int + num_flo
    
    print("datatype of num_int:",type(num_int))
    print("datatype of num_flo:",type(num_flo))
    
    print("Value of num_new:",num_new)
    print("datatype of num_new:",type(num_new))

    上記のプログラムを実行すると、出力は次のようになります:

    datatype of num_int: <class 'int'>
    datatype of num_flo: <class 'float'>
    
    Value of num_new: 124.23
    datatype of num_new: <class 'float'>

    上記のプログラムでは、


    それでは、文字列と整数を追加して、Python がそれをどのように処理するかを見てみましょう。

    例 2:文字列 (上位) データ型と整数 (下位) データ型の追加

    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str:",type(num_str))
    
    print(num_int+num_str)

    上記のプログラムを実行すると、出力は次のようになります:

    Data type of num_int: <class 'int'> 
    Data type of num_str: <class 'str'> 
    
    Traceback (most recent call last): 
      File "python", line 7, in <module> 
    TypeError: unsupported operand type(s) for +: 'int' and 'str'

    上記のプログラムでは、


    明示的な型変換

    明示的な型変換では、ユーザーはオブジェクトのデータ型を必要なデータ型に変換します。 int() のような事前定義された関数を使用します 、 float()str() などを使用して、明示的な型変換を実行します。

    このタイプの変換は、ユーザーがオブジェクトのデータ型をキャスト (変更) するため、型キャストとも呼ばれます。

    構文 :

    <required_datatype>(expression)

    型キャストは、必要なデータ型関数を式に割り当てることで実行できます。


    例 3:明示的な変換を使用した文字列と整数の加算

    num_int = 123
    num_str = "456"
    
    print("Data type of num_int:",type(num_int))
    print("Data type of num_str before Type Casting:",type(num_str))
    
    num_str = int(num_str)
    print("Data type of num_str after Type Casting:",type(num_str))
    
    num_sum = num_int + num_str
    
    print("Sum of num_int and num_str:",num_sum)
    print("Data type of the sum:",type(num_sum))

    上記のプログラムを実行すると、出力は次のようになります:

    Data type of num_int: <class 'int'>
    Data type of num_str before Type Casting: <class 'str'>
    
    Data type of num_str after Type Casting: <class 'int'>
    
    Sum of num_int and num_str: 579
    Data type of the sum: <class 'int'>

    上記のプログラムでは、


    覚えておくべき重要なポイント

    <オール>
  • 型変換とは、あるデータ型から別のデータ型へのオブジェクトの変換です。
  • 暗黙的な型変換は、Python インタープリターによって自動的に実行されます。
  • Python は、暗黙的な型変換でのデータの損失を回避します。
  • 明示的な型変換は型キャストとも呼ばれ、ユーザーが定義済みの関数を使用してオブジェクトのデータ型を変換します。
  • 型キャストでは、オブジェクトを特定のデータ型に強制するため、データが失われる可能性があります。

  • Python

    1. C# 型変換
    2. Python のキーワードと識別子
    3. Python ステートメント、インデント、およびコメント
    4. Python 変数、定数、およびリテラル
    5. Python データ型
    6. Python の入力、出力、およびインポート
    7. Python グローバル変数、ローカル変数、および非ローカル変数
    8. Python の数値、型変換、および数学
    9. Python ディレクトリおよびファイル管理
    10. Python エラーと組み込み例外
    11. Java の型キャスト