3. Numbers

Numbers provide literal or scalar storage and direct access. A number is also an immutable type, meaning that changing or updating its value results in a newly allocated object. This activity is, of course, transparent to both the programmer and the user, so it should not change the way the application is developed.

Python has four basic numeric types: integers, floating point real numbers, complex numbers, and booleans.

A) Integer

Integers can be represented in decimal, octal, hexadecimal or binary formats. Outside of base 10, you will find a leading indicator that determines what base. For octal (base 8), it is a leading "0o" (or "0O"). Hexadecimal (base 16) is represented by "0x" (or "0X"). Binary (base 2) is representation by "0b".

0o101 84 -273 017 0b0110 0xFFFFFFFF -0X92

B) Float

Floats in Python are implemented as double precision floating point real numbers, values that can be represented in straightforward decimal or scientific notations. These 8-byte (64-bit) values conform to the IEEE 754 definition (52M/11E/1S) where 52 bits are allocated to the mantissa, 11 bits to the exponent (this gives you about ± 10308.25 in range), and the final bit to the sign. However, the actual degree of precision you will receive depends completely on the architecture of the machine as well as the implementation of the compiler that built your Python interpreter.

Floating point values are denoted by a decimal point ( . ) in the appropriate place and an optional “e” suffix representing scientific notation. We can use either lowercase ( e ) or uppercase ( E ). Positive (+) or negative ( - ) signs between the “e” and the exponent indicate the sign of the exponent. Absence of such a sign indicates a positive exponent. 

0.0 -777. 1.6 -5.555567119 96e3 * 1.0
4.3e25 9.384e-23 -2.172818 float(12) 1.000000001
3.1416 4.2E-10 -90. 6.022e23 -1.609E-19

D) Complex

A long time ago, mathematicians were absorbed by the following equation:

x2 = - 1

The reason for this is that any real number (positive or negative) multiplied by itself results in a positive number. How can you multiply any number with itself to get a negative number? No such real number exists. So in the eighteenth century, mathematicians invented something called an imaginary number i (or j, depending on what math book you are reading) such that:

j = √ - 1

Basically a new branch of mathematics was created around this special number (or concept), and now imaginary numbers are used in numerical and mathematical applications. Combining a real number with an imaginary number forms a single entity known as a complex number. A complex number is any ordered pair of floating point real numbers (x, y) denoted by x + yj where x is the real part and y is the imaginary part of a complex number. 

It turns out that complex numbers are used a lot in everyday math, engineering, electronics, etc. Because it became clear that many researchers were reinventing this wheel quite often, complex numbers became a real Python data type.

Here are some facts about Python’s support of complex numbers:

  • Imaginary numbers by themselves are not supported in Python (they are paired with a real part of 0.0 to make a complex number)
  • Complex numbers are made up of real and imaginary parts
  • Syntax for a complex number: real+imagj
  • Both real and imaginary components are floating point values
  • Imaginary part is suffixed with letter “J”; lowercase ( j) or uppercase (J)

 

64.375+1j 4.23-8.5j 0.23-8.55j 1.23e-045+6.7e+089j
5.23+1.5j -1.23-875J 0+1j 9.80665-8.3144J -.0224+0j

Complex numbers are one example of data with attributes. The attributes are the real and imaginary components of the complex number object they belong to. Complex numbers also have a method attribute that can be invoked, returning the complex conjugate of the object.

num.real            # Returnes real component of complex number num
num.imag            # Returns imaginary component of complex number num
num.conjugate()     # Returns complex conjugate of num

C) Boolean

Booleans are either true or false. Python has two constants, named "True" and "False", which can be used to assign boolean values directly. Expressions can also evaluate to a boolean value. In certain places (like if statements), Python expects an expression to evaluate to a boolean value. You can use virtually any expression in a boolean context, and Python will try to determine its truth value. Different datatypes have different rules about which values are True or False in a boolean context. 

Here are some of the major concepts surrounding Boolean types:

  • They have a constant value of either True or False.
  • Booleans are subclassed from integers but cannot themselves be further derived.
  • Objects that do not have a __nonzero__() method default to True.
  • Python objects typically have a Boolean False value for any numeric zero or empty set.
  • If used in an arithmetic context, Boolean values True and False will take on their numeric equivalents of 1 and 0, respectively.
  • Standard library and built-in Boolean functions return Booleans.
size = 1
size < 0  # False
size = 0
size < 0  # False
size = -1
size < 0  # True

size is an integer, 0 is an integer, and < is a numerical operator. The result of the expression size < 0 is always a boolean.