top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

any and all in python

Writer: aya abdalsalamaya abdalsalam
Any and or are two built in functions in python Both functions are equivalent to writing a series of or , and operators respectively between each of the elements of the passed iterable

The or operator

x = 6
y = 7 
z = 8
print((x >= 3 ) or (y >= 6 ) or (z > 7))
True 
print((x == 3 ) or (y >= 6 ) or (z == 7))
True 
print((x == 3 ) or (y == 6 ) or (z < 7))
False

As we see in the previous examples if any of the part of the code is True the all of the sentence is treated as true and if all the parts are false the all is treated as False so we can use another functions which can help us and make code small and not repeat any part of the code We use any and use iterable as list to loop over it


print(any([x >= 3, y >= 6, z > 7]))
True 
print(any([x == 3, y >= 6, z == 7]))
True 
print(any([x == 3, y == 6, z < 7]))
False

The and operator

x = 6
y = 7 
z = 8
print((x >= 3 ) and (y >= 6 ) and (z > 7))
True 
print((x == 3 ) and (y >= 6 ) and (z == 7))
False 
print((x == 3 ) and (y == 6 ) and (z < 7))
False

if all the parts are true the output is true

else it false

but there exist a function which like any make code small and easy called all

print(all([x >= 3, y >= 6, z > 7]))
True 
print(all([x == 3, y >= 6, z == 7]))
False 
print(all([x == 3, y == 6, z < 7]))
False

Note:

1- while using dictionary

it evaluate the value of the key not the value

Let's see example

dict = {True : True, False: True,True:True,False:True}
print(any(dict))
print(all(dict))
True
False

As we noticed that all the values of the dict is True but it print True in the second case of use all because it not care about what is exist in value it only see the key



2- Empty iterable
In case of any it print false and in case of all it will print True 
print(any([]))
print(all([]))
False 
True

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page