Open In App

Truthy in Python

Last Updated : 18 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python Programming, every value is either evaluated as True or False. Any value that is evaluated as a True boolean type is considered Truthy. The bool() function is used to check the Truthy or Falsy of an object. We will check how Python evaluates the truthiness of a value within a conditional statement.

Python
a = True
if a:
  print("GeeksforGeeks")

Output
GeeksforGeeks

Explanation:

  • In this example, the variable a is set to True, hence it is a Truthy value.

Non-zero Numbers

Any number that isn't zero is considered truthy. This includes positive and negative integers and floating-point numbers.

Python
a = 10
print(bool(a))

b = 10.50
print(bool(b))

Output
True
True

Non-empty Sequences

A sequence in Python ( string, list, dictionary, tuple or set) is Truthy when the length of a sequence if greater than or equal to one. In other words the sequence should not be empty.

Python
# string
s = "GFG"
print(bool(s))

# list
l = [1, 2, 3]
print(bool(l))

# dictonary
d = {1: "Apple"}
print(bool(d))

# tuple
t = (1, 2, 3, 4, 1)
print(bool(t))

#set 
s = {1, 2, 3}  
print(bool(s))

Output
True
True
True
True
True

Constant Values

The boolean constant True, as the name suggests is a Truthy value in Python.

Python
a = True
print(bool(a))

Output
True

Next Article
Article Tags :
Practice Tags :

Similar Reads