BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Numbers

Last Updated: March 14, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

In this guide, we will see how to work with numbers in Python. Python supports integers, floats and complex numbers.

An integer is a number without decimal point for example 5, 6, 10 etc.

A float is a number with decimal point for example 6.7, 6.0, 10.99 etc.

A complex number has a real and imaginary part for example 7+8j, 8+11j etc.

Example: Numbers in Python

# Python program to display numbers of
# different data types

# int
num1 = 10
num2 = 100
print(num1+num2)

# float
a = 10.5
b = 8.9
print(a-b)

# complex numbers
x = 3 + 4j
y = 9 + 8j
print(y-x)

Output:

110
1.5999999999999996
(6+4j)

Python example to find the class(data type) of a number

We can use the type() function to find out the class of a number. An integer number belongs to int class, a float number belongs to float class and a complex number belongs to complex class.

# program to find the class of a number

# int
num = 100
print("type of num: ",type(num))

# float
num2 = 10.99
print("type of num2: ",type(num2))

# complex numbers
num3 = 3 + 4j
print("type of num3: ",type(num3))

Output:
Python numbers data type

The isinstance() function

The isinstance() functions checks whether a number belongs to a particular class and returns true or false based on the result.
For example:
isinstance(num, int) will return true if the number num is an integer number.
isinstance(num, int) will return false if the number num is not an integer number.

Example of isinstance() function

num = 100
# true because num is an integer
print(isinstance(num, int))

# false because num is not a float
print(isinstance(num, float))

# false because num is not a complex number
print(isinstance(num, complex))

Output:

True
False
False
❮ PreviousNext ❯

Top Related Articles:

  1. Python Keywords and Identifiers with examples
  2. Python Recursion
  3. Python Constructors – default and parameterized
  4. Python Function Arguments – Default, Keyword and Arbitrary
  5. Python all() Function with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Anu says

    May 12, 2021 at 6:18 AM

    Hi Chaitanya, good job. I find your tutorial useful, crisp, to the point. Thank you!

    Reply

Leave a Reply Cancel reply

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

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap