fbpx

The Ultimate Guide to Python’s Built-in Boolean Functions

Python Built in Functions

INTRODUCTION

Python, a versatile programming language, owes much of its power to its extensive set of built-in functions. These functions simplify coding tasks by providing ready-made solutions for common programming challenges. Among these are the abs(), all(), and any() functions, which cater to mathematical calculations, logical evaluations, and condition-based operations.

The abs() function calculates the absolute value of a number, stripping away any negative sign. Whether you’re working with integers, floating-point numbers, or complex numbers, abs() ensures you get a non-negative result effortlessly.

The all() function evaluates whether all elements in an iterable (like lists, tuples, sets, or dictionaries) meet a specific truth condition. It’s perfect for scenarios where you need a collective confirmation, such as verifying all inputs in a dataset.

  • Abs()(returns absolute value of a number):

 The absolute value is always non-negative, as it removes any negative sign from the number.

Example:                                                          

Input: -34

Output: 34

Program

      abs(-34)-à34

Output :

       34

Absolute of floating point number:                                                                   

        # floating point number

         float_number = -54.26                                                                              

          abs(-54.26)=54.26

         A complex number

        complex_number = (3 – 4j)

         abs(3-4j)=5                                                                                   

Python – all() function:

Syntax: all( iterable )

 

Returns: Boolean

Example

print(all([True, True, False]))-à  false

# All elements of list are true

m = [6, 3, 1]                                   

print(all(m))–àtrue

# All elements of list are false

m= [0, false, False]

print(all(m))-àfalse

# Some elements of list are

# true while others are false

m= [1, 0, 6, 7, False]

print(all(m))-àfalse                                           

# Empty List

m = []àtrue

print(all(m))

m = [1,-3,0,2,4]

print(all(ele > 0 for ele in m))

Output:

True

False

False

True

False

All function  with tuples

——————————-

# All elements of tuple are true

t = (2, 4, 6)

print(all(t))àtrue                                                                                       

 

# All elements of tuple are false

t = (0, False, False)

print(all(t))àfalse

 

# Some elements of tuple

# are true while others are false

t = (5, 0, 3, 1, False) àfalse

print(all(t))

 

# Empty tuple

t = (print(all(t))àtrue

 

All with set:

—————

# All elements of set are true

s = {1, 1, 3}

print(all(s))

 

# All elements of set are false

s = {0, 0, False}

print(all(s))

 

# Some elements of set

# are true while others are false

s = {1, 2, 0, 8, False}

print(all(s))

# Empty set

s = {}

print(all(s))

All function  with dictionaries:

If all the keys are present

—————————–

Dict1={1:”iies”,2:indian”,3:”institute”,4:”embedded”}

All(dict1);-àtrue

If all the keys are not present

———————————–

Dict1={0:”iies”,false:”institute”}

All(dict1);àfalse

If some of the keys are present and some of the keys are not present

———————————————————————————

Dict1={0:”iies”, 1:”indian”,2:”institute”,3:”embedded”,4:”systems”}-àfalse

Empty set

Dict1={ }-àtrue

All with condition:

———————–

Test_list=[1,2,3,4,5]

All((ele >10 for(ele in text_list)))-àtrue

Python any() :

Syntax: any(iterable)

Iterable: It refers to an object that can be iterated over, such as a dictionary, tuple, list, set, and similar types.

# a List of boolean values

m = [False, False, True, False, False]

print(any(m))à  true

Example

# All elements of list are True

m= [4, 5, 1]

print(any(m)) àtrue

# All elements of list are False

m= [0, 0, False]        

print(any(m))àfalse

# Some elements of list are

# True while others are False

# l = [1, 0, 6, 7, False]             

# print(any(m))àtrue

# Empty list

l = []à true

print(any(m))

output:

True

False

True

False

 

Any with sets

——————-

# All elements of set are True

s = { 1, 1, 3}

print(any(s))àtrue

# All elements of set are False

s = { 0, 0, False}

print(any(s))àfalse

 

# Some elements of set are True while others are False

s = { 1, 2, 0, 8, False}

print(any(s)) -àtrue

# Empty set

s = {}àfalse

print(any(s)) à

Output

——-

True

False

True

False

 

Any function with dictionaries

#if all the keys are present

dict={1:”people”,2:”arjun”,3:”iies”};

print(any(dict));àtrue

#if all the keys are false:

dict={0:”people”,false:”iies”}

print(any(dict));àfalse                              

Some keys of dictionary are true while others are false

Dict={0:”institute”,1:”iies”,2:”Indian” ,3:”Embedded”}->true

# Empty dictionary

d = {}

print(any(d))-àfalse

 

Any with strings:

#non empty string

string  a=”helloworld”;

any(a)à true

#non empty string

string a=”000”

Any(a)-àtrue

#Empty string

string  a= “ “

any(a)à false

any with conditional

——————————

text_list=[1,2,3,4,5];

any(ele>10 for( ele in text_list))  –false