Python all function example

Python all() function

In this python built-in functions tutorial, we are going to learn all about Python all() function along with various examples. This is the best built-in function to check an iterable contains valid items or not.

If the iterable contains valid items, it returns True otherwise returns False.

In the previous Python tutorial, we have seen all about the Python abs function to find the absolute value of a given number.

What is Python all() function?

Python all() function is a built-in function.all() function return True if any all items in an iterable are true otherwise return False.

Syntax

The syntax of all function in python is:-

Parameter

all function in Python accepts only one parameter that is iterable.

  • iterable:– any iterable object ( list, tuple, dictionary, etc) which contains the items.

Return Value

Python all function returns:-

  • True:- If all the elements are iterable are True.
  • False:- If any element in an iterable is False.

Python all function example

Here we will take various examples to understand Python all function.

Example 1:

Check if any item in a tuple is True:

x = (True, 1, True) x = all(x) print(x)

Output will be:- True

Example 2:

Check if any item in a set is True:

x = (True, 1, True) x = all(x) print(x)

Output will be:- False

Example 3:

Check if any item in a dictionary is True:

Output will be:- True

Note:- When we use all() function with a dictionary, all function checks only keys of the dictionary not values.

Conclusion

In this tutorial, you have learned all about Python all() method to check all the items in iterable are true or not. all() function return only boolean value, True or False.

Читайте также:  Javascript запретить все скрипты

If this tutorial helped you, please keep visiting for further python built-in functions tutorials.

For more information:- Click Here

Источник

Python all() Function

The all() function returns True if all items in an iterable are true, otherwise it returns False.

If the iterable object is empty, the all() function also returns True.

Syntax

Parameter Values

More Examples

Example

Check if all items in a list are True:

Example

Check if all items in a tuple are True:

Example

Check if all items in a set are True:

Example

Check if all items in a dictionary are True:

Note: When used on a dictionary, the all() function checks if all the keys are true, not the values.

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Python: all() function

The all() function is used to test whether all elements of an iterable are true or not.

Name Description Required/ Optional
iterable A container is considered to be iterble, which can return its members sequentially. Required

Return value:

Return True if all elements of the iterable are true.

Python: all() example with Lists

# all values true num = [23, 45, 10, 30] print(all(num)) #True # all values false num = [0, False] print(all(num)) #False # one false value num = [1, 3, 4, 0] print(all(num)) #False # one true value num = [0, False, 5] print(all(num)) #False # empty iterable num = [] print(all(num)) #True 
False False False False True

Pictorial Presentation:

Python: Built-in-function - all() function

Python: all() example with Dictionaries

dict = print(all(dict)) #False dict = print(all(dict)) #True dict = print(all(dict)) #False dict = <> print(all(dict)) #True # 0 is False # '0' is True dict = print(all(dict)) #True 
False True False True True

Python: all() example with Tuple

#Test if all items in a tuple are True: tup = (0, True, False) x = all(tup) print(x) #False tup = (1, True, True) x = all(tup) print(x) #True 

Python: all() example with Set

#Test if all items in a set are True: sss = x = all(sss) print(x) #False sss = (1, True, True) x = all(sss) print(x) #True 
False True False True True

Example: Python all() function with Strings

str = "David is a good boy" print(all(str)) #True # 0 is False # '0' is True str = '000' print(all(str)) #True str = '' print(all(str)) #True 

Pictorial Presentation:

Читайте также:  Css media query breakpoint

Python: Built-in-function - all() function

Python Code Editor:

Previous: abs()
Next: any()

Test your Python skills with w3resource’s quiz

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Converting string into datetime:

datetime.strptime is the main routine for parsing strings into datetimes. It can handle all sorts of formats, with the format determined by a format string you give it:

from datetime import datetime datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')

The resulting datetime object is timezone-naive.

  • Python documentation for strptime: Python 2, Python 3
  • Python documentation for strptime/strftime format strings: Python 2, Python 3
  • strftime.org is also a really nice reference for strftime
  • strptime = «string parse time»
  • strftime = «string format time»
  • Pronounce it out loud today & you won’t have to search for it again in 6 months.
  • Weekly Trends
  • Java Basic Programming Exercises
  • SQL Subqueries
  • Adventureworks Database Exercises
  • C# Sharp Basic Exercises
  • SQL COUNT() with distinct
  • JavaScript String Exercises
  • JavaScript HTML Form Validation
  • Java Collection Exercises
  • SQL COUNT() function
  • SQL Inner Join
  • JavaScript functions Exercises
  • Python Tutorial
  • Python Array Exercises
  • SQL Cross Join
  • C# Sharp Array Exercises

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

Источник

Python all

Summary: in this tutorial, you will learn how to use the Python all() function to check if all elements of an iterable are true.

Introduction to the Python all() function

The Python all() function accepts an iterable and returns True if all elements of the iterable are True . It also returns True if the iterable is empty.

Читайте также:  Get all request headers java

Here’s the syntax of the all() function:

all(iterable)Code language: Python (python)

The following example illustrates all() functions:

mask = [True, True, False] result = all(mask) print(result) # 👉 False mask = [True, True, True] result = all(mask) print(result) # 👉 True result = all([]) print(result) # 👉 TrueCode language: Python (python)
  • First, [True, True, False] has an element with the value False , the all() function returns False .
  • Second, [True, True, True] has all elements with value True , the all() function returns True .
  • Third, [] is an empty iterable, therefore, the all() function also returns True .

Practical examples of the all() function

Let’s take some practical examples of using the all() function.

1) Using Python all() function to make a complex condition more simple

The following example checks if the length of v is greater than zero and less than 25 and if it contains only alphanumeric characters:

v = 'Python' if len(v) > 0 and len(v) < 25 and v.isalnum(): print(v)Code language: Python (python)

The condition is quite complex. To make it shorter, you can replace all the and operators with the all() function like this:

v = 'Python' valid = all([len(v) > 0, len(v) < 25, v.isalnum()]) if valid: print(v)Code language: Python (python)

In this example, The valid evaluates to True if all the conditions inside the tuple passed to the all() the function returns True .

2) Using Python all() function to validate iterables of numbers

The following example uses the all() function to check if all numbers of an iterable are greater than or equal to four:

ratings = [3, 5, 4, 2, 4, 5] has_good_rating = all([rating >= 4 for rating in ratings]) print(has_good_rating) # falseCode language: Python (python)

First, use a list comprehension to convert the list of ratings to a list of True and False . The following

[rating >= 4 for rating in ratings]Code language: Python (python)

returns a list of boolean values:

[False, True, True, False, True, True]Code language: Python (python)

Second, pass the result of the list comprehension to all() function. So all() function returns False because the list contains some False elements.

Summary

Источник

Оцените статью