Typecasting:
Fundamental
data types are int, float, complex, bool, str.
Python
provides five in-built functions for typecasting
Int()
Float()
Complex()
Bool()
Str()
Int()
type conversion:
This
type uses for convert other data types to int data type. Check some
examples
int
– int # no need to convert
float
–int
number = int(23.34)
print(number)
23 # result
Complex
–int
number = int(2+3j)
print(number)
TypeError: can't convert complex to
int # result
Bool
-- int
number = int(True)
print(number)
1 # result
number = int(False)
print(number)
0 # result
Str
– int
String internally contains integer value (base-10)
or decimal number then only convert string to an integer. Otherwise, it’s
giving an error.
number = int('300')
print(number)
300 # result
Float()
type conversion:
This
type uses to convert other data types to float data type. Check some examples
int
– float
we can convert any int number or any other int
types (decimal, binary, octal, hexadecimal) to float type.
1. number = float(15)
print(number)
15.0 # result
2. number = float(0b0101011)
print(number)
43.0 # result
3. number = float(0o4574)
print(number)
2428.0 # result
4. number = float(0x457bef)
print(number)
4553711.0 # result
Float – Float # no need to convert
Complex
–float
we can’t convert a complex number
to float type
number = float(2+3j)
print(number)
TypeError: can't convert complex to
float # result
Bool -- float
we can convert bool type to float type
number = float (True)
print(number)
1.0 # result
number = float (False)
print(number)
0.0 # result
Str – float
we can convert str type to float type
with some conditions
String internally contains integer value or float
value on (base-10) or decimal number then only convert string to float.
Otherwise, it’s giving an error.
number = float ('300')
print(number)
300.0 # result
number = float ('300.0')
print(number)
300.0 # result
Complex
() type conversion:
This
type uses to convert other data types to complex data types. Check some examples
int – complex
we can convert any int number or any other int
types like (decimal, binary, octal, hexadecimal) to complex type. And the complex syntax is (a+bj) type that’s why we can assign two types one is complex
(1), and the second one is complex (1,2) like.
1. number = complex (15) or complex
(15,7)
print(number)
(15+0j) # result and
(15+7j) # result
2. number = complex (0b0101011)
or complex (0b0101011,
0b0101011)
print(number)
(43+0j) #
result and (43+43j)
3. number = complex (0o4574) or complex (0o4574, 0o4574)
print(number)
(2428+oj) # result
and (2428+2428j)
4. number = complex
(0x457bef) or complex (0x457bef, 0x457bef)
print(number)
(4553711+0j) #
result and (4553711+4553711j)
Float – complex
we can convert any float number to a complex type.
1. number = complex (15.0) or complex
(15.0,7.0)
print(number)
(15+0j) # result and
(15+7j) # result
Bool -- complex
we can convert bool type to complex type
number = complex(True)
print(number)
(1+0j) # result
number = complex (False)
print(number)
(0+0j) # result
Str – complex
we can convert str type to complex type
with some conditions
String internally contains integer value or float
value on (base-10) or decimal number then only convert string to complex.
Otherwise, it’s giving an error. and the second type not accepted.
number = complex ('300' )
print(number)
(300+0j) # result
number = complex ('300' , '300')
print(number)
Syntax error# result
number = complex ('300.0')
print(number)
(300+0j) # result
number = complex ('300.0' , '300.0')
print(number)
Syntex error# result
Bool
() type conversion:
This
type uses to convert other data types to a bool data type.
Note:
this is True and False having only. That’s why if we are given data or value is
zero, it takes as a False otherwise it’s taking as a True.
Check
some examples
int – Bool
we can convert any int number or any other int
types (decimal, binary, octal, hexadecimal) to bool type.
1. number = bool(15)
print(number)
True # result
2. number = bool (0b0101011)
print(number)
True # result
3. number = bool (0o4574)
print(number)
True # result
4. number = bool(0x457bef)
print(number)
True # result
Float – Bool
we can convert float number to
bool type
number
= bool (23.34)
print(number)
True # result
Complex -- Bool
we can convert complex type to bool type
number = bool (2+4J)
print(number)
True # result
number = bool (0+0J)
print(number)
False # result
Bool –B00l # no need to convert
Str – Bool
we can convert str type to bool type
String internally contains some data it’s taking as
True if it is an empty string it’s taking as a False.
number = bool ('300')
print(number)
True # result
number = bool (' ')
print(number)
False # result
Str
() type conversion:
This
type uses to convert other data types to str data type.
Check
some examples
int – Str
we can convert any int number or any other int
types (decimal, binary, octal, hexadecimal) to str type.
1. number = str(15)
print(number)
‘15’ # result
2. number = str(0b0101011)
print(number)
‘43’ # result
3. number = str (0o4574)
print(number)
‘2428’ # result
4. number = str(0x457bef)
print(number)
‘4553711’ # result
Float – Str
we can convert float number to
str type
number
= str (23.34)
print(number)
'23.34' # result
Complex -- Bool
we can convert complex type to bool type
number = str (2+4J)
print(number)
'(2+4J)' # result
Bool
–Str
number
= str (True)
print(number)
'True' # result
number = str(False)
print(number)
'False' # result
Str – Str # no need to convert
Post a Comment