• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

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
You are here: Home / Python / Check if Variable is Integer in Python

Check if Variable is Integer in Python

July 27, 2022 Leave a Comment

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

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

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

#Output:
True
False
False

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

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

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

#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 an integer in your Python code.

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

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

If type() returns int, then we can conclude the variable is an integer.

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

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

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

#Output:
True
False
False

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

One other way you can check if a variable is of type intis 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 an integer.

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

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

#Output:
True
False
False

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

Other Articles You'll Also Like:

  • 1.  Python Check if Object Has Attribute
  • 2.  Perfect Numbers in Python
  • 3.  Python os.sep – Create Operating System Path Seperator Character
  • 4.  Are Dictionaries Mutable in Python? Yes, Dictionaries are Mutable
  • 5.  Print Object Attributes in Python using dir() Function
  • 6.  How to Remove Vowels from a String in Python
  • 7.  Add Minutes to Datetime Variable Using Python timedelta() Function
  • 8.  Random Number Without Repeating in Python
  • 9.  Count Number of Files in Directory with Python
  • 10.  How to Write CSV File to AWS S3 Bucket Using Python

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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