How to Convert Bool to Int in Python

In this Python tutorial, I will demonstrate how to convert bool to int in Python.

For my data analysis project, I used the music website dataset, which I needed to analyze to extract insight from it. A column in the dataset named premium represented whether the user was a premium member. The column contains only two values: True or False.

After analysis, I had to prepare the dataset for machine learning algorithms; as you know, the premium column must be numerical so the algorithm can understand it and work on it.

To convert the values in the premium column, I applied the int() function, which I explained in this tutorial, and how I used this function to convert a boolean to an integer value.

Let’s begin,

Python Bool to Int

The Boolean or Bool values can be True or False. However, computers look at these two values numerically behind the scenes, meaning True equals 1 and False equals 0.

Sometimes, according to requirement, you must work on these two numerical values instead of a string like True or False. There must be a way to convert the boolean True or False value into numerical values.

You can do that in Python using the built-in function int(), which converts the given value into numerical form.

Let’s see different examples that involve converting a boolean to an integer.

Python Boolean to Int Using int() Function

So, first, I will show you how this int() functions convert the given value into numeric form, and then I will discuss boolean things.

READ:  Python Tkinter OptionMenu [Complete Tutorial)

For example, you have a number in string form like ’45’; to convert this into numeric, pass this string value to the int() function as shown in the code below.

str_to_int = int('45')
print(str_to_int)

The output returns the integer value 45; execute the code below to verify its data type.

print(type(str_to_int))
Understanding the Working of Int() Function Before Converting Boolean to Int

The data type is ‘int’. It can convert strings containing numeric values into integers but doesn’t work on pure string values like sentences. It can also convert float or decimal values into integers.

Now that you know how int() works let’s convert a boolean value into an integer.

For example, you have a list named user_premium containing True or False values in sequence, and the sequence order represents whether the user is a premium member, as shown below.

user_premium = [True, False, True, True, False]

Let’s say that you want to convert this list of boolean values into numerical for data analysis. Then, you can use the int() function, as shown in the code below.

user_numeric_premium = []
for user in user_premium:
     user_numeric_premium.append(int(user))

print(user_numeric_premium)
Python Boolean to Int Using int() Function

Look at the output; the int() function converted each boolean value in the list user_premium into their int representation. Thus, it returns ‘[1, 0, 1, 1, 0]’.

Let’s understand the code parts. The list of user_premium contains boolean values. The following list, user_numeric_premium, is an empty list that stores the int of the boolean.

  • Next, using this line of code ‘for the user in user_premium:’ iterating over the list of values and accessing each boolean value represented by ‘user’.
  • After that, the following line of code, ‘user_numeric_premium.append(int(user))’, takes each boolean value and converts it into an integer using the int(user) function. Each converted boolean is appended to the list ‘user_numeric_premium’ using the append (int(user)) function.
READ:  How to Create Date Time Picker using Python Tkinter

This is how you can boolean to int in Python using the int() function.

Conclusion

In this Python tutorial, you learned how to convert bool to int in Python using the Python built-in function int().

You also learned how the int() function works and what kind of value it can convert into integers.

You may like to read: