Cualquier Todo en Python

Any y All son dos incorporados provistos en python que se usan para Y/O sucesivos.

Cualquiera
Devuelve verdadero si alguno de los elementos es verdadero. Devuelve False si está vacío o si todos son falsos. Cualquiera puede considerarse como una secuencia de operaciones OR en los iterables proporcionados.
Cortocircuita la ejecución, es decir, detiene la ejecución tan pronto como se conoce el resultado.

Sintaxis: cualquiera (lista de iterables)

# Since all are false, false is returned
print (any([False, False, False, False]))
  
# Here the method will short-circuit at the
# second item (True) and will return True.
print (any([False, True, False, False]))
  
# Here the method will short-circuit at the
# first (True) and will return True.
print (any([True, False, False, False]))

Producción :

False
True
True

Todo
Devuelve verdadero si todos los elementos son verdaderos (o si el iterable está vacío). Todo se puede considerar como una secuencia de operaciones AND en los iterables proporcionados. También cortocircuita la ejecución, es decir, detiene la ejecución tan pronto como se conoce el resultado.

Sintaxis: all (lista de iterables)

# Here all the iterables are True so all
# will return True and the same will be printed
print (all([True, True, True, True]))
  
# Here the method will short-circuit at the 
# first item (False) and will return False.
print (all([False, True, True, False]))
  
# This statement will return False, as no
# True is found in the iterables
print (all([False, False, False]))

Producción :

True
False
False

Ejemplos prácticos

# This code explains how can we 
# use 'any' function on list 
list1 = []
list2 = []
  
# Index ranges from 1 to 10 to multiply
for i in range(1,11):
    list1.append(4*i) 
  
# Index to access the list2 is from 0 to 9
for i in range(0,10):
    list2.append(list1[i]%5==0)
  
print('See whether at least one number is divisible by 5 in list 1=>')
print(any(list2))

Producción:

See whether at least one number is divisible by 5 in list 1=>
True
# Illustration of 'all' function in python 3
  
# Take two lists 
list1=[]
list2=[]
  
# All numbers in list1 are in form: 4*i-3
for i  in range(1,21):
    list1.append(4*i-3)
  
# list2 stores info of odd numbers in list1
for i in range(0,20):
    list2.append(list1[i]%2==1)
  
print('See whether all numbers in list1 are odd =>')
print(all(list2))

Producción:

See whether all numbers in list1 are odd =>
True

Mesa de la verdad :-

Este artículo es una contribución de Mayank Rawat . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.

Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *