Repository navigation
Expand file tree
/
Copy path030-Float-to-Complex-Type-Casting.py
More file actions
77 lines (63 loc) · 3.49 KB
/
Copy path030-Float-to-Complex-Type-Casting.py
File metadata and controls
77 lines (63 loc) · 3.49 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
77
"""
===============================================================================
Float to Complex Type Casting
===============================================================================
Program Description:
--------------------
This program demonstrates how to convert a Float data type into a
Complex data type using the complex() function.
During this conversion, Python converts the float into a complex number
by assigning the float as the real part and 0 as the imaginary part.
Example:
99.99 ---> (99.99+0j)
Author : Shaik Mahaboob Basha
Repository : 13-Python
File Name : 30-Float-to-Complex-Type-Casting.py
===============================================================================
"""
# -----------------------------------------------------------------------------
# Creating a float variable.
# -----------------------------------------------------------------------------
float_number = 99.99
# float_number stores the float value 99.99.
# -----------------------------------------------------------------------------
# Displaying the original float value.
# -----------------------------------------------------------------------------
print("Original Float Value :", float_number)
# Output: Original Float Value : 99.99
# -----------------------------------------------------------------------------
# Displaying the data type of the original variable.
# -----------------------------------------------------------------------------
print("Original Data Type :", type(float_number))
# Output: Original Data Type : <class 'float'>
# -----------------------------------------------------------------------------
# Converting the float into a complex number using complex().
# The float becomes the real part, and the imaginary part is 0.
# -----------------------------------------------------------------------------
complex_number = complex(float_number)
# complex_number stores the complex value (99.99+0j).
# -----------------------------------------------------------------------------
# Displaying the converted complex value.
# -----------------------------------------------------------------------------
print("Converted Complex Value :", complex_number)
# Output: Converted Complex Value : (99.99+0j)
# -----------------------------------------------------------------------------
# Displaying the data type after conversion.
# -----------------------------------------------------------------------------
print("Converted Data Type :", type(complex_number))
# Output: Converted Data Type : <class 'complex'>
# -----------------------------------------------------------------------------
# Displaying the real part of the complex number.
# -----------------------------------------------------------------------------
print("Real Part :", complex_number.real)
# Output: Real Part : 99.99
# -----------------------------------------------------------------------------
# Displaying the imaginary part of the complex number.
# -----------------------------------------------------------------------------
print("Imaginary Part :", complex_number.imag)
# Output: Imaginary Part : 0.0
# -----------------------------------------------------------------------------
# Displaying an important note about Float-to-Complex conversion.
# -----------------------------------------------------------------------------
print("Note: complex() converts a float into a complex number with an imaginary part of 0.")
# Output: Note: complex() converts a float into a complex number with an imaginary part of 0.