Type Casting in Python

Rina Mondal
2 min readJan 9, 2024

--

Type casting, also known as type conversion, is the process of converting one data type into another. Ex: you want to change the data type of a variable from an integer (2) to a float (2.0). This is a case where you'd use type casting.

Type casting can be broadly categorized into two types: implicit (automatic) casting and explicit (manual) casting.

  1. Implicit Casting (Automatic Type Conversion): Occurs automatically when the system handles the conversion without the programmer’s intervention.
    Generally involves converting a “smaller” data type to a “larger” one to prevent data loss.

Ex: converting an integer to a floating-point number:

# Implicit casting (automatic) example in Python
integer_number = 10
float_number = 5.5

result = integer_number + float_number
# The integer '10' is implicitly cast to a float before the addition

print(result)

# Output: 15.5

2. Explicit Casting (Manual Type Conversion): Requires the programmer to explicitly specify the type conversion. Involves converting a “larger” data type to a “smaller” one. Ex: Converting a float to an integer or a string to an integer:

float_number = 10.5
int_number = int(float_number) # Explicit casting from float to int
str_number = str(float_number)
print(int_number)
print(str_number)
type(str_number)

# O/t-
10
10.5
str

It’s important to note that not all type conversions are valid, and attempting to convert incompatible types may result in errors or unexpected behavior.

Reasons for using type casting:

  1. Compatibility and Operations:
    Mixed Data Types: When you need to perform operations or comparisons involving variables of different data types, type casting helps ensure compatibility. For example comparing a string to a number.
integer_number = 5

# Comparing a string to a number
string_number = "10"
integer_from_string = int(string_number)

if integer_from_string > integer_number:
print(f"{integer_from_string} is greater than {integer_number}")
else:
print(f"{integer_from_string} is not greater than {integer_number}")

O/t- 10 is greater than 5

2. Function Parameters: When calling functions, you may need to pass arguments of a specific type. Type casting allows you to prepare the data appropriately before passing it to a function.

def multiply_numbers(num1, num2):
return num1 * num2

user_input1 = input("Enter the first number: ")
user_input2 = input("Enter the second number: ")

# Type casting the input strings to integers
num1 = int(user_input1)
num2 = int(user_input2)

result_product = multiply_numbers(num1, num2)

print("Product:", result_product)

#without type casting it will show as error: TypeError: can't multiply
sequence by non-int of type 'str'

3. User Input Handling: When dealing with user input, data is often initially received as strings. Type casting is necessary to convert these strings into the appropriate data types for further processing. Above example can be taken as a reference.

4. Programming Flexibility: Type casting allows for adaptability in different scenarios. It provides the flexibility to work with different types of data and integrate them seamlessly within a program.

In summary, type casting is a fundamental concept in programming that enables the manipulation, comparison, and combination of data of different types. It ensures that the program behaves as intended and that data is handled in a way that aligns with the specific requirements of the task at hand.

--

--

Rina Mondal

I have an 8 years of experience and I always enjoyed writing articles. If you appreciate my hard work, please follow me, then only I can continue my passion.