Learn Python: Booleans & Comparisons
Tags: python, comparisons, tutorial, beginners
Published: 2020-05-02 14:32:02

---
title: Learn Python: Booleans & Comparisons
published: true
description: Introduction to booleans & comparisons in Python
tags: python, comparisons, tutorial, beginners
series: [Learn Python]
cover_image: https://images.pexels.com/photos/4050296/pexels-photo-4050296.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940
---

# Simply Booleans

Checking for truthy or falsy. 

| Description | Operator |
|---| --- |
| Truthy | `True` |
| Falsy  | `False` |
| Greater than | `>` |
| Less than | `<` |
| Greater or equal to | `>=` |
| Less or equal to | `<=` |
| Equals to | `==` |
| Assignment | `=` |

```python
truthy = True
falsy = False
print(truthy);
print(falsy);

LEGAL_AGE = 18
age = int(input("Enter your age:"));
is_over_legal_age = age >= LEGAL_AGE;
print(f"User is OVER legal age: {is_over_legal_age}")

is_under_legal_age = age < LEGAL_AGE;
print(f"User is UNDER legal age: {is_under_legal_age}")

is_twenty = age == 20;
print(f"User is 20 years old: {is_twenty}")

is_eighteen = age == 18;
print(f"User is 18 years old: {is_eighteen}")
```
![](https://i.imgur.com/mcDVsNB.png)

# `bool()`
`bool()` is a function which takes in a parameter and evaluates it to either `True` or `False`.

```python
print(f"Integer 0 is evaluated to: {bool(0)}")
print(f"An empty character '' is evaluated to: {bool('')}")
print(f"Positve number 1 is evaluated to : {bool(1)}")
print(f"Negative -1 is evaluated to: {bool(-1)}")
print(f"A string 'abc' is evaluated to : {bool('abc')}")
```
![](https://i.imgur.com/UiAQss7.png)


# `and`

`and` gives you the first value if it is `False`, otherwise, it gives you the second value.

```python
print(f"True and True: { True and True}");
print(f"False and False: { False and False}");
print(f"True and False: { True and False}");
print(f"False and True: { False and True}");
```
![](https://i.imgur.com/IaZPrLY.png )

Let's compare some numbers and some strings.

```python
print(f"True and True: { bool(10) and bool(10)}");
print(f"False and False: { bool(0) and bool(10)}");
print(f"True and False: { bool(10) and bool(0)}");
print(f"False and True: { bool(0) and bool(10)}");
print()
print(f"10  and  10: { 10 and 10}");
print(f"0  and   0:  { 0 and 0}");
print(f"10  and   0:  { 10 and 0}");
print(f"0  and  10:  { 0 and 10}");
print()
print(f"'abc'  and  'xyz': { 'abc' and 'xyz'}");
print(f"''  and  '': { '' and 'xyz'}");
print(f"'abc'  and  '': { 'abc' and ''}");
print(f"''  and  'xyz': { '' and 'xyz'}");
```
![](https://i.imgur.com/gvlWNoh.png)


# `or`
`or` gives you the first value if it is `True`, otherwise it gives you the second value.

```python
print(f"True or True: { True or True}");
print(f"False or False: { False or False}");
print(f"True or False: { True or False}");
print(f"False or True: { False or True}");
 
```
![](https://i.imgur.com/XTYKcUW.png)

Let's compare some numbers and some strings.

```python
print(f"True or True: { bool(10) or bool(10)}");
print(f"False or False: { bool(0) or bool(10)}");
print(f"True or False: { bool(10) or bool(0)}");
print(f"False or True: { bool(0) or bool(10)}");
print()
print(f"10  or  10: { 10 or 10}");
print(f"0  or   0:  { 0 or 0}");
print(f"10  or   0:  { 10 or 0}");
print(f"0  or  10:  { 0 or 10}");
print()
print(f"'abc'  or  'xyz': { 'abc' or 'xyz'}");
print(f"''  or  '': { '' or 'xyz'}");
print(f"'abc'  or  '': { 'abc' or ''}");
print(f"''  or  'xyz': { '' or 'xyz'}");
```
![](https://i.imgur.com/7YGRavV.png)

# `not`
`not` negates any boolean value. 
`not` `True` is evaluated to `False`.
`not` `False`  is evaluated to `True`.
```python
print(f"True: { True}" );
print(f"not True: {not True}" );
print()
print(f"False: { False}" );
print(f"not False: {not False}" );
print()
print(f"35: { 35}" );
print(f"not 35: {not 35}" );
print()
print(f"'': { ''}" );
print(f"not '': {not ''}" );
print()
print(f"'abc': { 'abc'}" );
print(f"not 'abc': {not 'abc'}" );
```
![](https://i.imgur.com/LLUMuuP.png)

<hr>
{% replit @rishiabee/learnpycomparisons %}