In this course, we will cover the most commonly used Python data types as outlined in the Python Language Reference. The following four general data types are discussed in the course and, unless otherwise stated, will be used in examples: Numbers (Integer, Boolean, Real, Complex), Sequences (Immutable - String, Tuple, Bytes; Mutable - Lists, Byte Arrays), Mappings (Dictionary), and Sets.
On occasion, the description of technical material can vary from document to document at python.org. On this site we defer to the content as described in the Python Language Reference as the authoritative source.
Data Types - Numbers
Numbers - numbers in Python come in the following forms: integral (integers and boolean), real (floating point numbers), and complex.
Integer numbers, specified by the built-in function 'int()', are simply whole numbers like 7, 482, -9993, etc.
Boolean has only two values, True and False. It is considered an integral type since the values True and False are internally analogous to 1 and 0 respectively.
Real numbers, specified by the built-in function 'float()', are numbers including a decimal like 33.89, 1.75364, -5226.88, etc.
Complex numbers have a 'real' and 'imaginary' part. They have practical applications in many physical scientific disciplines such as: biology, chemistry, electrical engineering, and physics. Complex numbers are beyond the scope of this course.
The mathematical operations in the table below can be used with the numbers data type.
The relational operations in the table below can be used with the numbers data type.
|>=||Greater than or equal to|
|<=||Less or equal to|
The logical operations in the table below can be used in simple and complex conditional statements. Logical operators are usually used in combination with relational operators as shown in the code example below.
|and||returns True if two boolean expressions are both True|
|or||returns True if at least one boolean expression is True|
|not||reverses the value of a boolean expression|
The code below demonstrates uses of each of the operators listed above.
Here's the output. As always, ensure you understand the output based on the code example.
In the code example above, on lines 115 and 116 the maxsize() function from the sys module is used to display the maximum variable size supported by Python. That number (9223372036854775807) can also be confirmed on the Windows calculator set to Programmer mode. Note that the value is 263 - 1. The reason it is not 264 is that one bit is reserved for the sign bit. So, Python accommodates a very large positive and a very large negative number as shown in the program output as max and min sizes. 263 = 9223372036854775808 but one number has to be reserved for '0' so it is 263 - 1 = 9223372036854775807. Positive numbers are represented with the sign bit set to '0' .
With the sign bit set to '1', the number will be negative. Notice that when all bits are set, the value is -1 which is the highest value negative integer. Note that when bit 63, the highest bit is set, the result will be a negative number.
As bit are 'taken away' from the right, the negative number grows.
Finally, the lowest value negative number is reached by setting all values to '0' except the sign bit. With the sign bit set to '1', the number will be negative.
In the image below, notice with the calculator set to display hexadecimal numbers that 9223372036854775807 = 7FFFFFFFFFFFFFFF. Each of the four bits is called a nibble. Eight bits = one byte. There are a total of 64 bits used to represent a 'signed' variable on a 64-bit machine running a 64-bit operating system.