Numeric types

Number types include integers (int), that is, whole numbers of unlimited range, floating-point numbers (float), complex numbers (complex), which are represented by two float numbers, and Boolean (bool) in Python. Python provides the int data type that allows standard arithmetic operators (+, -, * and / ) to work on them, similar to other programming languages. A Boolean data type has two possible values, True and False. These values are mapped to 1 and 0, respectively. Let's consider an example:

>>> a=4; b=5   # Operator (=) assigns the value to variable
>>>print(a, "is of type", type(a))
4 is of type
<class 'int'>
>>> 9/5
1.8
>>>c= b/a # division returns a floating point number
>>> print(c, "is of type", type(c))
1.25 is of type <class 'float'>
>>> c # No need to explicitly declare the datatype
1.25

The a and b variables are of the int type and c is a floating-point type. The division operator (/) always returns a float type; however, if you wish to get the int type after division, you can use the floor division operator (//), which discards any fractional part and will return the largest integer value that is less than or equal to x. Consider the following example:

>>> a=4; b=5   
>>>d= b//a
>>> print(d, "is of type", type(d))
1 is of type <class 'int'>
>>>7/5 # true division
1.4
>>> -7//5 # floor division operator
-2

It is advised that readers use the division operator carefully, as its function differs according to the Python version. In Python 2, the division operator returns only integer, not float.

The exponent operator (**) can be used to get the power of a number (for example, x ** y), and the modulus operator (%) returns the remainder of the division (for example, a% b returns the remainder of a/b):

>>> a=7; b=5   
>>> e= b**a # The operator (**)calculates power
>>>e
78125
>>>a%b
2

Complex numbers are represented by two floating-point numbers. They are assigned using the j operator to signify the imaginary part of the complex number. We can access the real and imaginary parts with f.real and f.imag, respectively, as shown in the following code snippet. Complex numbers are generally used for scientific computations. Python supports addition, subtraction, multiplication, power, conjugates, and so forth on complex numbers, as shown in the following:

>>> f=3+5j
>>>print(f, "is of type", type(f))
(3+5j) is of type <class 'complex'>
>>> f.real
3.0
>>> f.imag
5.0
>>> f*2 # multiplication
(6+10j)
>>> f+3 # addition
(6+5j)
>>> f -1 # subtraction
(2+5j)

In Python, Boolean types are represented using  truth values, that is, True and False; it's similar to 0 and 1. There is a bool class in Python, which returns True or False. Boolean values can be combined with logical operators such as and, or, and not:

>>>bool(2)
True
>>>bool(-2)
True
>>>bool(0)
False

A Boolean operation returns either True or False. Boolean operations are ordered in priority, so if more than one Boolean operation occurs in an expression, the operation with the highest priority will occur first. The following table outlines the three Boolean operators in descending order of priority:

Operator

Example

not x

It returns False if x is True, and returns True if x is False.

x and y

It returns True if x and y are both True; otherwise, it returns False.

x or y

It returns True if either x or y is True; otherwise, it returns False.

 

Python is very efficient when evaluating Boolean expressions as it will only evaluate an operator if it needs to. For example, if x is True in an expression x or y, then there is no need to evaluate y since the expression is True anyway—that is why in Python the y is not evaluated. Similarly, in an expression x and y, if x is False, the interpreter will simply evaluate x and return False, without evaluating y.

The comparison operators (<, <=, >, >=, ==, and !=) work with numbers, lists, and other collection objects and return True if the condition holds. For collection objects, comparison operators compare the number of elements and the equivalence operator ( ==) returns True if each collection object is structurally equivalent, and the value of each element is identical. Let's see an example:

>>>See_boolean = (4 * 3 > 10) and (6 + 5 >= 11)
>>>print(See_boolean)
True
>>>if (See_boolean):
... print("Boolean expression returned True")
else:
... print("Boolean expression returned False")
...

Boolean expression returned True
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset