Home Micropython Python Basics : Typecasting

Python Basics : Typecasting

by shedboy71

We can convert one type of value to another type. This is known as Typecasting or Type conversion.

There are a variety of inbuilt functions for typecasting which are

  • int()
  • float()
  • complex()
  • bool()
  • str()

Let’s look at the first one

int() :

We use this function to convert values from other types to an int

#!/usr/bin/env python
 
print(int(3.1456))
print(int(True))
print(int(False))
print(int("10"))

You should see this

3
1
0
10

You can also try these, they will return a ValueError: invalid literal for int() with base 10:

#!/usr/bin/env python

print(int("16.7"))
print(int("0B1001"))
print(int("twenty"))

 

float() :

We can use float() function to convert other type values to a float type.

#!/usr/bin/env python

print(float(3)) 
print(float(True)) 
print(float(False)) 
print(float("10"))

You should see this

3.0
1.0
0.0
10.0

These will result in errors

  • float(18+7j) TypeError: can’t convert complex to float
  • float(“twenty”) ValueError: could not convert string to float: ‘twenty’
  • float(“0B0001”) ValueError: could not convert string to float: ‘0B0001’

We can convert any type of value to float type except a complex type.

complex() :

We can use the complex() function to convert other types to a complex type.

print(complex(3))
print(complex(3.14))
print(complex(True)) 
print(complex(False)) 
print(complex("3"))
print(complex("3.5"))
print(complex("three"))

run this and you should see the following output

(3+0j)
(3.14+0j)
(1+0j)
0j
(3+0j)
(3.5+0j)
Traceback (most recent call last):
File "type1.py", line 9, in <module>
print(complex("three"))
ValueError: complex() arg is a malformed string

You can also use this like this complex (x,y)
x will be the real part and y will be the imaginary part

#!/usr/bin/env python

print(complex(3,1))
print(complex(True,False))

You should see this

(3+1j)
(1+0j)

bool() :

We can use this function to convert other type values to a bool type

print(bool(0))
print(bool(1))
print(bool(10))
print(bool(10.5))
print(bool(0.1)) 
print(bool(0.0))
print(bool(10-1j))
print(bool(0+0j))
print(bool("True"))
print(bool("False"))
print(bool(""))

Run this and you will see the following output

False
True
True
True
True
False
True
False
True
True
False

Here are the general rules for the bool() function

Int datatypes

0 means false
Non–zero means true

float datatypes

If the total number value is zero then the result is false otherwise the result is true

Complex datatypes

If both real and imaginary parts are zero then the result is False otherwise the result is True

str datatype

An empty string the result is False otherwise the result is True

 

str() :

We can use this method to convert other type values to a str type

#!/usr/bin/env python

print(str(10))
print(str(10.5))
print(str(10-1j))
print(str("True"))

Run this and you should see the following

10
10.5
(10-1j)
True

 

You may also like