 Today we will explore about Python Numbers. Previously we learnt about Python package. If you want you can find it here.

Python Numbers

Data types that store numeric values are called Number. If you change the value of a number data type, this results in a newly allocated object. So you can call numbers immutable.

We can simply create a number object by assigning some value to a variable. Also we can manipulate them as shown in below example.

Different Numerical Type

Python supports four different data types.

1. int – signed integers. Can hold both positive and negative value
2. long – long integers. Similar like int. But range of this data type is unlimited
3. float– floating point real values. Holds real numbers.
4. complex– values of the form r+ij. Holds complex numerical values.

Python Numbers – Type Conversion

We can convert data types from one to another. This often is called type casting also. Here are some example.

1. int(x) – converts x to a plain integer.
2. long(x) – converts x to a long integer.
3. float(x) – converts x to a floating point number.
4. complex(x) – converts x to a complex number with real part x and imaginary part zero.
5. complex(x,y) – converts x to a complex number with real part x and imaginary part y.

Here are some example of type casting. Determining type

We can also determine what type of numeric value one variable holds.

The above code, if we run it will produce following output. Python Numbers with Prefix

In our day to day life we deal with decimal numbers(base 10). But in computer programs we may need to deal with numbers of other bases too like binary numbers(base 2), hexadecimal numbers(base 16), octal numbers(base 8) etc. We can represent these numbers with placing a prefix before the number like below-

This code will produce output like below. More on Complex numbers

There are some built-in accessors and functions to support complex number in python. Take a look at the following code for better understanding.

The above code will produce the following output- So that’s all for a quick roundup on python numbers. Make sure you run every piece of code on your own. Also it is better practice to explore things and think out of the box. If you have any doubt feel free to leave a comment.
#happy_coding 🙂