Numbers in Python

Types of numbers in Python

Python has three built in numeric data types-

  1. Integer (int)
  2. Floating-point numbers (float)
  3. Complex (complex)

Out of these, integers and floating-point numbers are used more commonly than complex number types.

1. Integer (int)-

The int data type can be used only with negative or positive numbers that don't have a decimal point. For example -

integer=245

print(type(integer))

The output-

<class 'int'>

In the above code, the int data type converts the string into integer. There is no limit on how long an integer type can be in Python.

2. Floating-point numbers (float)-

A floating-point number is a number with a decimal point.

For example-

floating=14.87

print(type(floating))

The output-

<class 'float'>

In the above code, the float data type converts the string into a floating-point number.

3. Complex numbers (complex)-

A complex number is a number with two distinct components:A real part and an imaginary part. Python is one of the few coding languages that provides built-in support for complex numbers.

For example-

com=1+2j

print(type(com))

The output-

<class 'complex'>

Python puts the complex number in brackets to eliminate any confusion of it being a arithmetic expression. To find the real and imaginary element from a complex number, we can do the following-

com=1+2j

print(com.real)

print(com.imag)

The output-

1.0
2.0

We can also get the complex conjugate of a number in the following way-

com=1+2j

print(com.conjugate())

The output-

(1-2j)

Other Ways to Define Numbers

Numbers can also be defined in the following ways-

1. Exponential Notation

num=2e4

print(num)

The output

20000.0

Here, the e notation multiplies the value before it with 10x, where x is the value after e.

2. Underscores

num=1_000_000

print(num)

The output-

1000000

Underscores between numbers are ignored by the Python interpreter. So, doing this helps the programmer, if he/she has to manually input the values


That is all for today's post on Python numbers. Hope it was informative and easy to understand. Please contact me for any suggestions by filling the contact form or commenting on any one of my posts or pages.

Check out my Medium page!

Regards,
Aarav Iyer

Aarav Iyer

I am a technology and programming enthusiast, currently a high school student. I also love drawing and am fairly interested in aeronautics and astrophysics. My favourite pastimes are reading books, blogging and skywatching with my telescope.

Post a Comment

Previous Post Next Post