• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Convert True to 1 in Python

Convert True to 1 in Python

July 6, 2022 Leave a Comment

To convert the boolean value True to 1 in Python, you can use int(). int() converts True to 1 explicitly.

print(int(True))

#Output:
1

You can also convert True to 1 implicitly in the following way.

print(True == 1)

#Output:
True

When working with different variable types in Python, the ability to easily convert between those types can be valuable.

One such situation is if you have boolean variables and want to convert them to integers.

In Python, you can use the int() function to explicitly convert boolean values True and False into 1 and 0, respectively.

Below shows you how you can use int() to convert True to 1 explicitly.

print(int(True))

#Output:
1

If want to implicitly convert True to 1, you can let Python do it for you as True is equal to 1.

print(True == 1)

#Output:
True

Convert False to 0 in Python

If you want to go the other way and convert False into an integer, you can use int().

False is equal to 0, but if you want to do the conversion explicitly, then you can use int() in the following way.

Below shows you how to convert False to 0 in Python.

print(int(False))

#Output:
1

Hopefully this article has been useful for you to learn how to convert True to 1 in Python.

Other Articles You'll Also Like:

  • 1.  Swap Two Values of Variables in Python
  • 2.  Python power function – Exponentiate Numbers with math.pow()
  • 3.  Reverse String with Recursion in Python
  • 4.  Selenium minimize_window() Function to Minimize Window in Python
  • 5.  Writing a Table Faster to Word Document Using python-docx
  • 6.  Perform Reverse Dictionary Lookup in Python
  • 7.  Get the Count of NaN in pandas Using Python
  • 8.  Using Python to Check if Number is Divisible by Another Number
  • 9.  How to Group and Aggregate By Multiple Columns in Pandas
  • 10.  Replace Values in Dictionary in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy