如何在 Python 中检查一个数字是否为整数(而非浮点数)

PythonPythonBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在这个实验中,你将学习如何判断 Python 中的一个数字是整数还是浮点数。实验首先会区分整数和浮点数,强调它们的关键特征并提供示例。

然后,你将探索两种检查数字数据类型的方法:使用 isinstance() 函数搭配 int 类型,以及使用 type() 函数以确保精确性。通过实践练习,你将创建一个 Python 脚本,用于识别和区分整数和浮点数数据类型,从而巩固你对 Python 中数据类型验证的理解。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python/BasicConceptsGroup -.-> python/variables_data_types("Variables and Data Types") python/BasicConceptsGroup -.-> python/numeric_types("Numeric Types") python/BasicConceptsGroup -.-> python/type_conversion("Type Conversion") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/variables_data_types -.-> lab-559545{{"如何在 Python 中检查一个数字是否为整数(而非浮点数)"}} python/numeric_types -.-> lab-559545{{"如何在 Python 中检查一个数字是否为整数(而非浮点数)"}} python/type_conversion -.-> lab-559545{{"如何在 Python 中检查一个数字是否为整数(而非浮点数)"}} python/build_in_functions -.-> lab-559545{{"如何在 Python 中检查一个数字是否为整数(而非浮点数)"}} end

区分整数和浮点数

在这一步中,你将学习区分 Python 中的两种基本数据类型:整数和浮点数。理解它们之间的差异对于进行准确的计算和数据操作至关重要。

整数 (int)

整数是没有小数点的整数,可以是正数或负数。例如 -3、0、5、100。

浮点数 (float)

浮点数,即带有小数点的数字。它们也可以用科学记数法表示。例如 -2.5、0.0、3.14、1.0e5(即 100000.0)。

让我们从创建一个 Python 脚本来探索这些数据类型开始。

  1. 在 LabEx 环境中打开 VS Code 编辑器。

  2. ~/project 目录下创建一个名为 datatypes.py 的新文件。

    touch ~/project/datatypes.py
  3. 在编辑器中打开 datatypes.py 文件,并添加以下 Python 代码:

    ## Assign an integer to the variable 'integer_number'
    integer_number = 10
    
    ## Assign a float to the variable 'float_number'
    float_number = 10.0
    
    ## Print the values and their types
    print("Integer:", integer_number, "Type:", type(integer_number))
    print("Float:", float_number, "Type:", type(float_number))
  4. 保存 datatypes.py 文件。

  5. 在终端中使用 python 命令运行脚本:

    python ~/project/datatypes.py

    你应该会看到以下输出:

    Integer: 10 Type: <class 'int'>
    Float: 10.0 Type: <class 'float'>

    此输出清楚地表明 integer_numberint 类型,而 float_numberfloat 类型。

  6. 现在,让我们执行一个简单的算术运算,看看 Python 如何处理这些类型:

    修改 datatypes.py 文件,添加以下内容:

    ## Assign an integer to the variable 'integer_number'
    integer_number = 10
    
    ## Assign a float to the variable 'float_number'
    float_number = 10.0
    
    ## Print the values and their types
    print("Integer:", integer_number, "Type:", type(integer_number))
    print("Float:", float_number, "Type:", type(float_number))
    
    ## Add an integer and a float
    sum_result = integer_number + float_number
    
    ## Print the result and its type
    print("Sum:", sum_result, "Type:", type(sum_result))
  7. 保存 datatypes.py 文件。

  8. 再次运行脚本:

    python ~/project/datatypes.py

    你应该会看到以下输出:

    Integer: 10 Type: <class 'int'>
    Float: 10.0 Type: <class 'float'>
    Sum: 20.0 Type: <class 'float'>

    注意,当你将一个整数和一个浮点数相加时,结果是一个浮点数。这是因为 Python 会自动将整数转换为浮点数以保持精度。

使用 isinstance() 搭配 int

在这一步中,你将学习如何使用 isinstance() 函数来检查一个变量是否为整数。这个函数是 Python 中验证数据类型的强大工具。

isinstance() 函数接受两个参数:

  • 你要检查的变量。
  • 你要检查的目标数据类型(例如 intfloatstr)。

如果变量是指定的数据类型,该函数返回 True,否则返回 False

让我们修改上一步中的 datatypes.py 文件,加入 isinstance() 函数。

  1. 在 VS Code 编辑器中打开 datatypes.py 文件。

  2. 在文件中添加以下代码:

    ## Assign an integer to the variable 'integer_number'
    integer_number = 10
    
    ## Assign a float to the variable 'float_number'
    float_number = 10.0
    
    ## Print the values and their types
    print("Integer:", integer_number, "Type:", type(integer_number))
    print("Float:", float_number, "Type:", type(float_number))
    
    ## Add an integer and a float
    sum_result = integer_number + float_number
    
    ## Print the result and its type
    print("Sum:", sum_result, "Type:", type(sum_result))
    
    ## Check if integer_number is an integer
    is_integer = isinstance(integer_number, int)
    print("Is integer_number an integer?", is_integer)
    
    ## Check if float_number is an integer
    is_integer = isinstance(float_number, int)
    print("Is float_number an integer?", is_integer)
  3. 保存 datatypes.py 文件。

  4. 在终端中使用 python 命令运行脚本:

    python ~/project/datatypes.py

    你应该会看到以下输出:

    Integer: 10 Type: <class 'int'>
    Float: 10.0 Type: <class 'float'>
    Sum: 20.0 Type: <class 'float'>
    Is integer_number an integer? True
    Is float_number an integer? False

    如你所见,isinstance(integer_number, int) 返回 True,因为 integer_number 确实是整数。而 isinstance(float_number, int) 返回 False,因为 float_number 是浮点数,不是整数。

当你需要根据变量的数据类型执行不同的操作时,这个函数特别有用。例如,如果变量是整数,你可能想执行整数除法;如果是浮点数,则执行浮点除法。

使用 type() 检查精度

在这一步中,你将探索如何使用 type() 函数来了解 Python 中数字的精度。虽然 isinstance() 用于检查变量是否属于特定类型,但 type() 会返回变量的实际类型。这对于理解 Python 如何处理不同的数值运算及其结果的精度很有帮助。

让我们继续修改 datatypes.py 文件来演示这一点。

  1. 在 VS Code 编辑器中打开 datatypes.py 文件。

  2. 在文件中添加以下代码:

    ## Assign an integer to the variable 'integer_number'
    integer_number = 10
    
    ## Assign a float to the variable 'float_number'
    float_number = 10.0
    
    ## Print the values and their types
    print("Integer:", integer_number, "Type:", type(integer_number))
    print("Float:", float_number, "Type:", type(float_number))
    
    ## Add an integer and a float
    sum_result = integer_number + float_number
    
    ## Print the result and its type
    print("Sum:", sum_result, "Type:", type(sum_result))
    
    ## Check if integer_number is an integer
    is_integer = isinstance(integer_number, int)
    print("Is integer_number an integer?", is_integer)
    
    ## Check if float_number is an integer
    is_integer = isinstance(float_number, int)
    print("Is float_number an integer?", is_integer)
    
    ## Perform division with integers
    division_result = integer_number / 3
    print("Division result:", division_result, "Type:", type(division_result))
    
    ## Perform integer division with integers
    integer_division_result = integer_number // 3
    print("Integer division result:", integer_division_result, "Type:", type(integer_division_result))
  3. 保存 datatypes.py 文件。

  4. 在终端中使用 python 命令运行脚本:

    python ~/project/datatypes.py

    你应该会看到以下输出:

    Integer: 10 Type: <class 'int'>
    Float: 10.0 Type: <class 'float'>
    Sum: 20.0 Type: <class 'float'>
    Is integer_number an integer? True
    Is float_number an integer? False
    Division result: 3.3333333333333335 Type: <class 'float'>
    Integer division result: 3 Type: <class 'int'>

    观察以下内容:

    • 当你使用 / 运算符对两个整数进行除法运算时,结果是浮点数,即使结果是整数。这是因为 Python 旨在提供尽可能准确的结果。
    • 当你使用 // 运算符(向下取整除法)时,结果是整数,会舍弃任何小数部分。

理解这些细微差别对于编写准确且高效的 Python 代码至关重要,尤其是在处理数值计算时。

总结

在这个实验中,你学习了如何区分 Python 中的整数(integer)和浮点数(float)。整数是没有小数点的整数,而浮点数是包含小数点的数字。你创建了一个 Python 脚本,将整数值和浮点数值赋给变量,然后使用 type() 函数打印出它们的值及其各自的类型。输出结果表明,被赋予整数的变量类型为 int,而带有小数点的变量类型为 float