Method # 1: Using the int () method
# Python code for demonstration # to convert a boolean value to an integer # Initializing values bool_val = True # Print initial values print ( "Initial value" , bool_val) # Boolean to integer conversion bool_val = int (bool_val = = True ) # Print result print ( "Resultant value" , bool_val) |
Exit:
Initial value True Resultant value 1
Method # 2: Taking a Naive Approach
# Python code for demo # to convert boolean # value to integer # Initializing values bool_val = True # Print initial values print ( "Initial value" , bool_val) # Convert a boolean number to integer if bool_val: bool_val = 1 else : bool_val = 0 # Print result print ( "Resultant value " , bool_val) |
Exit :
Initial value True Resultant value 1
Method # 3: Using NumPy
In case there is a boolean list
# Python code for demonstration # to convert boolean # value to integer import numpy # Initializing values bool_val = numpy.array ([ True , False ]) # Print initial values print ( "Initial values" , bool_val) # Convert boolean to integer bool_val = numpy.multiply (bool_val, 1 ) # Print result print ( "Resultant values" , str (bool_val)) |
Exit:
Initial values [True False] Resultant values [1 0]
Method # 4: Using the card ()
in the case of a logical list
# Python code to demonstrate # to convert boolean # value to integer # Initializing values bool_val = [ True , False ] # Print initial values print ( "Initial value" , bool_val) # Boolean to integer conversion bool_val = list ( map ( int , bool_val)) # Print result print ( " Resultant value " , str (bool_val)) |
Exit:
Initial value [True, False] Resultant value [1, 0]