Python | Ways to convert Boolean values to integer
Given a boolean value(s), write a Python program to convert them into an integer value or list respectively. Given below are a few methods to solve the above task.
Method #1: Using int() method
# Python code to demonstrate # to convert boolean value to integer # Initialsing Values bool_val = True # Printing initial Values print("Initial value", bool_val) # Converting boolean to integer bool_val = int(bool_val == True) # Printing result print("Resultant value", bool_val) |
chevron_right
filter_none
Output:
Initial value True Resultant value 1
Method #2: Using Naive Approach
# Python code to demonstrate # to convert boolean # value to integer # Initialsing Values bool_val = True # Printing initial Values print("Initial value", bool_val) # Converting boolean to integer if bool_val: bool_val = 1else: bool_val = 0# Printing result print("Resultant value", bool_val) |
chevron_right
filter_none
Output:
Initial value True Resultant value 1
Method #3: Using numpy
In case where boolean list is present
# Python code to demonstrate # to convert boolean # value to integer import numpy # Initialsing Values bool_val = numpy.array([True, False]) # Printing initial Values print("Initial values", bool_val) # Converting boolean to integer bool_val = numpy.multiply(bool_val, 1) # Printing result print("Resultant values", str(bool_val)) |
chevron_right
filter_none
Output:
Initial values [ True False] Resultant values [1 0]
Method #4: Using map()
in case where boolean list is presen
# Python code to demonstrate # to convert boolean # value to integer # Initialsing Values bool_val = [True, False] # Printing initial Values print("Initial value", bool_val) # Converting boolean to integer bool_val = list(map(int, bool_val)) # Printing result print("Resultant value", str(bool_val)) |
chevron_right
filter_none
Output:
Initial value [True, False] Resultant value [1, 0]
Recommended Posts:
- Python | Ways to concatenate boolean to string
- Python program to fetch the indices of true values in a Boolean list
- Python | Ways to sort a zipped list by values
- Python | Ways to sort list of float values
- Ways to sort list of dictionaries by values in Python – Using itemgetter
- Python | Ways to convert hex into binary
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Python | Convert a list of multiple integers into a single integer
- Python | Ways to convert string to json object
- Python | Ways to convert list of ASCII value to string
- Python | Convert list of string into sorted list of integer
- Python | Ways to convert array of strings to array of floats
- Python | Ways to Convert a 3D list into a 2D list
- Python | Visualize missing values (NaN) values using Missingno Library
- Python | Ways to split a string in different ways
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



