7 More Tricks to Write Better Python Code

preview_player
Показать описание

- 00:30 Inline if-else statements
- 03:00 Sequence comparisons
- 10:00 Extended unpacking (Python-3 only)
- 13:57 Dict comprehensions
- 18:25 collections.OrderedDict
- 26:17 collections.Counter
Рекомендации по теме
Комментарии
Автор

I watched the previous 7 tricks video 2days ago. I want to thank you again for the new 7 more tricks video.

yuanzhenggay
Автор

Nice! FYI dict is now insertion ordered as of python 3.6 (guaranteed in 3.7+)

snoopyjc
Автор

in the case where all your dict values are integers, instead of passing (lambda: 0) to defaultdict you can do defaultdict(int), which will also default to 0

zzphantom
Автор

I wonder how many people know about Groningen xD
Thanks for the excellent video Sebastiaan!

francescovanspronsen
Автор

Thx. On zip. There is also zip_longest which allows you to specify a value for a missing item in the iterable. So following your version comparison example you can specify a missing value is 0.

thesavvyfounder
Автор

Awesome job! Only problem is I was disappointed that you didn't use Maastricht. It is one of my favorite cities when I visit the Netherlands. Thank you for the lesson :)

johnreddenbacher
Автор

Great sir, super explanation and teaching . you know extreme teaching capabilities. Appreciate great help :)

srrkmm
Автор

great tut, do more tutorial master . greats from Egypt

ansakezay
Автор

Ad 9:40: You could use the lexicographic example of sorting names - "Ben" comes before "Benny" because it's shorter.

Great videos tho! Thanks for them :)

Автор

Instead of defaultdict(lambda: 0) you may use defaultdict(int) as int() == 0

vitalyefremov
Автор

In Python 3.6 you can change your print statements to (from example 1):

print(f'Update check: {msg}')

Uses format (f) and the variable {msg}. VERY Pythonic

kurtb
Автор

at 09:24, how about ?
msg = "update needed" if cmp(latest_python, my_python) else "upto date"

addishiwottadesse
Автор

Very helpful, however the lists/tuples/dicts with same number of items are very rare in real world. Perhaps that can be one of the topics for next video along with good ways of running multiple loops for such uneven elements. All the best.

iamsandeep
Автор

ad. OrderedDict: you can make a list comprehension of tuples (key, value) and pass it (the list) to a OrderedDict constructor

be_pie_today
Автор

Thank you ! it's very usefull for me.

konstantin
Автор

You should switch to fstrings, much easier to read than the % way

Other than that: great video

schogaia
Автор

Hi, Sebastiaan.
Am just learning Python and came across this and your previous 7 Tricks video--both excellent! I wish most Python courses just taught us these tricks from the beginning so our code would look cleaner and run faster.

re: DefaultDict vs Counter

What's the difference?

Your dict has the values as counts already, right? So if we want the count, we look for the key. But if key doesn't exist and we know the k, v pair, we can just add a default item to dict. So, what's the point of the Counter, especially since dicts can only have unique keys? I heard something about a single item default add (using the lambda notation) vs a "larger dict, " but still confused.

Cheers!

MadLadsAnonymous
Автор

dict(zip(map(string.capwords, cities), populations))

zzphantom
Автор

OrderedDictionary is useful but it's a double linked list. So it uses double the size of a regular dictionary. Any other way??

harshteck
Автор

Not sure if the trick with learning which version of python you're using is the most important thing to learn that you needed to put it as trick #1 towards better programming

elmirach
welcome to shbcf.ru