Python Tutorial: Else Clauses on Loops

Поделиться
HTML-код
  • Опубликовано: 21 ноя 2024

Комментарии •

  • @gergerger53
    @gergerger53 9 лет назад +25

    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.

  • @ivanabregu1
    @ivanabregu1 Год назад

    Thanks a lot Corey, each time I have to prepare my self for an interview I come back to your videos!! Saludos from Argentina

  • @jermm2183
    @jermm2183 2 года назад +1

    This is on my PCEP tomorrow I think since I have seen many practice questions with this. I think of it as it simply outputs this when you're adding a number but the loop won't for one iteration, but your video explains it far better than that. Thank you'

  • @malikrumi1206
    @malikrumi1206 9 лет назад +7

    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.

  • @sagshah10
    @sagshah10 7 лет назад +9

    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 :)

  • @deViant14
    @deViant14 3 года назад +1

    my_list = ['Corey', 'Rick', 'John']
    positions = [index for index,name in enumerate(my_list) if name=='Steve']
    index = positions[0] if positions else -1
    print(index)

  • @TheChrist559
    @TheChrist559 5 лет назад +4

    You sir will make me into a software engineer

  • @khaled-dz8357
    @khaled-dz8357 3 месяца назад

    Abdunetly thanks Corey

  • @IntegralDeLinha
    @IntegralDeLinha 2 года назад

    Thanks! I actually have a lot of applications for this!

  • @DarkAcademyUniversity
    @DarkAcademyUniversity 8 месяцев назад

    Please, make more videos, such as "sys" module or creating some project. PLEASE!

  • @most_aalsi
    @most_aalsi 4 года назад +1

    great explaination sir..... thank you

  • @382946rthu
    @382946rthu 7 лет назад +1

    Thank you Corey 😀👍

  • @DevLinux
    @DevLinux 6 лет назад +1

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

  • @videofountain
    @videofountain 5 лет назад

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

  • @sketchytv1321
    @sketchytv1321 2 года назад +1

    informative sir

  • @hubertcombomarketing2693
    @hubertcombomarketing2693 4 года назад +1

    Good as always. Thanks.

  • @asands123
    @asands123 5 лет назад +3

    These are amazing. I just became a Patreon member :)

  • @sahilsharmahere
    @sahilsharmahere 3 года назад

    Great video brother, good explanation.

  • @mufidharahap4192
    @mufidharahap4192 4 года назад

    Best explanation. Thamks

  • @mahimajangra3248
    @mahimajangra3248 4 года назад

    it was exactly what i needed ....thank you 😍

  • @dhanashree318
    @dhanashree318 7 лет назад +1

    I love all your videos and this one too!

  • @sakshamchhatkuli271
    @sakshamchhatkuli271 4 года назад +1

    Nice !!

  • @dellsantiago8108
    @dellsantiago8108 3 года назад

    Wow you are very very good

  • @harikrishnants8545
    @harikrishnants8545 4 года назад

    In finding the index, problem after break-in else statement why do we need ( -1 ). I didn't understand the concept.

  • @newaccbub
    @newaccbub 4 года назад

    your explanations are so clear! thank youu

  • @uchihasride932
    @uchihasride932 3 года назад

    first thing how can we use else after for ...we cannot use it in c++ and 2nd
    else indentation is not under if so after break why didnt else print respond

  • @aaronzewdu
    @aaronzewdu 4 года назад

    this is amazing......

  • @DakshGargas
    @DakshGargas 6 лет назад

    That helped, Thanks!

  • @SergioSovi
    @SergioSovi 4 года назад

    in loops else == nobreak; does it mean it only works with blocks with break statements?

  • @ismailrahmouni3032
    @ismailrahmouni3032 4 года назад

    Thank you so much Corey for your wonderful videos;
    would you please make a video on how to customize your IDE like the one you are working on?

    • @coreyms
      @coreyms  4 года назад

      If you search my channel for Sublime Text then I have a video on how I set it up.

  • @joaovitordutra3514
    @joaovitordutra3514 5 лет назад

    Hi!
    I never did a for loop in python like "for i, value in enumerate(to search)".
    I may be sounding dumb but where can I learn more about that?
    Thanks!

    • @NathanealGetahun
      @NathanealGetahun 11 дней назад

      It’s a function called enumerate. It’s a way to return the index and the element at the same time.

  • @rajeshs2840
    @rajeshs2840 5 лет назад

    Else Clauses on Loops will help to get rid of flags.

  • @tatianazihindula8762
    @tatianazihindula8762 7 лет назад

    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

  • @sunwoodad
    @sunwoodad 9 лет назад +6

    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.

    • @coreyms
      @coreyms  9 лет назад +5

      +Ethan Kim That works too. I was trying to show how the control flow worked with the break statement and that the else clause is actually skipped when a break occurs. If I was to return i when the target was found then it would have exited the function at that point and I wouldn't have been able to demonstrate that the else clause wasn't triggered.
      So really it's just to get the concept across and not written to be the most efficient. But your way is definitely more clean. Thanks for the comment.

    • @sunwoodad
      @sunwoodad 9 лет назад

      +Corey Schafer Thank you for quick response. If so, which case we SHOULD use for~else statement?

    • @coreyms
      @coreyms  9 лет назад +9

      +Ethan Kim "break" will only break you out of the for-loop. "return" will return a value and exit whatever function you are in. So if you have things to do within the function after the for-loop, then break is what you want. I don't know if I explained that very well, but here is a link that does a better job with examples:
      stackoverflow.com/questions/28854988/what-is-the-difference-between-return-and-break-in-python

    • @sunwoodad
      @sunwoodad 9 лет назад

      +Corey Schafer Thank you again, so if for loop doesn't have 'break' such as copying list to list then it doesn't need 'else', we just need to put that line at the bottom, correct? Anyway, sorry for Pirates down :)

  • @blank4331
    @blank4331 3 года назад

    my_list = [1, 2, 3, 4, 5, 6, 7, 9]
    for number in my_list:
    print(number)
    if number == 6:
    break
    else:
    print("This is the else statement")
    The output is 1
    Idk why the output is 1, can anybody help me with it.

  • @mariogabriel9230
    @mariogabriel9230 2 года назад

    Top

  • @mikijasar2594
    @mikijasar2594 3 года назад

    Please if you have time to help me figure out how to solve this problem. This is essentially a pointless program. Here we have a list of names where the user searches the names in the list. If the name is not in the list it works ok. However, if the name is found, we first get the information that the name was found, but then there is a problem. Namely, the else statement is also executed and if we have two same names in the list then the print statemet of the if block is executed twice and I want it executed only once. How can I handle this. Thank you very much in advance.
    # check the names in the list
    name_list = ['Dani', 'Tom', 'Mark', 'John', 'Mark']
    user = input('Which name you are looking for in the list: ')
    # if user enter Mark
    for i in name_list:
    if user in i:
    print(i)
    else:
    print('There is no such word in the text')
    _____________________________________________
    Which name you are looking for in the list: Mark
    The name Mark is in the list
    The name Mark is in the list
    There is no such word in the text

    • @pjmmccann
      @pjmmccann 3 года назад

      You just need a "break" after the "print(i)" statement, which pulls you out of the loop if the name is found (the first time it's found!) and also prevents the statement in the "else" from being executed.