CodeBlessU
learn code with examples

Python Data Type Conversion

bool to

bool to int

Use int() function to convert bool to int. False is converted to 0, True is converted to 1.

To create int from bool, same function - int() is used.

# Example, Convert bool to int, Create int from bool
a1 = False
int(a1) # returns 0

a1 = True
int(a1) # returns 1

bool to float

Use float() function to convert bool to float. False is converted to 0.0, True is converted to 1.0.

To create float from bool, same function - float() is used.

# Example, Convert bool to float, Create float from bool
a1 = False
float(a1) # returns 0.0

a1 = True
float(a1) # returns 1.0

bool to complex

Use complex() function to convert bool to complex. False is converted to 0+0j (or 0j), True is converted to 1+0j.

To create complex from bool, same function - complex() is used.

# Example, Convert bool to complex, Create complex from bool
a1 = False
complex(a1) # returns 0j

a1 = True
complex(a1) # returns 1+0j

bool to string

Use str() function to convert bool to string. False is converted to string "False", True is converted to string "True".

To create string from bool, same function - str() is used.

# Example, Convert bool to string, Create string from bool
a1 = False
str(a1) # returns 'False'

a1 = True
str(a1) # returns 'True'

int to

int to bool

Use bool() function to convert int to bool. 0 is False, any other int value is converted to True.

# Example, convert int to bool, create bool from int
a1 = 1
bool(a1) # returns True

a1 = -10000
bool(a1) # returns True

a1 = 0
bool(a1) # returns False

int to float

Use float() function to convert int to float.

# Example, Convert int to float, Create float from int
a1 = 1
a2 = float(a1) # assigns 1.0
print("Type: " + str( type(a2) ) + "      Value: " + str(a2) )

a1 = 0
a2 = float(a1) # assigns 0.0
print("Type: " + str( type(a2) ) + "      Value: " + str(a2) )

a1 = -5000
a2 = float(a1) # assigns -5000.0
print("Type: " + str( type(a2) ) + "      Value: " + str(a2) )

a1 = 10**31
a2 = float(a1) # assigns 1e+31
print("Type: " + str( type(a2) ) + "      Value: " + str(a2) )


int to complex

Use complex() function to convert int to complex. Int value gets assigned to real part. Imaginary part is marked as 0j.

# Example, Convert int to complex, Create complex from int
a1 = 1
a2 = complex(a1) # assigns (1+0j)
print("Type: " + str( type(a2) ) + "      Value: " + str(a2) )

a1 = 0
a2 = complex(a1) # assigns 0j
print("Type: " + str( type(a2) ) + "      Value: " + str(a2) )

a1 = -5000
a2 = complex(a1) # assigns (-5000+0j)
print("Type: " + str( type(a2) ) + "      Value: " + str(a2) )

a1 = 10**31
a2 = complex(a1) # assigns (1e+31+0j)
print("Type: " + str( type(a2) ) + "      Value: " + str(a2) )

int to string

Use str() function to convert int to string.

# Example, Convert int to string, Create string from int
a1 = 1
a2 = str(a1) # assigns '1'
print("Type: " + str( type(a2) ) + "      Value: " + a2 )

a1 = 0
a2 = str(a1) # assigns '0'
print("Type: " + str( type(a2) ) + "      Value: " + a2 )

a1 = -5000
a2 = str(a1) # assigns '-5000'
print("Type: " + str( type(a2) ) + "      Value: " + a2 )

a1 = 10**31
a2 = str(a1) # assigns '10000000000000000000000000000000'
print("Type: " + str( type(a2) ) + "      Value: " + a2 )

int to binary, int to octal, int to hex

This is same as decimal to binary, decimal to octal, decimal to hex.

decimal to

Say, your decimal number is available as int.


decimal to binary

Use bin() function to convert decimal to binary. Return type: class 'str'.

a1 = 255

# Example, convert decimal to binary
binary_of_a1 = bin(a1) # assigns '0b11111111'
type(binary_of_a1)
print(binary_of_a1)

decimal to octal

Use oct() function to convert decimal to octal. Return type: class 'str'.

a1 = 255

# Example, convert decimal to octal
octal_of_a1 = oct(a1) # assigns '0o377'
type(octal_of_a1)
print(octal_of_a1)

decimal to hex

Use hex() function to convert decimal to hex. Return type: class 'str'.

a1 = 255

# Example, convert decimal to hex
hex_of_a1 = hex(a1) # assigns '0xff'
type(octal_of_a1)
print(octal_of_a1)

binary to

How your binary number is available?

  • As binary literal? Then, its type is 'int' and it will be visible as decimal. Pls refer section Decimal to.
  • As string containing 0 and 1 (with 0b or without 0b)?, pls continue below.
  • As decimal number containing 0 and 1 (i.e. number 11111111)?, pls refer section Binary value specified as decimal.

binary to decimal

Use int(x, base) function to convert it to decimal. Specify string with binary value as x and base value 2. Type: class 'int'.

a1 = "0b11111111"

# Example, convert binary string to decimal
decimal_of_a1 = int(a1, 2)
type(decimal_of_a1)
print(decimal_of_a1)

binary to octal

It is two step conversion.

  1. Convert string to int, using int(x, base).
  2. Convert int to octal using oct() function. Type: class 'str'.
a1 = "0b11111111"

# Example, convert binary string to octal
decimal_of_a1 = int(a1, 2)
octal_of_a1 = oct(decimal_of_a1)
type(octal_of_a1)
print(octal_of_a1)

binary to hex

It is two step conversion.

  1. Convert string to int, using int(x, base).
  2. Convert int to hex using hex() function. Type: class 'str'.
a1 = "0b11111111"

# Example, convert binary string to octal
decimal_of_a1 = int(a1, 2)
hex_of_a1 = hex(decimal_of_a1)
type(hex_of_a1)
print(hex_of_a1)

Binary value specified as decimal

Use str() function to convert it to string. So that above approach can be continued.

a1 = 11111111
a1 = str(a1) # now its "11111111"

octal to

How your octal number is available?

  • As octal literal? Then, its type is 'int' and it will be visible as decimal. Pls refer section Decimal to.
  • As string containing 0 to 7 (with 0o or without 0o)?, pls continue below.
  • As decimal number containing 0 to 7 (i.e. number 377)?, pls refer section Octal value specified as decimal.

octal to decimal

Use int(x, base) function to convert it to decimal. Specify string with octal value as x and base value 8. Type: class 'int'.

a1 = "0o377"

# Example, convert octal string to decimal
decimal_of_a1 = int(a1, 8)
type(decimal_of_a1)
print(decimal_of_a1)

octal to binary

It is two step conversion.

  1. Convert string to int, using int(x, base).
  2. Convert int to binary using bin() function. Type: class 'str'.
a1 = "0o377"

# Example, convert octal string to binary
decimal_of_a1 = int(a1, 8)
binary_of_a1 = bin(decimal_of_a1)
type(binary_of_a1)
print(binary_of_a1)

octal to hex

It is two step conversion.

  1. Convert string to int, using int(x, base).
  2. Convert int to hex using hex() function. Type: class 'str'.
a1 = "0o377"

# Example, convert octal string to hex
decimal_of_a1 = int(a1, 8)
hex_of_a1 = hex(decimal_of_a1)
type(hex_of_a1)
print(hex_of_a1)

Octal value specified as decimal

Use str() function to convert it to string. So that above approach can be continued.

a1 = 377
a1 = str(a1) # now its "377"

hex to

How your hex number is available?

  • As hex literal? Then, its type is 'int' and it will be visible as decimal. Pls refer section Decimal to.
  • As string containing 0 to 9, a to f (with 0x or without 0x)?, pls continue below.

hex to decimal

Use int(x, base) function to convert it to decimal. Specify string with hex value as x and base value 16. Type: class 'int'.

a1 = "0xff"

# Example, convert hex string to decimal
decimal_of_a1 = int(a1, 16)
type(decimal_of_a1)
print(decimal_of_a1)

hex to binary

It is two step conversion.

  1. Convert string to int, using int(x, base).
  2. Convert int to binary using bin() function. Type: class 'str'.
a1 = "0xff"

# Example, convert hex string to binary
decimal_of_a1 = int(a1, 16)
binary_of_a1 = bin(decimal_of_a1)
type(binary_of_a1)
print(binary_of_a1)

hex to octal

It is two step conversion.

  1. Convert string to int, using int(x, base).
  2. Convert int to octal using oct() function. Type: class 'str'.
a1 = "0xff"

# Example, convert hex string to octal
decimal_of_a1 = int(a1, 16)
octal_of_a1 = oct(decimal_of_a1)
type(octal_of_a1)
print(octal_of_a1)

string to

string to bool

Use bool() function to convert string to bool. Only empty string is converted to False, any other case is True. Even string "False" or string "0" is converted to boolean True!

To create bool from string, same function - bool() is used.

# Example, convert string to bool, create bool from str
a1 = ""
bool(a1) # returns False

a1 = "False" 
bool(a1) # returns True

a1 = "0"
bool(a1) # returns True

Create bool from

Python int to bool

  • Use bool() function to convert int to bool.
  • 0 is False, any other int value is True.
# Example, convert int to bool, create bool from int
bool(1) # returns True
bool(-10000) # returns True
bool(0) # returns False

Python float to bool

  • Use bool() function to convert float to bool.
  • 0.0 is False, any other float value is True.
# Example, convert float to bool, create bool from float
bool(1.1) # returns True
bool(-10000.005) # returns True
bool(0.0) # returns False
bool(0.000000000000000000000001) # returns True

Python complex to bool

  • Use bool() function to convert complex to bool.
  • 0+0j (or 0j) is False, any other complex value is True.
# Example, convert complex to bool, create bool from complex
bool(1+1j) # returns True
bool(1+0j) # returns True
bool(0+1j) # returns True
bool(0+0j) # returns False
bool(0j) # returns False
bool(0.0001+120j) # returns True

Python list to bool

  • Use bool() function to convert list to bool.
  • Empty list is False, any other case is True.
# Example, convert list to bool, create bool from list
a1 = []
bool(a1) # returns False

a1 = [1,2,3]
bool(a1) # returns True

a1 = [0]
bool(a1) # returns True

a1 = [""]
bool(a1) # returns True

Python tuple to bool

  • Use bool() function to convert tuple to bool.
  • Empty tuple is False, any other case is True.
# Example, convert tuple to bool, create bool from tuple
a1 = tuple()
bool(a1) # returns False

a1 = 1,2,3
bool(a1) # returns True

a1 = (0,)
bool(a1) # returns True

a1 = ("")
bool(a1) # returns True

Similar cases

  • Similarly, set, dictionary, array can be converted to bool
  • Empty set/dictionary/array is False, other case is True.
# Example, convert set/dictionary/array to bool
a1 = set()
bool(a1) # returns False

a1 = set( [0, "Hi"] )
bool(a1) # returns False

a1 = dict()
bool(a1) # returns True

a1 = {0: "Zero", 1: "One"}
bool(a1) # returns True

import array
a1 = array.array('f') 
bool(a1) # returns False

a1 = array.array('i', (0,1) ) 
bool(a1) # returns True

In this page...

Copyright © All rights reserved.