📜  用于加减复数的Python程序

📅  最后修改于: 2022-05-13 01:55:47.798000             🧑  作者: Mango

用于加减复数的Python程序

给定两个复数 z1 和 z2。任务是添加和减去给定的复数。
添加复数:在Python中,可以使用 +运算符添加复数。
例子:

Input:  2+3i, 4+5i
Output: Addition is : 6+8i

Input: 2+3i, 1+2i
Output: Addition is : 3+5i

例子 :

Python3
# Python program to add
# two complex numbers
 
 
# function that returns
# a complex number after
# adding
def addComplex( z1, z2):
    return z1 + z2
 
# Driver's code
z1 = complex(2, 3)
z2 = complex(1, 2)
print( "Addition is : ", addComplex(z1, z2))


Python3
# Python program to subtract
# two complex numbers
 
 
# function that returns
# a complex number after
# subtracting
def subComplex( z1, z2):
    return z1-z2
 
# driver program
z1 = complex(2, 3)
z2 = complex(1, 2)
print( "Subtraction is : ", subComplex(z1, z2))


输出:

Addition is :  (3+5j)

复数减法: Python中的复数可以使用 –运算符进行减法。
例子:

Input: 2+3i, 4+5i
Output: Subtraction is : -2-2i

Input: 2+3i, 1+2i
Output: Subtraction is : 1+1i

例子 :

Python3

# Python program to subtract
# two complex numbers
 
 
# function that returns
# a complex number after
# subtracting
def subComplex( z1, z2):
    return z1-z2
 
# driver program
z1 = complex(2, 3)
z2 = complex(1, 2)
print( "Subtraction is : ", subComplex(z1, z2))

输出:

Subtraction is :  (1+1j)