**Python Boolean variables** are set to either the `True`

or `False`

keywords. Both keywords require an ** Upper Case** letter—lowercase letters are interpreted as normal variable or function names, so you could set

`true = True`

. A variable is of type Boolean if `type(variable)`

results in the output `<class 'bool'>`

. You can convert any object to a Boolean by using Python’s built-in function `bool()`

. >>> variable = True >>> type(variable) <class 'bool'>

## How to Convert an Integer Variable to a Boolean Variable?

You can convert an integer to a Boolean by using Python’s built-in function `bool()`

any non-zero integer will result in a Boolean value of `True`

. The only integer value that results in the Boolean `False`

is the value 0.

>>> integer = 42 >>> boolean = bool(integer) >>> boolean True >>> bool(0) False

## How to Convert a Float Variable to a Boolean Variable?

You can convert a float to a Boolean by using Python’s built-in function `bool()`

any non-zero float will result in a Boolean value of `True`

. The only float value that results in the Boolean `False`

is the value 0.0.

>>> integer = 42.0 >>> boolean = bool(integer) >>> boolean True >>> bool(0.0) False

## Boolean Logics Basics

Boolean logic is crucial for coding. Try finding any real-world code project that does not use boolean logic. You will most certainly fail. Hence, to become a code master, you must master boolean logic first.

This code shows the basic usage of ** boolean logic operators** in Python.

# Logic Statements A = True # Temperatures are high B = True # Arctic ice is melting C = True # Sea levels are rising D = False # All regions become hot def follows(A, B): """Returns True if 'B follows from A""" return not A or B # Do we have a climate change? climate_change = follows(follows(A, B), C) if not D: print(climate_change) else: print(D)

Before I show you the solution, take a guess—what’s the output of this code snippet?

We define four variables with various boolean assignments. Each boolean variable can be either `True`

or `False`

.

Consider two logical variables `A`

and `B`

. You have to understand three concepts.

- The expression
`A and B`

is, if and only if both variables**True**`A`

and`B`

are already.*True* - The expression
`A or B`

is, if and only if at least one variable is already*True*.*True* - The expression
`not A`

is, if and only if*True*`A`

is.*False*

In the code, we define our own function `follows`

that takes two arguments `A`

and `B`

and returns `A -> B`

.

In words: * ‘The consequence B follows from the premise A’*.

- You can deduct anything from a wrong premise. Thus,
`A -> B`

isif*True*`A`

is.*False* - You can deduct only true statements from a true premise. Thus,
`A -> B`

isif*True*`B`

is.*True*

The result of our nested call of the `follows`

function is ** True**:

`(A -> B) -> C`

is **as all three variables are**

*True***. After passing the if condition, we state that climate change is indeed happening.**

*True*## Related Boolean Video

## Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

**Practice projects is how you sharpen your saw in coding!**

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.