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
ใ€€

Keep Current with matt harrison

matt harrison Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @__mharrison__

27 Oct
Here's a crazy piece of Python code (from @driscollis) illustrating how to calculate prime numbers below 1000 in a "functional" style.

How would I write this?

๐Ÿงต

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:
Read 8 tweets
26 Oct
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/
Read 5 tweets
18 May
This tweet obviously resonates with many.

I often get asked about debugging when teaching a course...

A ๐Ÿงต

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
Read 13 tweets
17 Mar
I'm teaching a Fundamentals of Python class this week.

We talked about tuples, and there was some confusion as to their existence.

Certainly, you could live in a world without them, but...

๐Ÿงต
First of all ... pronunciation.

You can say "two-pull" or "tuh-pull" (both are valid).
A big confusion is when to use a list or tuple?

My basic rule of thumb:

If you have items of the same type and care about the order or re-arranging it, you want a list.
Read 9 tweets
1 Jan
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). โš’๐Ÿง 
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(