# Python Boolean Variables

5/5 - (1 vote)

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 True, if and only if both variables `A` and `B` are already True.
• The expression `A or B` is True, if and only if at least one variable is already True.
• The expression `not A` is True, if and only if `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` is True if `A` is False.
• You can deduct only true statements from a true premise. Thus, `A -> B` is True if `B` is True.

The result of our nested call of the `follows` function is True: `(A -> B) -> C` is True as all three variables are True. After passing the if condition, we state that climate change is indeed happening.

## Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!