search
HomeBackend DevelopmentPython TutorialHow to represent complex numbers in python

How to represent complex numbers in python

We call numbers in the form z=a bj (a, b are real numbers) called complex numbers, where a is called the real part, b is called the imaginary part, and j is the Imaginary unit.

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.

The concept of complex numbers in the Python language:

1. Imaginary numbers cannot exist alone. They always form a complex number together with a real number part with a value of 0.0

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

3. The syntax for expressing complex numbers: real imagej

4. The real part and the imaginary part are both floating point numbers

5. The imaginary part There must be a suffix j or J

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

The output result is:

123.0
-12.0

Built-in properties of complex numbers:

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

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 (real part of complex number), imag (imaginary part of complex number), conjugate() (returns the conjugate complex number of complex number)

class 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()

More Python related technical articles, Please visit the Python Tutorial column to learn!

The above is the detailed content of How to represent 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
Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

Python: Is it Truly Interpreted? Debunking the MythsPython: Is it Truly Interpreted? Debunking the MythsMay 12, 2025 am 12:05 AM

Pythonisnotpurelyinterpreted;itusesahybridapproachofbytecodecompilationandruntimeinterpretation.1)Pythoncompilessourcecodeintobytecode,whichisthenexecutedbythePythonVirtualMachine(PVM).2)Thisprocessallowsforrapiddevelopmentbutcanimpactperformance,req

Python concatenate lists with same elementPython concatenate lists with same elementMay 11, 2025 am 12:08 AM

ToconcatenatelistsinPythonwiththesameelements,use:1)the operatortokeepduplicates,2)asettoremoveduplicates,or3)listcomprehensionforcontroloverduplicates,eachmethodhasdifferentperformanceandorderimplications.

Interpreted vs Compiled Languages: Python's PlaceInterpreted vs Compiled Languages: Python's PlaceMay 11, 2025 am 12:07 AM

Pythonisaninterpretedlanguage,offeringeaseofuseandflexibilitybutfacingperformancelimitationsincriticalapplications.1)InterpretedlanguageslikePythonexecuteline-by-line,allowingimmediatefeedbackandrapidprototyping.2)CompiledlanguageslikeC/C transformt

For and While loops: when do you use each in python?For and While loops: when do you use each in python?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.