I Didn't Know That Python's For Loops Could Do THIS 🙈

preview_player
Показать описание
I didn't know that Python's for loops could do this. #Python #Code #Shorts
Рекомендации по теме
Комментарии
Автор

That syntax is confusing. Why would the else block execute when all conditions pass? That should have been named finally.

letsthinkforamoment
Автор

this literally feels like a bug not a feature

itznukeey
Автор

How confusing do you want your language to be developed? Yes.

SuperElephant
Автор

I accidentally found this by wrongly typing

AKA-
Автор

a bit more precise is to say that the 'else' statement runs when the condition of the 'for' loop turns false.

In the case of 'range(3)', let's say that it is equivalent to giving a list '[0, 1, 2]', which has three elements. The condition for looping through an iterable (the list in this case) is that it must have remaining items. After you reach '2' (third item) and print it, python goes back, checks the list and it turns to be that you are out of items, then the condition to loop through the list becomes 'false' and the 'else' statement gets triggered.

patocarrasco
Автор

From what I've heard from other devs this is considered to be bad practice. It makes it harder to read as its wording is unintuitive.

the_th_sun
Автор

This can be done in while loop as well

adventuresofavalon
Автор

Man thanks i had a programming problem
Which prints * inside the loop and has a if statement with break when the number equals certain number . I couldn’t get why after executing the for loop the else part was not executing….
Your video helped me

Ro-fdqu
Автор

This is bad practice. Even Python’s developers said they regretted including this because of its unintuitive behavior.

petebumble
Автор

This isn't exactly a feature that is touted around much hence why very few people know about it.

Even the creator regrets making it because he admits that the syntax is confusing and it isn't really useful.

You can get a full explanation as to why you shouldn't use it if you look up mCoding. He does a pretty good job explaining why it's bad lol.

xXJMXx
Автор

This becomes useful in cases like finding whether a number is prime or not

benbabu
Автор

Oh it needs to break..
This just solves a issue i suspect is in my script that i was working on last week. Hope i remember to fix it on monday 😅

RocketLR
Автор

if i = 1 <- this will never give back “True”

sunwukong
Автор

Me: Knowing this
Also me: Never using it 🤣

zeldaplayergl
Автор

The "else" name seems confusing here, btw 😅

gazzalifahim
Автор

I think for might stand for "if or" cause you get if i = 0, or i =1 or etc

Alexthatsnottaken
Автор

I can't think of a situation where that would be useful

cimmik
Автор

They say Python is user friendly and easy to learn. They lied. Syntax like this makes absolutely no sense. ELSE is supposed to be executed if the main thing is false. That's why it's called ELSE, it's the other branch!!!

humanrightsadvocate
Автор

It seems useful as a feature. But the key word 'else' seems very random. Should have been another key word like 'all' or 'finished' or 'complete' or something

oscareriksson
Автор

it should be 'finished' or 'finally' or something like this but not using 'else' as the name

junyulu