Python Tutorial: Else Clauses on Loops

///Python Tutorial: Else Clauses on Loops

Python Tutorial: Else Clauses on Loops

FavoriteLoadingAdd to favorites

What does the ‘else’ clause mean when it comes after a for or while loop? This isn’t obvious at first glance, so let’s take a look at a few examples and clear up any confusion with the Python ‘else’ clause.

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2019-07-08T21:03:29+00:00July 8th, 2019|Python Video Tutorials|13 Comments

13 Comments

  1. videofountain July 8, 2019 at 9:03 pm - Reply

    I think the video is useful. A suggestion would be to create a video for the most recent version of Python and a link.

  2. Investor Jess July 8, 2019 at 9:03 pm - Reply

    You sir will make me into a software engineer

  3. DevLinux July 8, 2019 at 9:03 pm - Reply

    Well explained with examples! Had to hit that Subscribe button!

  4. Ivan Booba July 8, 2019 at 9:03 pm - Reply

    return -1 instead break ? lol

  5. Armando Morales July 8, 2019 at 9:03 pm - Reply

    I've been really confused on the for and else's in this code that checks for anagrams for a good while now and haven't been able to understand the indentation of else inside loops. In this code, why is it that on our last for loop (the one that checks that all counts are 0) we need to keep the else under the for and not indent it on the same level as the if, but on all of the other for loops that check the letters in count, we keep the if and else on the same indentation. It makes sense to me to keep the if and else on the same level on the last loop as well, but it doesn't work for examples like 'aabbcc', 'aabbc' we get the wrong solution. Here is the code:

    def anagram2(s1, s2):
    s1 = s1.replace(' ', '').lower()
    s2 = s2.replace(' ', '').lower()

    if len(s1) != len(s2):
    return False

    # Create counting dictionary
    count = {}

    # Fill dictionary for first string (add counts)
    for letter in s1:
    if letter in count:
    count[letter] += 1
    else:
    count[letter] = 1

    # Fill dictionary for second string (subtract counts)
    for letter in s2:
    if letter in count:
    count[letter] -= 1
    else:
    count[letter] = 1

    # Check that all counts are 0
    for k in count:
    if count[k] != 0:
    return False
    else:
    return True

    * This last loop here, why is it incorrect to keep the else under the if? Makes sense to me, if !=0 return false else return true, but apparently it must be right under the for, unlike all of the other loops in this code.***

  6. Daksh Gargas July 8, 2019 at 9:03 pm - Reply

    That helped, Thanks!

  7. Tatiana Zihindula K. July 8, 2019 at 9:03 pm - Reply

    Tbh the last example is impractical. Instead of braking why not just return i? I bet it would be use to clean up the loop’s potential mess

  8. sshah93 July 8, 2019 at 9:03 pm - Reply

    Damn, why do i find this now, just wrote a piece of code where this would have been much more efficient, I was forced to use found = False, and then when the loop finds a match I had to do found = True, then break and then write "if found: xxx else: " , this would have been much more easier. Oh well thanks alot, I will be using it in the future, and when I plan to refactor my existing code. Thanks Alot for all the Awesome Tutorials 🙂

  9. Shree July 8, 2019 at 9:03 pm - Reply

    I love all your videos and this one too!

  10. Aaron Howard July 8, 2019 at 9:03 pm - Reply

    Thank you Corey 😀👍

  11. algrmu July 8, 2019 at 9:03 pm - Reply

    Your Python videos are great. There is a lot of information on the basics already out there, but you bring down the more complex functionality to a level that is manageable/understandable and doesn't already require you to be a coding buff to utilise or see the value in. It's definitely your niche, which is why I subscribed.

  12. Ethan Kim July 8, 2019 at 9:03 pm - Reply

    Why don't we just write like this:

    def find_index(to_search, target):
    for i, value in enumerate(to_search):
    if value == target:
    return i
    else:
    return -1

    This result is same.

  13. Malik Rumi July 8, 2019 at 9:03 pm - Reply

    I'd never even heard of an else statement on a for loop before, but having watched this video a couple of times now, I can think of several use cases I have for it. Thanks, and keep 'em (python stuff) coming.

Leave A Comment

*