How to determine whether it is an integer in python

coldplay.xixi
Release: 2021-03-11 14:48:00
Original
35582 people have browsed it

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)
Copy after login

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;>
Copy after login

2. Use the isinstance() function to determine

isinstance() Syntax of the method:

isinstance(object, classinfo)
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template