Maxim Danilov
2 min readOct 5, 2019

--

hi, Seremba. For those article you should learn more about Python. In so small article and chat answers you already make some mistakes:

5. use IN

for testing membership of elements at the beginning of the collection, lists or tuples perform more than 100% better than sets. the same ist true for small collection or if you collection has “collision”

8. Formatting Strings:

You wrote article in 2017, but f-strings in python was since 2015. it much more efficient than .format or %

9. List comprehension, even finder.

not(%2) is good, but that requires a division and a negation. Because computers use binary arithmetic, a much more efficient solution is:

value & 1 == 0, i use bitweise operator.

12. Sets

in “good” you dont need a list conversion and variable elements_in_both

print('elements_in_both', set(ls1) & set(ls2))

13. Set Comprehension

Aleady Ioannis Petrousov says: bad “good” example.

unique_elements = {element for element in elements} is the same:

unique_elements = set(elements).

to make a good example you can use this:

{expression(variable) for variable in iterable [predicate][, …]}

14. Use the default parameter of ‘dict.get’ to provide default values.

If you make it more than 1 time for same key in yours program you can use

payload.setdefault.('auth_token', 'Unauthorized')

15. Don’t Repeat Yourself (DRY)

More obviously is

print(‘{line}\n{user}\n{line}’.format(line=‘-’*30, user=user))

but are you shure that ‘\n’ work always? If you don’t care about multi-platform stuff you can just use ‘\n’.

And, of course, the last one. if you really want Dont repeate yourself:

print(‘\n’.join(((‘-’*30, ‘user’)*2)[:3]))

p.s. I havу worked 10 years in siemens labor like Teacher for Students.

--

--

Maxim Danilov
Maxim Danilov

Written by Maxim Danilov

Python & Django Developer and speaker

No responses yet