Let's explore the "any" and "all" built-in functions in Python.
A ๐งต
First of all, I'll teach you how to fish in Python before giving you the fish. ๐๐ฃ
The built-in "help" function will give you documentation in Python. Make liberal use of it and reach out to it before ceding control to a search engine.
These are "aggregation" or "reducing" functions. They take a sequence and collapse it to a single value.
"any" returns if any value was truthy.
"all" returns if all values were truthy.
Most examples look like this:
Generally, I don't have lists of true, false values. Rather I tend to create them using "predicate functions" and comprehensions. A predicate returns a true/false value:
You can also replace "and" and "or" with "all" and "any". Here is code that checks if a letter is alphanumeric, period, or underscore:
Here is another pattern. If I wanted to determine if all characters in a filename were "valid", I would probably use a for loop and check each character. However, such for loops can be replaced with "all":
Likewise, we can replace searching for loops with "any". Here I search if there is a negative number and show the equivalent "any" version:
I hope you enjoyed learning about patterns for "any" and "all".
Feel free to share to your network and follow me for more Python insights. ๐
โข โข โข
Missing some Tweet in this thread? You can try to
force a refresh
Functional programming like this can be great for minimizing lines of code. But it is also great for making your brain spin. Here is how I would initially write this (if I were fancy, I would use the Sieve of Eratosthenes):
Can we collapse this into fewer lines of code? Certainly, (the functional style already showed that) we can. One thing to realize is that lines 3-6 can be replaced with an any call:
I'm frequently asked: Is it easy to get a tech job?
Probably not. It will require a degree (or a lot of hard work). People who say otherwise are probably selling you something.
1/
I've taught thousands of people Python over the years. Some pick it up quickly, others really struggle. That is why Universities have "weeder" courses. To sift out the strugglers.
2/
Does that mean you can't self-learn? No, but it will take a lot more work than just sitting back and watching YouTube.
3/
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
-Kernighan's Law
๐คฏ
When I teach programming, I emphasize that to me, the most important part is that the code is "readable."
You write code (once or twice), but it is read all the time. Optimize for what happens a lot.
Not:
๐ฆ Brevity
๐ฆ Cleverness
๐ฆ Using all of the features
One of the most effective things I did for my career is probably the most difficult.
๐งต๐
It is not difficult because it requires strength or knowledge, it is difficult because it requires will power.
I'm talking about writing a book. ๐
Writing a book taught me a bunch of lessons about work, myself, and business. I would say it jumpstarted my career (even though I had been employed for over 13 years by that point). โ๐ง