Python - Number Type
An object of Number data type represents a numeric literal. In computer science, a literal is a notation for representing a fixed value in the source code. For example, in the assignment statement:>>> X=10
Here 10 is a literal, as the numeric value representing 10 is directly stored in memory. However,>>> Y=X*2
Here, even if the expression evaluates to 20, it is not literally included in the source code.
Python identifies three numeric types.
Zero, positive and negative whole numbers without a fractional part and having unlimited precision, e.g. 1234, 0, -456.
A number having 0o or 0O as prefix represents an octal number.
For example: 0O12: equivalent to 10 (ten) in the decimal number system.
A number with 0x or 0X as prefix represents hexadecimal number.
For example: 0x12: equivalent to 18 (Eighteen) in the decimal number system.
Positive and negative real numbers with a fractional part denoted by the decimal symbol or the scientific notation using E or e, e.g. 1234.56, 3.142, -1.55, 0.23.
Scientific notation is used as a short representation to express floats having many digits.
345600000000 is represented as 3.456e11 or 3.456E11
345.56789 is represented as 3.4556789e2 or 3.4556789E2
A complex number is a number with real and imaginary components. For example, 5 + 6j is a complex number where 5 is the real component and 6 multiplied by j is an imaginary component.
Examples: 1+2j, 10-5.5J, 5.55+2.33j, 3.11e-6+4j
|+ (Addition)||Adds operands on either side of the operator.||
|- (Subtraction)||Subtracts the right-hand operand from the left-hand operand.||
|* (Multiplication)||Multiplies values on either side of the operator.||
|/ (Division)||Divides the left-hand operand by the right-hand operand.||
|% (Modulus)||Returns the remainder of the division of the left-hand operand by right-hand operand.||
|** (Exponent)||Calculates the value of the left-operand raised to the right-operand.||
|// (Floor Division)||The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):||
Arithmetic Operations on Complex Numbers
Addition and subtraction of complex numbers is straightforward. Real and imaginary parts are added/subtracted to get the result.>>> a=6+4j
The process of multiplying these two complex numbers is very similar to multiplying two binomials. Multiply each term in the first number by each term in the second number.a=6+4j
Verify this result using the Python interpreter.>>> a=6+4j
To obtain the division of two complex numbers, multiply both sides by the conjugate of the denominator, which is a number with the same real part and the opposite imaginary part.a=6+4j
Verify this using the Python interpreter.>>> a=6+4j
A numeric object of one type can be converted in another type using the following functions:
|int||Returns the integer object from a float or a string containing digits.|
|float||Returns a floating-point number object from a number or string containing digits with decimal point or scientific notation.|
|complex||Returns a complex number with real and imaginary components.|
|hex||Converts a decimal integer into a hexadecimal number with 0x prefix.|
|oct||Converts a decimal integer in an octal representation with 0o prefix.|
|pow||Returns the power of the specified numbers.|
|abs||Returns the absolute value of a number without considering its sign.|
|round||Returns the rounded number.|