Day07 - Typecasting in Python

Day07 - Typecasting in Python

Implicit Conversion and Explicit Conversion

Typecasting:

The conversion of one data type into the other data type is known as 'type casting' in python or 'type conversion' in python.

Python supports a wide variety of functions or methods like: int(), float(), str(), ord(), hex(), oct(), tuple(), set(), list(), dict(), etc. for the type casting in python.

Example:

a = "1"  #string
b = "2"  #string
print(a + b)  #Output= 12 (because a and b are strings)

c = 1 #integar
d = 2 #integar
print(c + d) #Output= 3 (because a and b are integars)

Types of Typecasting

  1. Implicit Conversion (Implicit type casting in python).

  2. Explicit Conversion (Explicit type casting in python)

Implicit Type Conversion in Python:

In this, method, Python converts the datatype into another datatype automatically. Users don’t have to involve in this process.

Python converts a smaller data type to a higher data type to prevent data loss. This is called, 'implicit typecasting' in python.

Example 01:

a = 8 #a is int
b = 1.9 #b is float
print(a + b)  #Output= 9.9

Note: In the above example, python coverts 'int' into 'float' than add float + float - and gives answer in float.

Example 02:

# Python automatically converts a to int
a = 7
print(type(a))

# Python automatically converts b to float
b = 3.0   
print(type(b))

# Python automatically converts c to float as it is a float addition
c = a + b
print(c)
print(type(c))

# Python automatically converts d to float as it is a float multiplication
d = a * b
print(d)
print(type(d))

Output:

<class 'int'>
<class 'float'>
10.0
<class 'float'>
21.0
<class 'float'>

Explicit Type Conversion in Python:

In this method, Python needs user involvement to convert the variable data type into the required data type.

It means that the conversion of one data type into another data type, done via developer or programmer's intervention or manually as per the requirement, is known as 'explicit type conversion'.

It can be achieved with the help of Python’s built-in type conversion functions such as int(), float(), hex(), oct(), str(), etc .

Example 01:

a = "1" #string 
b = "2" #string

#int - convert string into integar 
#then perform operation.
print(int(a) + int(b))     #Output = 3

Example 02:

  • Python Convert Int to Float
# Python program to demonstrate type Casting
a = 5 # int variable
n = float(a) # typecast to float

print(n)        #Output: 5.0
print(type(n))  #Output: <class 'float'>
  • Python Convert Float to Int
# Python program to demonstrate type Casting
a = 5.9      # int variable
n = int(a)   # typecast to int

print(n)        #Output: 5
print(type(n))  #Output: <class 'int'>
  • Python Convert int to String
# Python program to demonstrate type Casting
a = 5        # int variable
n = str(a)   # typecast to str

print(n)        #Output: 5
print(type(n))  #Output: <class 'str'>
  • Python Convert String to float
# Python program to demonstrate type Casting
a = "5.9"      # string variable
n = float(a)   # typecast to float

print(n)       #Output: 5.9
print(type(n)) #Output: <class 'float'>

<< Previously -------------------------------------------------- Next Lesson >>