Avoid ENDLESS Variable Comparisons In Python With THIS Shortcut

preview_player
Показать описание
Avoid endless variable comparisons in Python with this. #Code #Shorts
Рекомендации по теме
Комментарии
Автор

also, searching through a set is done in O(1) time while searching through a list is O(n), so a set would be better for this scenario

MigzPigz
Автор

No sane individual would start with that first if statement. First pass would be a for loop.

sinistergroupon
Автор

Please make more logical, real scenario problems and tricks :pp don't produce random things.

trilon
Автор

Same with for example several function outputs, when you wanna check if one of the inputs in a function gives a certain output like 4, you can do 4 in [foo(x), foo(y), foo(z)].

callbettersaul
Автор

anyone who starts python could just loop through the list instead of manually write down each condition

tdh
Автор

Wow and how does number==1 or number==2 or ... number==n check if number is in numbers?
People who don't know about the in-key word will properly use manual written loop but not this chained comperasion.

oida
Автор

Isn’t the first if statement just checking if the int number is equal to 2? Not actually reading through numbers

ovi_snipes
Автор

print(list.index(2)) would tell you if a given number is in the list and where it's at, right?

viatori
Автор

It’s the same thing either way but yeah it’s definitely more readable and less typing

rkroll
Автор

Either this or you could do if numbers[number]:

lguthrie