Repository navigation
Expand file tree
/
Copy path016-Complex-to-Integer-Type-Casting.py
More file actions
76 lines (61 loc) · 3.31 KB
/
Copy path016-Complex-to-Integer-Type-Casting.py
File metadata and controls
76 lines (61 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
===============================================================================
Complex to Integer Type Casting
===============================================================================
Program Description:
--------------------
This program demonstrates Complex to Integer Type Casting in Python.
A complex number cannot be converted directly into an integer using the
int() function.
Attempting this conversion raises a TypeError because Python does not know
how to discard the imaginary part automatically.
Example:
complex(10, 5) ---> TypeError
Author : Shaik Mahaboob Basha
Repository : 13-Python
File Name : 16-Complex-to-Integer-Type-Casting.py
===============================================================================
"""
# -----------------------------------------------------------------------------
# Creating a complex variable.
# -----------------------------------------------------------------------------
complex_number = 10 + 5j
# complex_number stores the complex value (10+5j).
# -----------------------------------------------------------------------------
# Displaying the original complex value.
# -----------------------------------------------------------------------------
print("Original Complex Value :", complex_number)
# Output: Original Complex Value : (10+5j)
# -----------------------------------------------------------------------------
# Displaying the data type of the original variable.
# -----------------------------------------------------------------------------
print("Original Data Type :", type(complex_number))
# Output: Original Data Type : <class 'complex'>
# -----------------------------------------------------------------------------
# Attempting to convert the complex number into an integer.
# This operation raises a TypeError.
# -----------------------------------------------------------------------------
try:
integer_number = int(complex_number)
# -------------------------------------------------------------------------
# Displaying the converted integer value.
# This statement is not executed because an exception occurs.
# -------------------------------------------------------------------------
print("Converted Integer Value :", integer_number)
except TypeError as error:
# -------------------------------------------------------------------------
# Displaying the error message generated by Python.
# -------------------------------------------------------------------------
print("Error :", error)
# Output: Error : int() argument must be a string, a bytes-like object or
# a real number, not 'complex'
# -----------------------------------------------------------------------------
# Displaying an important note.
# -----------------------------------------------------------------------------
print("Note: A complex number cannot be converted directly into an integer.")
# Output: Note: A complex number cannot be converted directly into an integer.
# -----------------------------------------------------------------------------
# Displaying another important note.
# -----------------------------------------------------------------------------
print("Tip: Extract the real part first using .real, then convert it to int().")
# Output: Tip: Extract the real part first using .real, then convert it to int().