Home  >  Article  >  Backend Development  >  How to determine whether it is an integer in python

How to determine whether it is an integer in python

coldplay.xixi
coldplay.xixiOriginal
2021-03-11 14:48:0035592browse

Python method to determine whether it is an integer: 1. Use the [type()] function to determine, the code is [type(name, bases, dict)]; 2. Use the [isinstance()] function to determine, the code It is [isinstance(object,classinfo)].

How to determine whether it is an integer in python

The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer.

Python method to determine whether it is an integer:

1. Use the type() function to determine

type ()Function syntax:

type(object)
type(name, bases, dict)

type() function returns the type of the object if you only have the first parameter, and the three parameters return the new type object.

Example:

>>> type(1)
<type &#39;int&#39;>

2. Use the isinstance() function to determine

isinstance() Syntax of the method:

isinstance(object, classinfo)

If the type of the object is the same as the type of parameter two (classinfo), it returns True, otherwise it returns False.

Example:

>>>a = 2
>>> isinstance (a,int)
True

Related free learning recommendations: python video tutorial

The above is the detailed content of How to determine whether it is an integer in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn