Truthiness and Falseness in Python (With Examples)

truthiness-and-falseness-in-python-with-examples

In the previous tutorial, we learn about the Loop Control Statements in python. So, now we are going to discuss truthiness and falseness in python.

In Python, truthiness and falseness refer to the boolean interpretation of an object or value.

A boolean is a type of data that can only have two values: True or False.

Truthiness refers to a value that is considered True in a boolean context, while falseness refers to a value that is considered False.

Python has a set of rules for determining the truthiness or falseness of a value. In general, the following values are considered False:

  • None
  • False
  • Zero of any numeric type (0, 0.0, 0j)
  • Empty sequences (e.g., [], ”, ())
  • Empty mappings (e.g., {})

All other values are considered True. For example, the following values are considered True:

  • Non-zero numbers (e.g., 1, 2.5, -3)
  • Non-empty sequences (e.g., [1, 2], ‘hello’, (1,))
  • Non-empty mappings (e.g., {‘a’: 1}, {1: ‘a’})

Here are some examples of truthiness and falseness in Python:

Checking if a list is empty:

Here we are going to check if the following list is empty by using all the rules of falseness.

First, we are going to use “None”.

Code:

fruits = None
if fruits:
    print("The list is not empty.")
else:
    print("The list is empty.")

Next, we are going to use “False”.

fruits = False
if fruits:
    print("The list is not empty.")
else:
    print("The list is empty.")

Now we are going to use Zero of any numeric type (0, 0.0, 0j)

fruits = 0
if fruits:
    print("The list is not empty.")
else:
    print("The list is empty.")

Here we can use Empty sequences (e.g., [], ())

fruits = [] #you can also use this one ()
if fruits:
    print("The list is not empty.")
else:
    print("The list is empty.")

we can also use Empty mappings (e.g., {})

fruits = {}
if fruits:
    print("The list is not empty.")
else:
    print("The list is empty.")

Output:

The list is empty.

The output will remain the same for all the codes.

in the above examples, the if statement checks if the list fruits is truthy or false.

Since it is empty, it evaluates to False, so the code inside the else block is executed.

Now we are going to check if the following list is not empty by using all the rules of truthiness.

Code:

fruits = [1] #you can enter any number and also use Non-zero numbers (e.g., 1, 2.5, -3), Non-empty sequences (e.g., [1, 2], 'hello', (1,)) and Non-empty mappings (e.g., {'a': 1}, {1: 'a'})
if fruits:
    print("The list is not empty.")
else:
    print("The list is empty.")

Output:

The list is not empty.

Understanding truthiness and falseness is important when writing conditional statements in Python. It allows you to write more concise and readable code.

Instead of explicitly checking if a value is equal to True or False, you can simply use the value itself in a boolean context. For example:

Code:

# Checking truthiness explicitly
x = 5
if x == 5:
    print("x is equal to 5.")

# Checking truthiness implicitly
x = 5
if x:
    print("x is truthy.")

Output:

x is equal to 5.
x is truthy.

In the above examples, both if statements will execute the code inside the block, but the second example is more concise and idiomatic.

So, in the next tutorial, we are going to learn about Iteration over Sequences and Collections (With Examples). So, make sure to subscribe to us to learn python in an easy way.


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading