Numbers. Representation of numbers of different types. Basic numeric types. Number conversion functions
Contents
- 1. Categories of numeric types
- 2. List of numeric types
- 3. Representation of integers (literals) in different calculus systems. Example
- 4. Types of integers in Python 2.6: regular and long. Example
- 5. Representation of integers in Python 3.0
- 6. Conversion functions of the numbers in the string, and vice versa hex(), oct(), bin(), int(). Examples
- 7. Representation of real numbers. Example
- 8. Complex numbers. Examples
- 9. Creating complex numbers. Function complex(). Example
- 10. The eval() function. Converting a string to number
- Related topics
Search other websites:
1. Categories of numeric types
In Python, number types are represented by the following categories:
- ordinary numeric types (real, integer);
- literals (create numeric types);
- expressions (processing of numeric types).
⇑
2. List of numeric types
The following numeric types are supported in Python:
- integers and real numbers;
- complex numbers;
- fixed precision numbers;
- rational numbers;
- sets
- logical values;
- integers of unlimited precision.
⇑
3. Representation of integers (literals) in different calculus systems. Example
Integers (or integer literals) can be written in different calculus systems:
- decimal format;
- hexadecimal;
- octal;
- binary.
Example. Writing the number (literal) 30 in various formats
30 # decimal format 0x1e # hexadecimal 0X1E # hexadecimal 036 # octal, Python 2.6 0B11110 # binary 0b11110 # binary 0O36 # octal, Python 3.0 0o36 # octal, Python 3.0
If you try to represent an octal value in Python 3.0, as in Python 2.6 (036), the compiler will throw an error.
⇑
4. Types of integers in Python 2.6: regular and long. Example
In Python 2.6, integers can be represented by two types:
- regular (32-bit) numbers;
- long (unlimited precision) numbers. Such numbers end with the character l or L. If the numeric value of an integer literal does not fit into a regular 32-bit number, then this value is automatically cast to a long integer. In this case, the interpreter automatically performs the corresponding conversion.
Example.
12345 # regular 32-bit number 1234567890l # long number in Python versions up to 3.0 7777777777777L # long number in Python versions up 3.0
⇑
5. Representation of integers in Python 3.0
In Python 3.0, both types of numbers (regular and long) were represented as a single (single) type of integers, which is automatically represented as a long integer (supports unlimited precision). Therefore, in this version of Python there is no need to complete literals with l or L. If you specify l or L after the number in Python 3.0, the interpreter will throw an “Invalid token” error.
⇑
6. Conversion functions of the numbers in the string, and vice versa hex(), oct(), bin(), int(). Examples
The following functions are used to turn the numeric form of a literal into a string:
- hex(num) – converts the number num to a string that displays this number in hexadecimal notation;
- oct(num) – converts the number num into a string that represents this number in octal;
- bin(num) – converts the number num to a string that represents this number in binary.
To convert a string to an integer taking into account the calculus system, use the function
int(s, base)
here
- s – a string;
- base – an integer that determines the number of digits in the number system. For example, for calculus 7, the value base = 7.
Examples of conversion functions.
sh = hex(15) # sa = 0xF so = oct(15) # sb = 0o17 sb = bin(15) # sb = 0b1111 i = int('15',10) # i = 15 i = int('15',8) # i = 13 # i = int('19',8) - error, the number 9 cannot be in the 8th calculus i = int('2EF',16) # i = 751 i = int('20',16) # i = 32 i = int('14',5) # i = 9
⇑
7. Representation of real numbers. Example
Real numbers define floating point numbers. Real numbers (literals) can be represented as:
- numbers that contain the separator of the integer and fractional part;
- numbers that contain the character e or E to indicate the order of the number.
The Python interpreter recognizes a real number by the presence of the character ‘.’ (“dot”) or exponent (e or E). After the number is recognized, the corresponding object of a real type is created. Such an object is used in further mathematical expressions as an object of a real type (and not an object of an integer or another type).
Example.
2.85 # ordinary representation of a real number 3. # number 3.0 1.3e-3 # number 0.0013 1.52E6 # 1520000.0 3e2 # 300.0 2.0E-1 # 0.2
⇑
8. Complex numbers. Examples
Python has the ability to work with complex numbers. The general form for representing a complex number is as follows:
real_part + imag_part j
here
- real_part – the real part of the complex number, which is optional (in the case when the number does not contain the real part);
- imag_part – the imaginary part of the complex number, which ends with the character j or J.
The interpreter represents a complex number as two real numbers. When used in mathematical expressions, complex numbers are processed accordingly.
Examples of complex number literals
6-8j 2.5+3.8j 0.001J 2.-3.J
⇑
9. Creating complex numbers. Function complex(). Example
Complex numbers are created by the complex() function, which is a built-in Python function. General form of function:
complex(real, imag)
Example.
a = complex(6, -8) # a = (6-8j) b = complex(0, 2.5) # b = 2.5j c = complex(0.02, -1e4) # c = (0.02-10000j)
⇑
10. The eval() function. Converting a string to number
A number can be represented as a string, for example
'258' "0x2A5"
You can use the eval() function to convert a string to a number. The eval(s) function considers the string s as Python code. The function compiles the string s and uses it. The string is considered as part of the program.
Example.
# Function eval() # execution of a line as program code a = 5 b = 6 c = eval('a+b') # c = a+b = 11 # converting strings represented as a number to the number itself c = eval('0xF5') # c = 245 c = eval('0b111') # c = 7 c = eval('0o135') # c = 93 d = bin(eval('0b101')) # d = 0b101
⇑
Related topics
- Presentation of data in Python. The concept of the object. Identity, type, value of the object. The functions id(), type(). Operators is, is not
- Literals. Creation (generation) objects. Basic types of objects
⇑