Python
Python Numbers
Python supports integers, floating point numbers and complex numbers. They are defined as int, float and complex class in Python. There’s no type declaration to distinguish them; Python tells them apart by the presence or absence of a decimal point.

Integers and floating points are separated by the presence or absence of a decimal point. 5 is integer whereas 6.3 is a floating point number.

Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.

We can use the type() function to know which class a variable or a value belongs to and isinstance() function to check if it belongs to a particular class.
>>> a = 5
>>> # Output: <class 'int'>
>>> print(type(a))
<class 'int'>
>>> # Output: <class 'float'>
>>> print(type(6.3))
<class 'float'>
>>> # Output: (9+7j)
>>> c = 5 + 7j
>>> print(c + 7)
(12+7j)
>>> # Output: True
>>> print(isinstance(c, complex))
True
>>> 

Python supports the following operators on numbers.

+ addition

- subtraction

* multiplication

/ division

** exponent

% remainder

Let’s try them on integers.
>>> 10+15
25
>>> 10-5
5
>>> 10*7
70
>>> 10/3
3.3333333333333335
>>> 10**2
100
>>> 10%2
0
>>> 

The operators have precedence, a kind of priority that determines which operator is applied first. Among the numerical operators, the precedence of operators is as follows, from low precedence to high.

  • +, -
  • *, /, %
  • **
  • When we compute 2 + 3 * 4, 3 * 4 is computed first as the precedence of * is higher than + and then the result is added to 2.

>>> 2 + 3 * 4
14
We can use parenthesis to specify the explicit groups.
>>> (2 + 3) * 4
20

We see you’re using an ad blocker.

Our website is made possible by displaying ads to our visitors.

Please help us continue to provide you with free. So please disabling your ad blocker.