preamble
While reviewing the test questions, I found a complex number problem
concern
The following options incorrectly describe Python's plural types
The imaginary part of an A-complex is denoted by the suffix "J" or "j".
B For a complex number z, you can obtain the real part of the number with
C For a complex number z, you can obtain the real part of the number with
D complex types represent complex numbers in mathematics
Correct Answer: C
First of all, let's clarify what a complex number is: a complex number is mathematically defined as a number consisting of a real part and an imaginary part, such as a + bj .
Where a, b is a real number, j is the "imaginary unit", the square of j is equal to -, b is called the real and imaginary parts of the complex number a + bj, respectively.
Let's define a complex number in Python: real + imag (the unit of the imaginary part can be j or J)
a = 6 + 0.6j
# Output this complex number a
print(a)
# Get the real part
print()
# Get the imaginary part
print()
# Get the conjugate complex of this complex number
print(())
# Let's define a complex number by means of the complex function #
a = complex(1, 2) b = complex(1) c = complex("1") d = complex("1+2j")
# Running results
ADDITIONAL: Python Complex Numbers and Operation Types Question
Ran into this problem while working on a question:
Following the math, we usually think of the real part as 1.23e+4, or 12,300, and the imaginary part as 9.87e+6, or 98,700,000.
But that's not what happens when the program runs:
Why it's not what we thought it would be involves two questions:
1. Problems of the real part of the imaginary part
2. The question of type of outcome
Look at some more examples:
The above example shows that if we use <complex>. <imag> to get the imaginary part, then the computer would add the real and imaginary parts of the complex number and return it as a floating-point number. To get the imaginary part in the sense that we normally understand it, you assign the complex number to a variable, via <variable>. <imag> to get the imaginary part of the "a + bi" pattern.
Getting the real part is relatively easy to understand; anything that is not immediately followed by j is the real part, which is also returned as a floating-point number.
Another issue is the type of operation. There are three types of data in Python: integer, floating point, and complex. When these three types of data are mixed in an operation, the type of the result adopts the type with the "widest range", the plural type has the widest range, and the integer type has the narrowest range.
In the above example, the real and imaginary parts of the complex number will not be of complex type, and will be returned as a floating point number of type next to the complex type.
List some more examples of operations:
Of course, if the types are consistent, the result of the operation is returned with the same type (type consistency also means that the widest type is his own type)
The above is summarized through my experiments, did not look for authoritative references, if there are incorrect places hope to correct.