Examples of complex attributes and method operations in Python

Source: Internet
Author: User
Tags cplex
This article mainly introduces the Python complex attribute and method operation operation, combined with the instance form Analysis Python complex operation related operation skill, the code comment has the detailed explanation, the need friend can refer to the next

The examples in this article describe Python complex attributes and method operation operations. Share to everyone for your reference, as follows:


#coding =utf8 "' plural is composed of a real number and a combination of imaginary numbers, expressed as: X+yj a negative when a pair of ordered floating point number (x, y), where × is the real part, Y is the imaginary part. The concept of a negative number in the Python language: 1. Imaginary numbers cannot exist alone, they are always together with a real part with a value of 0.0 to form a plural 2, a complex number consists of real and imaginary parts 3, the syntax for imaginary numbers: Real+imagej4, real and imaginary parts are floating point 5, The imaginary part must have the built-in properties of the suffix J or J complex: Complex objects have data attributes, respectively, the real and imaginary parts of the complex number. The complex number also has the Conjugate method, which calls it to return the complex's conjugate complex object.  Plural attributes: Real (the real part of a complex number), Imag (imaginary part of a complex number), conjugate () (the conjugate complex that returns complex numbers) ' Class Complex (object): ' ' Create a static property to record the class version ' ' version=1.0 "' Creates a plural class for manipulating and initializing complex number ' Def __init__ (self,rel=15,img=15j): Self.realpart=rel self.imagpart=img #创建复数 def CREATC Omplex (self): return self.realpart+self.imagpart #获取输入数字部分的虚部 def getimg (self): #把虚部转换成字符串 img=str (Self.imagpar T) #对字符串进行切片操作获取数字部分 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:p Rint com.realpart else:pass #原复数 print "The Religion Complex is:", Cplex #求取共轭复数 print "The conjugate complex is : ", Cplex.Conjugate () if __name__== "__main__": Test () 

Result of Operation:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.