Home
About Us Privacy Policy
 

TYPECASTING IN PYTHON

ADVERTISEMENT

Intermediate Typecasting



What is Typecasting, and why is it required?

In programming, Typecasting is a technique of converting a variable to a different data type. But, before delving into the programming aspect, let us understand it through a more understandable example. We've all been in circumstances where we admire certain parts of a product that we saw on the internet catalog or in a store somewhere, but it wasn't entirely to our liking; we wished we could only alter certain aspects to make it the way we want it to be. Well, when it comes to dealing with digital objects, alterations are possible.

During the application development, you might need to convert the value of one data type to another for specific purposes discussed in later topics.

In Python, you can convert a variable of type a to type b using in-built methods.


Converting int to float using the in-built float() method.

Python provides the in-built float() method to convert type int into float type value.

Example:

my_int_val = 1234
my_float_val = float(my_int_val)
print(my_float_val)

1234 .0

You can only convert int into float data type.


ADVERTISEMENT

Converting float to int using the in-built int() method.

Python provides the in-built int() method to convert type complex, float into int type value.

Example:

my_float_value = 1234 .5678
my_int_value = int(my_float_value)
print(my_int_value)

1234

You can only convert float into int data type.


Converting int, float to complex using the in-built complex() method.

Python provides the in-built complex() method to convert type int, float into complex type value.

Example:

my_float_value = 1234 .5678
my_int_val = 8
my_complex_val_1 = complex(my_float_value)
my_complex_val_2 = complex(my_int_val)
print(my_complex_val_1, my_complex_val_2)

(1234 .5678+0j) (8+0j)


ADVERTISEMENT

Converting lists, set into a tuple using the in-built tuple() method.

Python provides the in-built tuple() method to convert type lists, set into tuple type value.

Example:

my_list = [4932, 829, 11, 3.14]
my_set = {1953, 8134, 43, 193}
tuple_1 = tuple(my_list)
tuple_2 = tuple(my_set)
print(tuple_1, tuple_2)

(4932, 829, 11, 3.14) (1953, 43, 193, 8134)


Converting lists, tuple into a set using the in-built set() method.

Python provides the in-built set() method to convert type lists, tuple into set type value.

Example:

my_list = [4932, 829, 11, 3.14]
my_tuple = (1953, 8134, 43, 193)
set_1 = set(my_list)
set_2 = set(my_tuple)
print(set_1, set_2)

{3.14, 11, 4932, 829} {1953, 43, 193, 8134}


Converting set, tuple into a list using the in-built list() method.

Python provides the in-built list() method to convert type set, tuple into list type value.

Example:

my_set = {4932, 829, 11, 3.14}
my_tuple = (1953, 8134, 43, 193)
list_1 = list(my_set)
list_2 = list(my_tuple)
print(list_1, list_2)

[11, 3.14, 4932, 829] [1953, 8134, 43, 193]


ADVERTISEMENT

Getting a string representation of any object using the in-built str() method.

Python provides the in-built str() method to perform various operations. One of them is getting the string representation of an object.

In Python, everything is an object, numerical types, lists, tuples, sets, custom implemented classes, everything.

Example:

my_list = [11, 3.14, 4932, 829]
my_set = {93403, 2212, 39, 82, 111, "A"}
my_str_1 = str(my_set)
my_str_2 = str(my_list)
print(my_str_1, my_str_2)

{82, 2212, 39, 'A', 93403, 111} [11, 3.14, 4932, 829]


Conclusion

This chapter taught us what typecasting is, why it is required, its applications, use-cases, and several examples of converting one data type into another.


ADVERTISEMENT



All product names, logos, and brands are property of their respective owners.