Typecasting in Python | type conversion with examples

Typecasting-in-Python

Typecasting in Python is a powerful concept that allows you to convert one data type into another.

It is useful for data conversion between different types like integers, strings, and floats.

With typecasting, you can also make sure that a variable contains the right type of data.

For example, if you are expecting an integer value, you can use typecasting to make sure that the data is an integer.

Typecasting is also useful when you need to convert a string to a number, or vice versa.

With typecasting, you can also convert between other data types such as boolean, list, tuple, and dictionary.

Knowing how to typecast in Python can help you to write better code and improve the performance of your programs.

There is some situation when need to convert the type from one to another.

This is a very important lecture and keeps full attention because typecasting is used in many important programs.

Also, Know Python application examples ( What you can do with Python?).

So let’s start to learn…

Type Conversion in Python

Type conversion in Python is a process of converting one data type into another data type.

It is an important concept to understand in programming as it allows you to manipulate data in a way that is useful for your program.

Python has several built-in functions that allow you to easily convert types, such as int(), float(), str(), and bool().

Each type of conversion function takes the value you want to convert as an argument and returns the converted value.

For example, int(3.14) will return 3, and float(‘7’) will return 7.0.

It is important to be aware of the possible data loss when converting between data types, as some conversions can result in an approximation or truncation of the original value.

Understanding type conversion can help you make the most of your data and write code that is effective and efficient.

This guide is aimed at providing information about conversion functions.

Why and when to use Type-casting in Python?

Casting is when you convert a variable value from one type to another. That is, in Python, done with functions like int() or float() or str().

A pattern is that you convert a number as a string into a number.

The practice of converting the value of a single data type (integer, string, float, etc.) to a different data type is known as type conversion.

Python includes two types of type conversion.

  • Explicit Conversion
  • Implicit Conversion

Explicit Conversion

Explicit Type Conversion in Typecasting

In Explicit Type Conversion, users convert the data type of the item to the needed data type.

We use predefined roles such as int(), float(), str(), etc to execute explicit type conversion.

num_int = 327
num_string = "573"

print("Data type of num_int:",type(num_int))
print("Data type of num_string before Type Casting:",type(num_string))

num_string = int(num_string)
print("Data type of num_string after Type Casting:",type(num_string))

num_sum = num_int + num_string

print("Sum of num_int and num_string:",num_sum)
print("Data type of the sum:",type(num_sum))

#OutPut will be:

Data type of num_int: <class 'int'>
Data type of num_string before Type Casting: <class 'str'>
Data type of num_string after Type Casting: <class 'int'>
Sum of num_int and num_string: 900
Data type of the sum: <class 'int'>

In the preceding program:

We include num_string and num_int factor.
We switched num_string from the string(greater ) into an integer(reduced ) type utilizing int() work (Python Homework) to carry out the addition.

After converting num_string into some integer value Python can bring these two factors.

Implicit Conversion

Implicit Type Conversion in typecasting

This procedure does not require any user participation.

Let us see an instance where Python boosts the conversion of a lower data type (integer) to a greater data type (floats) to prevent data loss.

Example: Converting Int to Float

num_int = 321
num_float = 1.84

num_new = num_int + num_float

print("datatype of num_int:",type(num_int))
print("datatype of num_float:",type(num_float))

print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))
#OutPut Will be
datatype of num_int: <class 'int'>
datatype of num_float: <class 'float'>
Value of num_new: 322.84
datatype of num_new: <class 'float'>

In the preceding program,

We include two factors num_int and num_float, preserving the value in num_new.
We’ll have a look at the data type of three items respectively.

From the output, we may observe the datatype of num_int is an integer, the datatype of num_float is a float.

Additionally, we may observe the num_new has float data type as Python consistently transforms smaller data type to bigger data type to prevent the lack of data.

Let us take an example to understand this logic.

Python Type Casting Logic

Here you can get a simple Logic to convert one Data Type to another Data Type. There is no rocket science, simply you can follow the simple steps as given below.

Python Cast to Float

Python cast to Float: Need to convert the Integer and String Data Type into Float.

x=float(2)
y=float(30.0)
z=float("20")
print(x)
print(y)
print(z)

Output:
2.0
30.0
20.0

Python Cast to Int

Python cast to Int: Need to convert the Float and String Data Type Cast to Int python.

x=int(2)
y=int(30.0)
z=int("20")
print(x)
print(y)
print(z)

Output:
2
30
20

Cast to String Python

Cast to String Python: Need to convert the Float and Int Data Type Cast to string python.

x=str(2)
y=str(30.0)
z=str("20")
print(x)
print(y)
print(z)

Output:
2
30.0
20

As you can observe three types of casting that construct value according to the casting. Pythonic is an easy language that has many predefined functions to solve these types of problems.

Let us take a more real-life example to understand the type of casting and their usage in the real-life problem.

For example, we want to add two numbers in which one variable has the string value and the second variable has an integer value.

How is it possible to add? we can solve these problems by using Python typecasting. Let see an example program

How to add two numbers in which one variable has the string value and second has integer?

x=10
y="5"
# first we can convert an integer to add
z=int(y)       # here int() function is used for casting
result=x+z
print(result)

Output: 15

Note: String value cannot be added with the integer value. Therefore, it is not possible to add without Typecasting. In the above example, first, we can convert the string value into an integer value. Then we can add.

I hope this article is very helpful to understand “Typecasting in Python | Python type Conversion“.

Video’s Credit: ProgrammingWithHarry 

If you have any questions please discuss them in the comments section. Also, give your feedback that is very helpful to improve performance.

Some Useful Article links

Are you want to learn about: Python Class inside Class (Nested Classes in Python)

Leave a Comment