Комментарии:
Great!, A question I never asked myself. Great video!
Ответить10**7 are ten millions, not 1 that would be 10**6.
Ответить... is the same thing like pass?
Ответитьwhat editor is this?
Ответитьelif conditions does not exists, elif is really an further simplified and readable version of an if inside the else of the condition before it,
so the question is reduced to if vs if inside an else, and that does not make sense since an if can't replace an if inside an else since the if inside the else is connected with the else, while the if doesn't
Excellent video man, thanks !!
ОтветитьWhy do you need that crap?
Python has match since 3.11 or something.
Great video bro, always watch him, and i have a doub, why you declare de type of the variables? is because is good practice, avoid some dinamic typing problems, or maybe is because more faster?
ОтветитьThey dont do the same thing. If the condition changes to meet the next condition, elif will make sure that it doesnt do it, and else is everything not matched by the ifs and elifs.
ОтветитьThe first test isn't really identical, since with the "if if", the last ... Runs no matter what so it is functionally different. It's not like the "if else" where it only runs if all else fails.
I guess that was a part of the video too though, showing that these are fundamentally different. I just think a head to head test should use functionally equivalent functions.
I have a simple rule:
Use If Else.
Using if and if not in chains makes it hard to re-read your own code weeks, months or years later.
If you are really worried about speed, then you probably shouldn't be using python in the first place.
10 million...
ОтветитьI do like this kind of video, comparing the different ways to do the same thing in Python. If one way is 10% faster, then that's something I want to know.
ОтветитьWhich method is best? Pretty vague answer in video
ОтветитьThere are also conditions where multiple if statements would actually be true, yet you only want to run the first true statement. A classic example is printing a letter grade, where you can say…
if grade >= 90:
print(“A”)
elif grade >= 80:
print(“B”)
elif grade >= 70:
print(“C”)
elif grade >= 60:
print(“D”)
else:
print(“F”)
If you had a grade >=90, then by definition you would also have a grade >= 80, etc etc. But this won’t matter with an elif because the first condition was already true, and so the rest is skipped.
I believe that the simplest way to think about it is in terms of the old GOTO instruction...that is the underlying code at a simpler level. With the "if-if" case the GOTOs will always point to the end of the current if statement, whereas the "if-elif-else" will always point to the last condition, regardless of which condition it enters.
In terms of "Big O" notation, "if-if" will always be O(N), however, the "if-elif-else" will be O(1) at best, O(N) at worst. Simple analysis, but worth noting for any programming language.
Match case vs if else !!!!! Battle
Ответить🙏👍
Ответитьi think the 2 kastd ones are producing the exact same assembly
Ответитьdef what_should_I_do():
if timeit.if_if < timeit.if_else:
return if_else
else:
return if_if
print('I am such a nerd')
❤
Ответить