In Python, we can work by changing the data types of objects using type conversion or typecasting. Type conversion in Python is of two types - Implicit, where type conversion is automatically performed by Python, and explicit, where we make use of predefined functions to change data types. In this post, we will learn how we can convert a Complex data type to Int in Python, if at all.
Complex Data Type to Int in Python
Note that a complex number has two parts, real and imaginary. We can make use of the real and imag attributes of Python to access the real and imaginary part of a complex data type as float respectively.
Python3
#complex data type
x = 4 + 6j
#access the real part of the data
print(x.real)
#access the imaginary part of the data
print(x.imag)
Thus, we will first use the real attribute to change the complex number to a float and then further use the int() function to convert the float data type to an integer. Here is the code for the same:
Python3
#complex data type
x = 4 + 6j
#the given complex number
print("The given complex number: ", x)
#access the real part of the data and store it in a new variable
r = x.real
#change the float to int
i = int(r)
#the equivalent integer
print("The equivalent integer: ", i)
OutputThe given complex number: (4+6j)
The equivalent integer: 4
Error While Converting Python Complex to Int
To convert a complex number into an integer, we can use the same method of explicit type conversion. However, there is a problem. Look at the code given below.
Here, we are changing an integer data type to a complex data type using the complex() function which works normally as expected.
Python3
# x contains an integer value
x = 4
# print the integer
print(x)
# print the value of x after type casting it to a complex data type
print(complex(x))
However, if we try the opposite of this, that is, if we use the int() data type to change a complex data type to an integer, then we will run into an error:
Python3
#x contains a complex value
x = 4 + 3j
#print the complex number
print(x)
#print the value of x after type casting it to an int data type
print(int(x))
Output
Traceback (most recent call last):
File "Solution.py", line 8, in <module>
print(int(x))
TypeError: can't convert complex to int
This happens because we cannot convert a larger data type to a smaller data type since it leads to the loss of data. Naturally, changing a complex to integer will result in the loss of the imaginary part of the number, and thus, this kind of type casting is not supported by Python. The same is true for some other data types as well. As an example, we cannot convert a string to an integer in Python using the int() function. Here is the code for the same:
Python
#string data type
x = "GFG"
#type cast string to int
print(int(x))
Output
Traceback (most recent call last):
File "Solution.py", line 5, in <module>
print(int(x))
ValueError: invalid literal for int() with base 10: 'GFG'
Thus, we can say that conversion of a complex to integer is not possible in Python. However, if you do not care about the loss of the imaginary part of a complex number, then you can follow some workarounds to get the desired result.
This is essentially how you can convert a complex number to an Integer in Python. Note that there is loss of data even if you follow this workaround, and hence, you should only use this when absolutely necessary.
Similar Reads
.to_bytes() in Python
In Python, the .to_bytes() method is used to convert an integer into its byte representation. This is useful when weneed to store or transmit data in binary format. Example: Convert the integer 10 into bytes[GFGTABS] Python num = 10 byte_data = num.to_bytes(2, 'big') print(byte_data) [/GFGTA
2 min read
Convert Complex Number to String in Python
We are given complex numbers and we have to convert these complex numbers into strings and return the result in string form. In this article, we will see how we can convert complex numbers to strings in Python. Examples: Input : 3+4j <complex>Output : 3+4j <string> Explanation: We can se
3 min read
Python - Convert Float to digit list
We are given a floating-point number and our task is to convert it into a list of its individual digits, ignoring the decimal point. For example, if the input is 45.67, the output should be [4, 5, 6, 7]. Using string manipulationIn this method, the number is converted to a string and then each chara
4 min read
Python - Convert Binary tuple to Integer
Given Binary Tuple representing binary representation of a number, convert to integer. Input : test_tup = (1, 1, 0) Output : 6 Explanation : 4 + 2 = 6. Input : test_tup = (1, 1, 1) Output : 7 Explanation : 4 + 2 + 1 = 7. Method #1 : Using join() + list comprehension + int() In this, we concatenate t
5 min read
Convert list to Python array - Python
We can use a number of approaches to convert a list into a Python array based on the requirements. One option is to use the array module, which allows us to create arrays with a specified data type. Another option is to use the numpy.array() method, which provides more features for working with arra
2 min read
Python - Binary list to integer
A binary list represents binary digits (0s and 1s) as individual elements of a list. This article will explore various methods to convert a binary list into an integer. Using int() with String ConversionThis is the most efficient method. By joining the binary list into a string and using the built-i
3 min read
Convert Celsius To Fahrenheit - Python
We are given the temperature in Celsius and our task is to convert the temperature value in the Fahrenheit scale and display it. To convert the temperature from Celsius to Fahrenheit or vice versa in Python firstly we will take the input in Celsius or Fahrenheit and then convert that temperature to
2 min read
Python | Decimal to binary list conversion
The conversion of a binary list to a decimal number has been dealt in a previous article. This article aims at presenting certain shorthand to do the opposite, i.e binary to decimal conversion. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension + format() In t
6 min read
Python program to convert int to exponential
Given a number of int type, the task is to write a Python program to convert it to exponential. Examples: Input: 19 Output: 1.900000e+01 Input: 2002 Output: 2.002000e+03 Input: 110102 Output: 1.101020e+05Approach: We will first declare and initialise an integer numberThen we will use format method t
1 min read
Python - Convert Number to List of Integers
We need to split a number into its individual digits and represent them as a list of integers. For instance, the number 12345 can be converted to the list [1, 2, 3, 4, 5]. Let's discuss several methods to achieve this conversion. Using map() and str() Combination of str() and map() is a straightforw
3 min read