• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Check if Variable is Float in Python

Check if Variable is Float in Python

July 27, 2022 Leave a Comment

To check if a variable is a float, you can use the type() function and check if the variable is of type float.

t = 1.01
a = 123
l = [0, 1, 2]

print(type(t) == float)
print(type(a) == float)
print(type(l) == float)

#Output:
True
False
False

You can also use the isinstance() function to check if a variable is a float.

t = 1.01
a = 123
l = [0, 1, 2]

print(isinstance(t,float))
print(isinstance(a,float))
print(isinstance(l,float))

#Output:
True
False
False

When working with different types of variables in Python, the ability to check the type of the variables easily is valuable.

One such case is if you want to check if a variable is a float in your Python code.

To check if a variable is of type float, you can use the type() function.

type() returns the class type of the argument passed.

If type() returns float, then we can conclude the variable is a float.

Below are some examples showing you how to check if a variable is a float in Python.

t = 1.01
a = 123
l = [0, 1, 2]

print(type(t) == float)
print(type(a) == float)
print(type(l) == float)

#Output:
True
False
False

Using isinstance() to Check if Variable is float in Python

One other way you can check if a variable is of type float is with the isinstance() function.

isinstance() checks to see if a variable is an instance of the class passed.

Below is an example showing you how to use isinstance() in Python to check if a variable is float.

t = 1.01
a = 123
l = [0, 1, 2]

print(isinstance(t,float))
print(isinstance(a,float))
print(isinstance(l,float))

#Output:
True
False
False

Hopefully this article has been useful for you to check if a variable is a float in Python.

Other Articles You'll Also Like:

  • 1.  How to Check if Tuple is Empty in Python
  • 2.  Using Python to Read Random Line from File
  • 3.  Python acos – Find Arccosine and Inverse Cosine of Number
  • 4.  Get First Digit in Number Using Python
  • 5.  Find Median of List in Python
  • 6.  How to Group By Columns and Find Mean in pandas DataFrame
  • 7.  How to Group By Columns and Find Variance in pandas
  • 8.  Python gethostbyname() Function – Get IPv4 Address from Name
  • 9.  Using Python to Remove Non-Empty Directory
  • 10.  How to Draw a Triangle in Python Using turtle Module

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy