3 Python Features I Use Everyday!! #python #programming #coding

preview_player
Показать описание
This short video described three Python features I use everyday. This includes list comprehension, fstrings, and unpacking.

Background Music:
Creative Commons / Attribution 4.0 International (CC BY 4.0)
Рекомендации по теме
Комментарии
Автор

Its better practice to use n.rsplit(' ', maxsplit=1)[-1] if you only want the final word. maxsplit=1 meaning it will stop splitting after finding the first space and rsplit meaning it will start on the right of the string and go left. In this case not much is gained but if the string may be long with many words then this will reduce time complexity.

cole
Автор

Keep on going, good work better explained than most paid courses.

GardenofEdens
Автор

That unpacking thing. I feel I could've used that not long ago. Thanks!

deemon
Автор

Regarding the last example: First, using _ is just convention for any assignment (generally from functions with tuple return values) you don't intend to use. It could be any variable name, but the _ makes it so the variable isn't assigned to the namespace (though you can still call it just like a regular variable).
Second, the result of that assignment would be:
first="John"
last="Smith"
_=["United States", "Blue", "Brown"]
age=29

acoupleofschoes
Автор

List comps are lowkey one of my favorite parts of Python tbh.

LunarSoul
Автор

for the first example, you should really use rsplit (reverse split) and set a limit of 1, so tthe function stops at the first occurance of a space character.

felixcusson
Автор

Was active in python2 but not much since. Appreciate finding these shorts with tips and tricks!

voxar
Автор

Ngl didnt knew the ladt one, really usefull

imperial
Автор

The second one is string formatting that’s why it’s f

whosalbercik
Автор

Nice! .. my tip is don't use unpacking on lists without fixed length

tonymudau
Автор

so the asterisk is the *args variable for positional arguments, you can also use this to send unpacking request to a function. you can also do this with keyword arguments using the kwargs argument or double asterisk **

bjni
Автор

For your first example, I tried to account for trailing space characters at end of the string but still use a list comprehension and i came up with this:

last = [re.search(r"\S+", n[::-1]).group()[::-1] for n in names if bool(re.search(r"\S", n))]

i know its not as elegant as your solution (i just wanted to see what it would look like with a regex). A more elegant way based on your solution would be:

last = [n.rstrip().split(' ')[-1] for n in names]

potatojeans
Автор

i use f string very often
the 1st one not so much but i know about it
the last one seems very useful thanks for letting me know

mrtbts
Автор

I've never seen that unpacking trick before. Thanks for posting!

madisonhanberry
Автор

I'd personally use a slice for that last one, seems easier

kindahungry
Автор

the last one was really good.keep making more.

maunoxyz
Автор

I need to agree .. the f strings are something i always use for my python programs

CreateCG
Автор

You could use just split() since if no arguments provided then this function will split string by spaces.

vanya
Автор

You can do more with unpacking too. If you use something like *leftovers instead, you can preserve the interim elements (i.e. ‘United states’, ‘blue’ and ‘brown’) in a sublist called leftovers. I had to look it up, btw.

RS-fzps
Автор

Nice one, I didn't knew we could add another variable after using the asterisk while unpacking

VinayKumar-ehfx