返回复数参数实部的Python程序
给定一个复数,任务是编写一个Python程序来返回复数参数的实数部分。
什么是复数
复数是那些以a+ib格式书写的数字,其中’a’和’b’是实数,’i’是称为 “iota “的虚部。(√-1)是i的值。
在Python中,虚数可以通过使用complex()函数来创建,而实数部分则使用.real属性返回。在这篇文章中,我们使用一个NumPy数组来创建虚数,并使用numpy.real()函数从中获取实数部分。
示例 1:
在这个例子中,我们可以使用complex()函数创建一个复数,并在.real属性的帮助下返回一个实数。
# creating an imaginary number.
imaginary_number = complex(1+2j)
# returning the real part of the
# imaginary number
print('Real part of the imaginary number is : '+str(imaginary_number.real))
输出:
Real part of the imaginary number is : 1.0
示例 2:
在这个例子中,我们可以使用NumPy数组创建一个复数,并返回其尺寸、数据类型、形状和实数。
import numpy as np
# Creating an array of imaginary numbers
array = np.array([1.+2.j , 2.+3.j , 4.+5.j])
print(array)
# dimension of the array
print("The dimension of the array is : ",array.ndim)
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
# shape of the array is
print("Shape of the array is : ",array.shape)
# numpy is real() method is used to return the
# real part of the imaginary numbers in the array
print("real part of the imaginary numbers in the array is :",np.real(array))
输出:
[1.+2.j 2.+3.j 4.+5.j]
The
import numpy as np
# Creating an array of imaginary numbers
array = np.array([1.+2.j , 2.+3.j , 4.+5.j])
print(array)
# dimension of the array
print("The dimension of the array is : ",array.ndim)
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
# shape of the array is
print("Shape of the array is : ",array.shape)
# numpy is real() method is used to return the
# real part of the imaginary numbers in the array
print("real part of the imaginary numbers in the array is :",np.real(array))
of the array is : 1
Datatype of our Array is : complex128
Shape of the array is : (3,)
real part of the imaginary numbers in the array is : [1. 2. 4.]