Python: Variables, Numeric Types and Operators
Python offers four different kinds of numbers with which you can work:integers, long numbers (or longs), floating-point numbers (or floats), and imaginary/complex numbers.
We are familiar with Integer, Long or Floating point Numbers. The new type is Imaginary/Complex number. The imaginary number behaves very much like a float, except that it cannot be mixed with a float. When you see an imaginary number, it will have the letter j trailing it.
Note: Python 2 had separate int and long types for non-floating-point numbers. An int could not be any larger than sys.maxint, which varied by platform. Longs were defined by appending an L to the end of the number, and they could be, well, longer than ints. In Python 3, there is only one integer type, called int, which mostly behaves like the long type in Python 2. Since there are no longer two types, there is no need for special syntax to distinguish them.
To determine the type of a number, you can use a special function that is built into Python, called type. When you use type, Python will tell you what kind of data you’re looking at. Let’s try this with a few examples.
In the above examples, we use type() function to determine the type of 2 different numbers: integer, float and complex.
Format Specifiers:
Int: %d
Float: %f
Exponential (For large numbers): %E
Manipulating the Numeric Datatypes:
We can use most of the Python’s operators when working with Numeric data types. the following table lists the operators and how they behavewith numeric types:

Next Post: Useful functions in Python



leave a comment