How to Check If a Number Is an Integer (Not Float) in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a number in Python is an integer or a float. The lab begins by differentiating between integers and floats, highlighting their key characteristics and providing examples.

You'll then explore two methods for checking the data type of a number: using the isinstance() function with int and using the type() function for precision. Through hands-on exercises, you'll create a Python script to identify and distinguish between integer and float data types, solidifying your understanding of data type verification in Python.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) 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{{"How to Check If a Number Is an Integer (Not Float) in Python"}} python/numeric_types -.-> lab-559545{{"How to Check If a Number Is an Integer (Not Float) in Python"}} python/type_conversion -.-> lab-559545{{"How to Check If a Number Is an Integer (Not Float) in Python"}} python/build_in_functions -.-> lab-559545{{"How to Check If a Number Is an Integer (Not Float) in Python"}} end

Differentiate Integers and Floats

In this step, you will learn to differentiate between two fundamental data types in Python: integers and floats. Understanding the difference is crucial for performing accurate calculations and data manipulation.

Integers (int)

Integers are whole numbers, positive or negative, without any decimal points. Examples include -3, 0, 5, 100.

Floats (float)

Floats, or floating-point numbers, are numbers that contain a decimal point. They can also represent numbers in scientific notation. Examples include -2.5, 0.0, 3.14, 1.0e5 (which is 100000.0).

Let's start by creating a Python script to explore these data types.

  1. Open the VS Code editor in the LabEx environment.

  2. Create a new file named datatypes.py in the ~/project directory.

    touch ~/project/datatypes.py
  3. Open the datatypes.py file in the editor and add the following Python code:

    ## 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. Save the datatypes.py file.

  5. Run the script using the python command in the terminal:

    python ~/project/datatypes.py

    You should see the following output:

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

    This output clearly shows that integer_number is of type int and float_number is of type float.

  6. Now, let's perform a simple arithmetic operation to see how Python handles these types:

    Modify the datatypes.py file to include the following:

    ## 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. Save the datatypes.py file.

  8. Run the script again:

    python ~/project/datatypes.py

    You should see the following output:

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

    Notice that when you add an integer and a float, the result is a float. This is because Python automatically converts the integer to a float to maintain precision.

Use isinstance() with int

In this step, you will learn how to use the isinstance() function to check if a variable is an integer. This function is a powerful tool for verifying data types in Python.

The isinstance() function takes two arguments:

  • The variable you want to check.
  • The data type you want to check against (e.g., int, float, str).

It returns True if the variable is of the specified type, and False otherwise.

Let's modify the datatypes.py file from the previous step to include the isinstance() function.

  1. Open the datatypes.py file in the VS Code editor.

  2. Add the following code to the file:

    ## 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. Save the datatypes.py file.

  4. Run the script using the python command in the terminal:

    python ~/project/datatypes.py

    You should see the following output:

    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

    As you can see, isinstance(integer_number, int) returns True because integer_number is indeed an integer. isinstance(float_number, int) returns False because float_number is a float, not an integer.

This function is particularly useful when you need to perform different actions based on the data type of a variable. For example, you might want to perform integer division if a variable is an integer, and floating-point division if it's a float.

Check with type() for Precision

In this step, you will explore how the type() function can be used to understand the precision of numbers in Python. While isinstance() checks if a variable belongs to a specific type, type() returns the actual type of the variable. This is useful for understanding how Python handles different numerical operations and their resulting precision.

Let's continue modifying the datatypes.py file to demonstrate this.

  1. Open the datatypes.py file in the VS Code editor.

  2. Add the following code to the file:

    ## 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. Save the datatypes.py file.

  4. Run the script using the python command in the terminal:

    python ~/project/datatypes.py

    You should see the following output:

    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'>

    Observe the following:

    • When you divide two integers using the / operator, the result is a float, even if the result is a whole number. This is because Python aims to provide the most accurate result possible.
    • When you use the // operator (floor division), the result is an integer, discarding any fractional part.

Understanding these nuances is crucial for writing accurate and efficient Python code, especially when dealing with numerical computations.

Summary

In this lab, you learned to differentiate between integers and floats in Python. Integers are whole numbers without decimal points, while floats are numbers containing a decimal point. You created a Python script to assign integer and float values to variables and then printed their values along with their respective types using the type() function. The output demonstrated that variables assigned whole numbers are of type int, and those with decimal points are of type float.