/    /  Python – Casting

Python-Casting

In this tutorial you will learn python in detail.

 

Python provides type conversion functions to directly convert one data type to another data type. There are two types of conversions.

  1. Implicit type conversion
  2. Explicit type conversion

 

Implicit type conversion

 

Python converts one data type to another data type automatically without user intervention.

 

Converting integer to float

 

Example:

 

num_int = 253
num_float = 1.24
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:

 

datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>
Value of num_new: 254.24
datatype of num_new: <class 'float'>

 

In the above example, we added two variables of integer and float values. The result generated is of float value. This is because Python always converts smaller data types to larger data types.

Python cannot add string and integer values, because it is not able to use Implicit conversion in such condition.

However, in such condition’s Python has a solution which is Explicit type conversion.

 

Explicit Type Conversion

 

In Explicit Type Conversion, we use predefined functions like int (), float (), str () etc. to perform Explicit type conversion.

Users cast (changes) the data types of the objects, hence it is called as typecasting.

  1. Int (a, base): This converts any data type to integer. ‘Base’ represents the base in which string is if datatype is string.
  2. Float (): This converts any data type to floating value.

 

Example:

 

# initializing string
x = "10010"


# printing string converting to int base 2
z = int(x,2)
print ("After converting to integer base 2: ", end="")
print (z)


# printing string converting to float
e = float(x)
print ("After converting to float: ", end="")
print (e)

 

Output:

 

After converting to integer base 2: 18
After converting to float: 10010.0

 

  1. Ord (): This function converts a character to integer.
  2. hex (): This function converts integer to hexadecimal
  3. oct (): This function converts integer to octal string.

 

s = '4'
# printing character converting to integer
c = ord(s)
print ("After converting character to integer: “, end="")
print (c)

# printing integer converting to hexadecimal string
c = hex (56)
print ("After converting 56 to hexadecimal string: “, end="")
print (c)

# printing integer converting to octal string
c = oct (56)
print ("After converting 56 to octal string: “, end="")
print (c)

 

Output:

 

After converting character to integer: 52
After converting 56 to hexadecimal string: 0x38
After converting 56 to octal string: 0o70

 

  1. tuple (): This function converts to a tuple.
  2. Set (): This function is used to convert to set.
  3. List (): This function is used to convert any datatype to a list type.

 

Example:

 

x = 'Python'


# printing string converting to tuple
c = tuple(x)
print ("After converting string to tuple: “, end="")
print (c)


# printing string converting to set
c = set(x)
print ("After converting string to set: “, end="")
print (c)


# printing string converting to list
c = list(x)
print ("After converting string to list: “, end="")
print (c)

 

Output:

 

After converting string to tuple: ('P', 'y', 't', 'h', 'o', ‘n’)
After converting string to set: {'y', 'P', 't', 'h', ‘n’, ‘o’}
After converting string to list: ['P', 'y', 't', 'h', 'o', ‘n’]

 

  1. dict (): This function converts a tuple of order (key,value) into a dictionary.
  2. str(): Used toconvert integer into a string.
  3. complex(real,imag) : : This functionconverts real numbers to complex(real,imag) number.

 

Example:

 

a = 1
b = 2


# initializing tuple
tup = (('a', 1), ('b', 2), ('c', 3))


# printing integer converting to complex number
c = complex (2,3)
print ("After converting integer to complex number: “, end="")
print (c)

# printing integer converting to string
c = str(a)
print ("After converting integer to string: “, end="")
print (c)

# printing tuple converting to expression dictionary
c = dict(tup)
print ("After converting tuple to dictionary: “, end="")
print (c)

 

Output:

 

After converting integer to complex number: (2+3j)
After converting integer to string: 1
After converting tuple to dictionary: {'a': 1, 'b': 2, 'c': 3}

 

  1. chr (number): This functionconverts number to its corresponding ASCII character.

 

Example:

 

a = chr(76)
b = chr(77)

print(a)
print(b)

 

Output:

 

L
M