Python Type Casting


What is Python Type Casting

Type casting, also known as type conversion, is a process of converting the variable of one data type to the other.

For Example:

a = "45"

Suppose “a” is stored as a string, but it may be needed as an integer to perform some operation like addition, to achieve this goal, we can use typecasting.

There are two types of type casting:

  1. Implicit Type Casting (or Implicit Type Conversion or Upcasting).
  2. Explicit Type Casting (or Explicit Type Conversion).
Python Type Casting. Implicit and Explicit.

Implicit Type Casting

Implicit type casting is performed automatically by the python interpreter. In this, the interpreter’s priority is to avoid data loss. To achieve this, it converts lower data types like an integer to higher data types like float.

The best example for understanding implicit type casting is the addition of a float with an integer. The integer value will be converted to float because python is supposed to convert lower data type to higher data type to avoid data loss.

In [1]:
product_1_Price = 5
product_2_Price = 5.5

sum = product_1_Price + product_2_Price
print(sum)
10.5

Now check the data type of each variable.
When addding integer and float, final result is float

In [2]:
print("Product 1 Price data type = ", type(product_1_Price))
print("Product 2 Price data type = ", type(product_2_Price))
print("Sum data type             = ", type(sum))
Product 1 Price data type =  <class 'int'>
Product 2 Price data type =  <class 'float'>
Sum data type             =  <class 'float'>

Explicit Type Casting

Explicit type casting is performed manually by the developer. To achieve this, we use inbuilt constructor functions. For example, we use the int() function to convert a float or string into an integer.

Below is the table showing the data types and function used for typecasting:

DatatypeFunction used for casting
Integerint()
Floatfloat()
Complexcomplex()
Booleanbool()
Stringstr()
Listlist()
Tupletuple()
Setset()

Example – Add Two Numbers

The best example to understand explicit type casting is to ask the user to input two numbers and add them. Here, the data type of numbers received by the input function is a string. Python cannot convert them into a float or an integer automatically. We have to do the manual conversion.

In [1]:
first_num = input("Enter first number:")
second_num = input("Enter second number:")

print("Sum = ", float(first_num)+float(second_num))
Enter first number:5.5
Enter second number:5
Sum =  10.5

Examples(Few Type Cast Operations)

Few examples of some of the typecast operations possible:

String to Integer.

In [1]:
a = "45"
type(a)
Out[1]:
str
In [2]:
a = "45"
b = 10
sum = int(a)+b
sum
Out[2]:
55

String to Float.

In [3]:
a = "45"
b = 10.5
sum = float(a)+b
sum
Out[3]:
55.5

String to Float.

In [4]:
emp_id = 205
emp_name = "Tim"
emp_code = str(emp_id)+"_"+emp_name
emp_code
Out[4]:
'205_Tim'

Integer to Float.

In [5]:
a = 45
float(a)
Out[5]:
45.0

Float to Integer.

In [6]:
a= 45.89
int(a)
Out[6]:
45

Integer to Complex.

In [7]:
complex(45)
Out[7]:
(45+0j)

Integer to Boolean.

In [8]:
bool(1)
Out[8]:
True
In [9]:
# Note that other than 0 all numbers will return True
bool(2)
Out[9]:
True
In [10]:
bool(0)
Out[10]:
False

List to Tuple.

In [11]:
cust_id = [25,26,27,28,29]
type(cust_id)
Out[11]:
list
In [12]:
cust_id_tuple = tuple(cust_id)
type(cust_id_tuple)
Out[12]:
tuple
In [13]:
cust_id_tuple
Out[13]:
(25, 26, 27, 28, 29)

List to Set.

In [14]:
cust_id = [25,26,27,28,29]
type(cust_id)
Out[14]:
list
In [15]:
cust_id_set = set(cust_id)
type(cust_id_set)
Out[15]:
set
In [16]:
cust_id_set
Out[16]:
{25, 26, 27, 28, 29}

Set to Tuple.

In [17]:
cust_id = {25,26,27,28,29}
type(cust_id)
Out[17]:
set
In [18]:
cust_id_tuple = tuple(cust_id)
type(cust_id_tuple)
Out[18]:
tuple
In [19]:
cust_id_tuple
Out[19]:
(25, 26, 27, 28, 29)

Set to List.

In [20]:
cust_id = {25,26,27,28,29}
type(cust_id)
Out[20]:
set
In [21]:
cust_id_list = list(cust_id)
type(cust_id_list)
Out[21]:
list
In [22]:
cust_id_list
Out[22]:
[25, 26, 27, 28, 29]

Set to List.

In [23]:
cust_id = (25,26,27,28,29)
type(cust_id)
Out[23]:
tuple
In [24]:
cust_id_list = list(cust_id)
type(cust_id_list)
Out[24]:
list
In [25]:
cust_id_list
Out[25]:
[25, 26, 27, 28, 29]

Tuple to Set.

In [26]:
cust_id = (25,26,27,28,29)
type(cust_id)
Out[26]:
tuple
In [27]:
cust_id_set = set(cust_id)
type(cust_id_set)
Out[27]:
set
In [28]:
cust_id_set
Out[28]:
{25, 26, 27, 28, 29}

Resources

Handwritten Notes

Python Type Casting Handwritten Notes

References:

  1. Type Casting
  2. Implicit and Explicit
  3. Benefits of Type casting