Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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 - 

>>>int("25")
25

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 - 

>>>float("1.25")
25

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

(a)
>>>1e8
100000000.0

In the above code, the type of the number is already defined as a floating point number. The e notation is the short form for Exponential notation and is used to express numbers too big to fit. In this case, 1e8 means 1*10^8. If we take 3e+12,

>>>3e+12
3000000000000.0

In the code, +12 means that 12 is a positive power. We can also use e with a negative power. For example - 

>>>2e-4
0.0002

(b)
Another way to pre define the number type is,

>>>100000.0
100000.0

(c)
A third way to pre define the number type is by grouping the digits of the number into groups of three each, separated by an underscore (_) as it becomes more convenient and easy for us if we are typing the numbers by hand. For example,

>>>1_000_000
1000000

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. An example of complex number is - 

>>>n=1+2j
>>>n
(1+2j)

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 - 

>>>n.real
1.0
>>>n.imag
2.0

We can also get the complex conjugate of a number. For example - 

>>>n.conjugate()
(1-2j)

That is all for today's post on Python numbers. Hope it was informative and easy to understand. Please contact me for any suggestions or fill the contact form.

Visit my Repl on replit.com here. You can find some simple sample programs here.

Regards,

Aarav Iyer

References:

(1) https://realpython.com/python-numbers/#:~:text=Python%20has%20three%20built%2Din,numbers%20in%20a%20later%20section.

(2) https://www.w3schools.com/python/python_numbers.asp

(3) (image)https://www.faceprep.in/python/numbers-in-python/



This post first appeared on Code Streak, please read the originial post: here

Share the post

Numbers in Python

×

Subscribe to Code Streak

Get updates delivered right to your inbox!

Thank you for your subscription

×