Repository navigation
Expand file tree
/
Copy path012-Explicit-Type-Casting.py
More file actions
141 lines (117 loc) · 5.92 KB
/
Copy path012-Explicit-Type-Casting.py
File metadata and controls
141 lines (117 loc) · 5.92 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
===============================================================================
Explicit Type Casting
===============================================================================
Program Description:
--------------------
This program demonstrates Explicit Type Casting (Manual Type Conversion)
in Python.
Explicit Type Casting is performed manually by the programmer using
Python's built-in type conversion functions such as:
1. int()
2. float()
3. str()
4. bool()
5. complex()
It is used when the programmer wants to convert one data type into
another explicitly.
Author : Shaik Mahaboob Basha
Repository : 13-Python
File Name : 12-Explicit-Type-Casting.py
===============================================================================
"""
# -----------------------------------------------------------------------------
# Creating a float variable.
# -----------------------------------------------------------------------------
number = 25.75
# number stores the float value 25.75.
# -----------------------------------------------------------------------------
# Displaying the original value.
# -----------------------------------------------------------------------------
print("Original Value :", number)
# Output: Original Value : 25.75
# -----------------------------------------------------------------------------
# Displaying the original data type.
# -----------------------------------------------------------------------------
print("Original Data Type :", type(number))
# Output: Original Data Type : <class 'float'>
# -----------------------------------------------------------------------------
# Converting the float value into an integer.
# int() removes the decimal part of the float value.
# -----------------------------------------------------------------------------
integer_number = int(number)
# integer_number stores the integer value 25.
# -----------------------------------------------------------------------------
# Displaying the converted integer value.
# -----------------------------------------------------------------------------
print("Integer Value :", integer_number)
# Output: Integer Value : 25
# -----------------------------------------------------------------------------
# Displaying the data type after conversion.
# -----------------------------------------------------------------------------
print("Data Type :", type(integer_number))
# Output: Data Type : <class 'int'>
# -----------------------------------------------------------------------------
# Converting the integer value into a string.
# str() converts the integer into a string.
# -----------------------------------------------------------------------------
string_value = str(integer_number)
# string_value stores the string "25".
# -----------------------------------------------------------------------------
# Displaying the converted string value.
# -----------------------------------------------------------------------------
print("String Value :", string_value)
# Output: String Value : 25
# -----------------------------------------------------------------------------
# Displaying the data type after conversion.
# -----------------------------------------------------------------------------
print("Data Type :", type(string_value))
# Output: Data Type : <class 'str'>
# -----------------------------------------------------------------------------
# Converting the integer value into a float.
# float() converts the integer into a floating-point number.
# -----------------------------------------------------------------------------
float_value = float(integer_number)
# float_value stores the float value 25.0.
# -----------------------------------------------------------------------------
# Displaying the converted float value.
# -----------------------------------------------------------------------------
print("Float Value :", float_value)
# Output: Float Value : 25.0
# -----------------------------------------------------------------------------
# Displaying the data type after conversion.
# -----------------------------------------------------------------------------
print("Data Type :", type(float_value))
# Output: Data Type : <class 'float'>
# -----------------------------------------------------------------------------
# Converting the integer value into a complex number.
# complex() converts the integer into a complex number.
# -----------------------------------------------------------------------------
complex_value = complex(integer_number)
# complex_value stores the complex value (25+0j).
# -----------------------------------------------------------------------------
# Displaying the converted complex value.
# -----------------------------------------------------------------------------
print("Complex Value :", complex_value)
# Output: Complex Value : (25+0j)
# -----------------------------------------------------------------------------
# Displaying the data type after conversion.
# -----------------------------------------------------------------------------
print("Data Type :", type(complex_value))
# Output: Data Type : <class 'complex'>
# -----------------------------------------------------------------------------
# Converting the integer value into a Boolean value.
# Any non-zero number becomes True.
# -----------------------------------------------------------------------------
boolean_value = bool(integer_number)
# boolean_value stores the Boolean value True.
# -----------------------------------------------------------------------------
# Displaying the converted Boolean value.
# -----------------------------------------------------------------------------
print("Boolean Value :", boolean_value)
# Output: Boolean Value : True
# -----------------------------------------------------------------------------
# Displaying the data type after conversion.
# -----------------------------------------------------------------------------
print("Data Type :", type(boolean_value))
# Output: Data Type : <class 'bool'>