Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Check for float string
Next article icon

Check If Value Is Int or Float in Python

Last Updated : 31 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to check if a value is an integer or float in Python.

To Check If Value Is Int Or Float In Python

Below, we provide examples to illustrate how to check if a value is int or float in Python.

  • Using type() function
  • Using isinstance() function
  • Using isdigit() function

Check If Value Is Int Or Float Using type() Function

In Python, you can simply use the built-in type() function to check the data type of any object or variable. Here is an example where we are using the type() function on an integer and a float value. You can see that the type() function returns the class to which an object belongs:

Python3
# integer value a = 14  # float value b = 2.3  print(type(a)) print(type(b)) 

Output
<class 'int'> <class 'float'>

Check If Value Is Int Or Float Using Isinstance() Function

The isinstance() function takes two arguments, an object and a data type and returns boolean outputs - True and False depending on whether the given object belongs to the given data type or not. Here is how you can check if a number is an int or float in Python:

Python3
# integer number num_1 = 654  # float number num_2 = 566.8  # ininstance function res = isinstance(num_1, int)   print(res)  res = isinstance(num_1, float)   print(res)  res = isinstance(num_2, int)  print(res)  res = isinstance(num_2, float)   print(res) 

Output
True False False True

You can further use the isinstance() function to add more functionality to your code as follows:

Python3
num = 344  # perform multiplication if the number is an integer if isinstance(num, int):     print(num*2)     print(&quot;The number is an integer&quot;)  # perform division if the number is a float else:     print(num/2)     print(&quot;The number is a float&quot;) 

Output
688 The number is an integer

Using the isinstance() function, you can also check if an object is either an int or a float. For doing this, you will have to pass a tuple with both data types to the isinstance() function. What we are doing here is nothing but like using the isinstance() function twice along with the or operator.

Python3
# integer number num = 234  if isinstance(num, (int, float)):  # pass tuple     print(&quot;The number is either an int or a float&quot;) else:     print(&quot;It's might be a string&quot;) 

Output
The number is either an int or a float

Check If A Value Is Int Or Float Using Isdigit() Function

The isdigit() method is used to check if all the characters of a string are digits or not. The function will return False as an output even if one value in the string is not a digit. Here is how we can use this function to check if a given string is an int or float:

Python3
a = '345.5' res = a.isdigit()  if res == True:     print(&quot;The number is an integer&quot;) else:     print(&quot;The number is a float&quot;) 

Output
The number is a float

So far so good, but look at the code given below. Here, we have a string that contains one alphabet. When the code runs, the isdigit() function returns False because one of the values is not a digit. And thus, the control goes to the else block and we can see the output says - 'The number is a float' when actually it's not.

Python3
a = '345f23' res = a.isdigit()  if res == True:     print(&quot;The number is an integer&quot;) else:     print(&quot;The number is a float&quot;) 

Output
The number is a float

Conclusion

In this post, we saw how we can check if a number is an integer or a float in Python. We also saw how we can check if a string is an integer or a float. We can make use of the various functions of Python like isinstance(), int() and isdigit() to work along the way. Note that if you simply want to know the datatype yourself, you can use the type() function but if you want to further add some functionality for the user depending on the data type, then you can use other methods discussed in this post.


Next Article
Python - Check for float string

E

everythingtech
Improve
Article Tags :
  • Python
  • Geeks Premier League
  • Geeks Premier League 2023
Practice Tags :
  • python

Similar Reads

  • Check if the value is infinity or NaN in Python
    In this article, we will check whether the given value is NaN or Infinity. This can be done using the math module. Let's see how to check each value in detail. Check for NaN values in Python NaN Stands for "Not a Number" and it is a numeric datatype used as a proxy for values that are either mathema
    4 min read
  • Check For NaN Values in Python
    In data analysis and machine learning, missing or NaN (Not a Number) values can often lead to inaccurate results or errors. Identifying and handling these NaN values is crucial for data preprocessing. Here are five methods to check for NaN values in Python. What are Nan Values In In Python?In Python
    2 min read
  • Check for True or False in Python
    Python has built-in data types True and False. These boolean values are used to represent truth and false in logical operations, conditional statements, and expressions. In this article, we will see how we can check the value of an expression in Python. Common Ways to Check for True or FalsePython p
    2 min read
  • Check If String is Integer in Python
    In this article, we will explore different possible ways through which we can check if a string is an integer or not. We will explore different methods and see how each method works with a clear understanding. Example: Input2 : "geeksforgeeks"Output2 : geeksforgeeks is not an IntigerExplanation : "g
    4 min read
  • Python - Check for float string
    Checking for float string refers to determining whether a given string can represent a floating-point number. A float string is a string that, when parsed, represents a valid float value, such as "3.14", "-2.0", or "0.001". For example: "3.14" is a float string."abc" is not a float string.Using try-
    2 min read
  • Shell Script To Check For a Valid Floating-Point Value
    User Input can be hectic to manage especially if it's about numbers. Let's say you wanted to perform mathematical calculations in your app or script. It's not hard to say that it will have to deal with floating-point numbers. It's easy in many statically typed programming languages but if it has to
    4 min read
  • Floating point error in Python
    Python, a widely used programming language, excels in numerical computing tasks, yet it is not immune to the challenges posed by floating-point arithmetic. Floating-point numbers in Python are approximations of real numbers, leading to rounding errors, loss of precision, and cancellations that can t
    8 min read
  • Float type and its methods in python
    A float (floating-point number) is a data type used to represent real numbers with a fractional component. It is commonly used to store decimal values and perform mathematical calculations that require precision. Characteristics of Float TypeFloats support decimal and exponential notation.They occup
    5 min read
  • How to convert Float to Int in Python?
    In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded. For example: Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.
    5 min read
  • Python If Else in One Line
    In Python, if-else conditions allow us to control the flow of execution based on certain conditions. While traditional if-else statements are usually written across multiple lines, Python offers a more compact and elegant way to express these conditions on a single line. Python if-else in One LineIn
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences