Quantcast
Channel: Python - ByteArray
Viewing all articles
Browse latest Browse all 10

Type casting in Python

$
0
0

Type casting is the process or way to convert or cast the one data type to another, as it is clear from its name Type casting, it’s also called type conversion where one data type is converted to another.

e.g. typecast of integer value to float value, or vice-versa.

Type casting in Python

In Python, there is also a type casting. In Python we can do type casting in two ways:

 

Implicit Type casting

Explicit Type casting

 

Implicit Type casting

Implicit type casting in python means which happened implicit means by own. Python do it by own.  In this, Python automatically converts one data type to another. In this casting programmer need not to do anything.

Example: Let’s understand it by taking example.

salary=10000
incentive=100.50
total = salary + incentive
print (type(salary))
print (type(incentive))
print (type(total))
print ("**************")
print(total)

Output is

<class 'int'>
<class 'float'>
<class 'float'>
**************
10100.5

 

Observations from implicit type casting example in Python

  • Salary is of type int. (number)
  • Incentive is of type float (decimal)
  • When we sum integer and float, it automatically converts the integer to float to make sure no data lose.

 

Explicit Type conversion in Python

Explicit type conversion is that when we have to do explicit type conversion, means we have to manual do the typecast the value.

Let’s understand it by example


salary=10000
incentive="100"
total = salary + incentive
print(total)

Output


TypeError: unsupported operand type(s) for +: 'int' and 'str'

 

Observation

  • salary is of type int.
  • incentive is of type String.
  • When we sum u integer and String value, it throws TypeError

So here we need to do explicit typecasting as Python will not typecast String to Integer.

Explicit typecasting in Python

So, to Overcome these errors, Python has way to handle this by explicit typecasting. Below is some python explicit typecasting.

Integer and float typecasting

String typecasting

Python Collections type casting

 

Let’s discuss one by one

Convert any date type to Integer (Number)

In this conversion, we use int() function to convert any data type to integer.

  1. Convert float to Integer
f = 10.2
c = int(f)
print ("convert float to integer: ")
print (c)

Output

convert float to integer:

10

 

Here int() converts the float value to int value and remove the value after the decimal, As we can see that in float to integer conversation data lose will occur.

Convert String to integer

st = "10"
#type cast string to integer
c = int(st)
print ("convert to integer: ")
print (c)

Output

convert to integer:

10

 

Convert to number with some base

We can also convert to number with base value. In this case our method will be int(I,base_value). Let’s understand by some example:

st = "101"
c = int(st,2)
print ("convert to integer with base: ")
print (c)

Output will be

convert to integer with base:
5

 

Here python int() function will convert 101 as we give instruction to Python that change the value 101 to integer with base 2.

Convert String to integer

 

st = "101"
#type cast string to integer
c = int(st)
print ("convert to integer with base: ")
print (c)
print(type(c))

Output

convert to integer with base: 
101
<class 'int'>

 

Here String “101” is converted to integer, we can verify it by type() method of Python.

Convert any date type to float (Decimal)

We can also value to float(decimal). See below:

Typecast Integer to float value

We can type cast integer to float by using float() method of python. Below is the example of using float() method.

Example:

value = 101

#type cast to float

data = float(value)
print ("convert to float: ")
print (data)
print(type(data))

Output will be

convert to float: 
101.0
<class 'float'>

 

Typecast String to float value

We can type cast String to float by using float() method of python. Below is the example of using float() method.

Example:

value = "101"

#type cast to float

data = float(value)
print ("convert to float: ")
print (data)
print(type(data))

Output will be


convert to float: 
101.0
<class 'float'>


 

Convert Character to integer (Number)

In Python, we can convert character into integer also. Python ord() function is used to convert character into integer value. Python ord() function will return the ASCII value of character.

Example:

c = "A"

#type cast character to integer

data = ord(c)
print ("convert from cast character to integer: ")
print (data)
print(type(data))

Output

convert from cast character to integer: 
65
<class 'int'>


 

Explanation: In the above example, we have character “A”. when we apply ord() function on it, it return the ASCII value of character “A”. Similarly we can get the ASCII value of any character using ord() function in python.

 

Convert Integer to Character

In python we can also convert integer to Character. Python chr() function is used for it. When we pass the ASCII value of some character It will return the character for corresponding ASCII value.

Example:

c = 68

#type cast integer to character
data = chr(c)
print ("convert from cast integer to character: ")
print (data)

Output

convert from cast integer to character:

D

 

Explanation: When we passed the 68 (ASCII value of D). Python chr() function return the character corresponding to that ASCII code.

Convert Integer to String

In Python, we can easily convert integer to String using str() function of Python.

Example:

i = 68

#type cast integer to String
data = str(i)
print ("convert from integer to string: ")
print (data)
print(type(data))

 Output

convert from integer to string: 
68
<class 'str'>


 

Explanation: As variable i is integer having value 68, str() function will convert it to String value.

 

Convert type into tuple

In Python, value can also be converted into tuple. A tuple is a collection of data like

vegetables= (“potatoes”,” onions”,” pumpkin”)

Here vegetable is a tuple. We will read detail about python tuple in coming posts.

So we can convert some data into tuple also. Python tuple() function is used for this.

Example:


value = "hello"

#type cast to tuple
data = tuple(value)
print ("convert to tuple: ")
print (data)
print(type(data))

Output


convert to tuple: 
('h', 'e', 'l', 'l', 'o')
<class 'tuple'>

 

Explanation: tuple() function convert the each character of “Hello” into its tuple elements.

Convert into set

In Python, we can convert value to set by using set() function of Python.

Example:


value = "hello"

#type cast to set
data = set(value)
print ("convert to set: ")
print (data)
print(type(data))

 Output


convert to set: 
{'e', 'o', 'h', 'l'}
<class 'set'>

 

Explanation:  In set we cannot have duplicate values, that why set() function will eliminate the duplicate value and order is not preserved, as we can see “helo” words are not arranged.

Convert into list

Values can be easily convert into Python list by using list() function of Python. see below example to understand more.

Example


value = "hello"

#type cast to list
data = list(value)
print ("convert to list: ")
print (data)
print(type(data))

Output 


convert to list: 
['h', 'e', 'l', 'l', 'o']
<class 'list'>

 

Explanation: list can have duplicate values, and here list() function in Python is used to convert the “hello” value to create a list.

The post Type casting in Python appeared first on ByteArray.

Viewing all articles
Browse latest Browse all 10

Trending Articles