bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Numbers Datatype in Python

In Python, number datatype is used to store numeric values. Numbers in Python are immutable data type. Being an immutable datatype means that if we change the value of an already allocated number datatype then that would result in newly allocated object. In this module we will delve deeper into number datatype. Following is the list of the topics that will be covering in this module, in case you want to jump to a specific one:

Without any further ado, let’s get started.

Learn Python in 16 hrs from experts

Categories of Number Datatype

The number datatype is further categorized based on the type of numeric value that can be stored in it. If a variable contains a numeric value, the data type of that variable will under one of the categories of the number datatype based on the type of value assigned to that variable.

The number datatype is divided into the following five datatypes.

  • integer
  • long integer
  • octal and hexadecimal
  • floating-point
  • complex numbers

Let’s understand each of the category of number datatype separately

Integers

Python integers are nothing but whole numbers, whose range dependent on the hardware on which python is run. Integers can be of different types such as positive, negative, zero, and long.

Example:

I = 123                           #Positive Integer
J = -20                           #Negative Integer
K = 0                              #Zero Integer

Long Integers

L suffix is used for the representation of long integers in python. Long integers are is used to store large numbers without losing precision.

I = 99999999999L

Wish to Learn Python? Click Here

Octal and Hexadecimal

In python, we also have another number datatype called octal and a hexadecimal number.

To represent the octal which has base 8 in python add a preceding 0 (zero), so that Python interpreter can recognize that you want the value to be in base 8 and not in base 10.
Example:

I = 11
#Then in Octal you will write –
I = 011
print (I)

Output:

9

To represent the hexadecimal numbers (base 16) in Python, add a preceding 0x, so that the interpreter can recognize that you want the value to be in base 16 and not in base 10.
Example:

I = 0x11
print (I)

Output:

17

Floating Point numbers

Floating point numbers symbolize the real numbers that are written with a decimal point dividing the integer and fractional parts.
Floating point numbers may also be in scientific notation with E or e, indicating the power of 10
Example:
5.6e2 that means 5.6 * 102.

I = 2.5
J = 7.5e4

Complex Numbers

Complex numbers are of the form of a + bj, where a is real part floating value and b is the imaginary part floating value and j represents the square root of -1.
Example:

2.5 + 2j
Become Python Certified in 16 hrs.
CLICK HERE

Number Type Conversion

Python offers a few built-in functions that let us convert numbers explicitly from one type to another. This process is called coercion. The conversion of one type of number to another becomes essential when performing certain operations that require parameters of same type. For example, programmers may need to perform mathematical operations like addition and subtraction between values of different number types such as integer and float.

We can use the following built in functions to convert one number type into another:

  • int(x), to convert x into an integer value
  • long(x), to convert x into a long integer value
  • float(x), to convert x into a floating-point number
  • complex(x), to convert x into a complex number where the imaginary part stays 0 and x becomes the real part.
  • complex(x,y), to convert x and y to a complex number where x becomes the real part and y becomes the imaginary part.

Example:

a = 3.5
b = 2
c = -3.5
a = int(a)
print (a)
b = float(b)
print (b)
c = int(c)
print (c)

Output:

3
2.0
-3

When converting a float datatype into integer datatype, the value gets converted into an integer value closest to zero.
This brings us to the end of this module, the next module highlights the String Datatype in Python. See you there!

Next, if you have covered all the topics, get your Python Certification by Intellipaat to ensure your career stability. Also you will be provided with our free Python interview questions listed by the experts.

Previous Next

Download Interview Questions asked by top MNCs in 2018?

"0 Responses on Python Numbers"

    100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
    top

    Sales Offer

    Sign Up or Login to view the Free Python Numbers.