Change language

Python | Ways to Convert Boolean Values ​​to Integer

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] `

Shop

Learn programming in R: courses

\$FREE

Best Python online courses for 2022

\$FREE

Best laptop for Fortnite

\$399+

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best computer for crypto mining

\$499+

Best laptop for Sims 4

\$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries