Home  >  Article  >  Backend Development  >  How to express the imaginary part of complex numbers in python

How to express the imaginary part of complex numbers in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-25 09:43:3915110browse

A complex number is composed of a real number and an imaginary number, expressed as: x yj

A complex number is a pair of ordered floating point numbers (x, y), where x is the real part and y is the imaginary part .

How to express the imaginary part of complex numbers in python

Concepts about complex numbers in Python language:

1. Imaginary numbers cannot exist alone. They always add up to a value of 0.0 The real parts together form a complex number

2. A complex number consists of a real part and an imaginary part

3. The syntax for expressing an imaginary number: real imagej

4. Real part and imaginary number Parts are all floating point numbers

5. The imaginary part must have the suffix j or J

#coding=utf8
aa=123-12j
print aa.real  # output 实数部分 123.0  
print aa.imag  # output虚数部分 -12.0

The output result is:

123.0
-12.0

Related recommendations: "Python Video Tutorial

Built-in properties of complex numbers:

The complex number object has data properties, which are the real part and the imaginary part of the complex number.

Complex numbers also have the conjugate method, which can be called to return the conjugate complex object of the complex number.

Complex number attributes: real (the real part of the complex number), imag (the imaginary part of the complex number), conjugate() (returns the conjugate complex number of the complex number)

#coding=utf8class Complex(object):
    '''创建一个静态属性用来记录类版本号'''
    version=1.0
    '''创建个复数类,用于操作和初始化复数'''
    def __init__(self,rel=15,img=15j):
        self.realPart=rel        self.imagPart=img       
    #创建复数
    def creatComplex(self):
        return self.realPart+self.imagPart    #获取输入数字部分的虚部
    def getImg(self):
        #把虚部转换成字符串
        img=str(self.imagPart)
        #对字符串进行切片操作获取数字部分
        img=img[:-1] 
        return float(img)  
                       def test():
    print "run test..........."
    com=Complex()
    Cplex= com.creatComplex()
    if Cplex.imag==com.getImg():
        print com.getImg()
    else:
        pass
    if Cplex.real==com.realPart:
        print com.realPart    else:
        pass
    #原复数
    print "the religion complex is :",Cplex
    #求取共轭复数
    print "the conjugate complex is :",Cplex.conjugate()
    if __name__=="__main__":
    test()

The above is the detailed content of How to express the imaginary part of complex numbers in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn