10 Python Shortcuts You Need To Know

Поділитися
Вставка
  • Опубліковано 30 лис 2024

КОМЕНТАРІ • 209

  • @TechWithTim
    @TechWithTim  3 роки тому +18

    💻 Want to jumpstart your career in tech? Click the link below to match with the best programs for you and join your first coaching session. The first 1,000 people to click the link can join their free career coaching session with a Career Karma coach today: ck.chat/tech-with-tim

  • @Shalonchi
    @Shalonchi 3 роки тому +215

    Something that he left out about unpacking is that you could use * to group a section of your collection. Let's grab his example tup = (1,2,3,4,5), if we would unpack it as follow a, *b, c = tup, than a = 1, b = [2,3,4], c = 5

    • @n49o7
      @n49o7 3 роки тому +2

      You can also use this to optionally assign. For example, with a, b, *c = (1, 2), a == 1, b == 2, c == []. It's pretty ugly, though.

    • @yogiblak7819
      @yogiblak7819 2 роки тому +1

      In your example, what would determine that b = [2,3,4]? Is is because it is the MIDDLE values and 1 and 5 are the beginning and end? How would I get b to = [3,4,5] if a, *b, c = (1,2,3,4,5)?

    • @evanli4272
      @evanli4272 2 роки тому +1

      @@yogiblak7819 Hi I know this is late but I assume that you would instead just do a, b, *c = (1,2,3,4,5), because the star automatically divides it up into the number of divisions you need

    • @yogiblak7819
      @yogiblak7819 2 роки тому +1

      @@evanli4272 but there are 3 variables, so does it automatically assign a to first value, c to last and be to all values in between? Whatn if there were 60 elements in the list? Would b = all 48 of the values not in position 0 and and len - 1?

    • @jelmermulder7276
      @jelmermulder7276 2 роки тому +1

      That's exactly (one of the things) what I was looking for. Thanks

  • @Bassel-Ojjeh
    @Bassel-Ojjeh 3 роки тому +49

    You are truly a legend Tim. I used to code when I was younger but lost my passion for it however now I am re-engaged to coding and your videos have been FANTASIC. Keep up the excellent quality of your teaching and videos.

  • @huantian
    @huantian 3 роки тому +118

    Watch out when you multiply a nested list like that! All the sublists will be the same object, and changing one will change the rest

    • @paulah1639
      @paulah1639 3 роки тому +4

      This is important. I didn’t know that. Thanks for pointing it out.

    • @kenny-kvibe
      @kenny-kvibe 3 роки тому +9

      I was once debugging exactly this problem for one whole day, where I didn't understand what happens at: [[random()]*5]*5
      The 2D list was containing the same values because at run-time the "random" call executes first (most inner brackets) and returns a value and then that value gets mutated, and that mutation (column) gets mutated further on. It basically mutated the pointer/reference that points to the first "random" value.
      Must be really carefull when it comes to this!

    • @arkie87
      @arkie87 2 роки тому +1

      @@kenny-kvibe I also had to debug this problem once. Luckily for me, it was in a relatively small program, so I could track it down somewhat quickly.

    • @jplkid14
      @jplkid14 2 роки тому

      I ran into this and thought to myself...this sucks...lol

  • @xhjb4ever
    @xhjb4ever 3 роки тому +172

    “The first thing you need to know is the sponsor of this video”, haha

    • @shwm19
      @shwm19 3 роки тому

      Was just about to say the same.

    • @stephane5346
      @stephane5346 3 роки тому +3

      raid shadow legends

    • @sdaniels1288
      @sdaniels1288 3 роки тому

      Smooth segue, lol!

    • @borat1
      @borat1 3 роки тому +3

      That’s Linus levels of smooth

    • @vinicus508
      @vinicus508 3 роки тому

      Hahaha so smooth. But with this high quality content I dont mind at all haha.

  • @marcoreiser8836
    @marcoreiser8836 3 роки тому +15

    11:25 , you probably wouldn't want to do this since it uses the original list's reference to duplicate it. If you make changes to one of the nested lists the other lists will also be changed. If you do x[0][0] = 2 for example, x is now [[2,2,3],[2,2,3],[2,2,3],...] which is most likely not the desired behavior.

  • @Shepherd-ik1qw
    @Shepherd-ik1qw 10 місяців тому +1

    EXTREMELY more helpful than I had expected !!! Thanks Tim !!

  • @marckiezeender
    @marckiezeender 3 роки тому +11

    fun fact: the "multiple assignment" syntax is actually the same as the unpacking syntax: the right side of the equation creates a tuple that is then unpacked by the left side. for instance: x, y = a, b creates a tuple (a, b) and then unpacks it into x, y

  • @serg472
    @serg472 3 роки тому +22

    There is no name for the multiple assignment because there is no multiple assignment here, it is still a tuple unpacking:
    x, y = 1, 2
    is the same as:
    x, y = (1, 2)
    A tuple is defined by the commas, not the brackets. Brackets are optional in tuples and used just for readability, that's why a tuple with one element has to be declared as (1,) and not (1).

    • @kenhaley4
      @kenhaley4 2 роки тому +1

      Exactly right. Too bad it's not usually explained that way.

  • @erin1569
    @erin1569 3 роки тому +1

    Another cool thing combining multiple assignment, * and fstring:
    tup = (1, 2, 3, 4, 5)
    a, *b, _, c = tup
    print(f"{a = }; {b = }; {c = }")
    >>> a = 1; b = [2, 3]; c = 5
    _ works as a variable (usually a throwaway one).

  • @fadamitanolatomiwas.miguel6626
    @fadamitanolatomiwas.miguel6626 3 роки тому +35

    God bless you Tim! Thank you! From Nigeria

  • @DerMarkus1982
    @DerMarkus1982 2 роки тому +3

    That bit about For / Else was very insightful to me, specifically the fact that Else: ... only gets triggered if the For ... part has completed without a *break* . Thanks for this!

    • @matbailie3816
      @matbailie3816 2 роки тому +1

      Don't use this. There's a book full of reasons this is bad, not least being that even the creator regrets its inclusion in python.

  • @timwebster85
    @timwebster85 2 роки тому

    args and kwargs finally make sense! Thanks Tim

  • @uscjake868
    @uscjake868 2 роки тому +1

    Thanks Tim! I couldn't program at all 6 months ago, and with videos like yours it really helps make coding fast and productive.

  • @MaBuSt
    @MaBuSt 3 роки тому +9

    for list comprehension, or any other 'tips', its very easy to get in a habbit of trying to keep everything tight and fun and pythonic, but what should guide your behavior is the following-
    find someone who has had to troubleshoot a problem in a python module while under extreme time pressure, ask them what 'pythonic' habits are very hard to understand when under extreme duress.
    One of the pythonic habbits that because very hard to trouble-shoot under time pressure is list comprehensions where people have put way too much logic into a single conditional.
    How much is too much? A good rule of thumb would be 'anything more than 1 conditonal'.
    1 conditional is easy, why is that our limit? Because the number of times that 1 condition turns into 2, or 3, or more during refactoring... is very high.. each step/addition makes sense when it is added but when you are trying to comprehend the entire list comprehension while on the run and in a hurry, it because a huge issue.
    list comprehensions are nice, just keep them very short and simple.
    create additional list comprehensions if you need to keep conditionalizing them.
    Make sure that the You under time pressure, or your friend or coworker, isn't in big trouble later.

  • @dreamy-star
    @dreamy-star 2 роки тому

    'for & while else' and last bonus is pretty nice tips!!

  • @alexbadger8385
    @alexbadger8385 3 роки тому +1

    Thank you for going over list comprehension. I was so confused on how to work them and their logic. You explained it so simply and beautifully

  • @BoredSpaceRoo
    @BoredSpaceRoo 3 роки тому

    Who in their right mind gave this video thumbs down? It is brilliant. Thank you Tim you Python genius. I’ve learnt so much from your videos.

  • @the_ehiane
    @the_ehiane 3 роки тому

    Damn! Bro dosent even think. He just goes with the flow.

  • @paulah1639
    @paulah1639 3 роки тому +3

    Very helpful video. Straight to the point. I watched it several times to make sure I remember each one. I am using all of them now including the bonus itertools. Thanks a lot Tim. 👍👍

  • @kvelez
    @kvelez Рік тому

    Thanks for teaching us "zip".

  • @joshkristine6189
    @joshkristine6189 3 роки тому +1

    “The first thing you need to know is the sponsor of this video” You got it right Tim! 🤣 You Legend! Thanks for sharing this video.

  • @vijayragav4717
    @vijayragav4717 3 роки тому +4

    We need some Time out with Tim!

  • @stephenaustin3026
    @stephenaustin3026 3 роки тому +5

    I've been following you for quite a while, but this is the first time I recognized from your accent that you're Canadian!

    • @arvidd
      @arvidd 3 роки тому

      Wut? He is!?

  • @maverick_augustine
    @maverick_augustine 3 роки тому +10

    Just started learning python, your vids help me a lot.
    Thx man !!!

  • @polojuninho
    @polojuninho 3 роки тому +1

    That "bonus" one were very useful for me! Thx a lot!

  • @ShivaPrasad-hm5lk
    @ShivaPrasad-hm5lk 3 роки тому

    Your videos are my energy packs

  • @guitaek4100
    @guitaek4100 3 роки тому +2

    Multiplying a nested list causes trouble as x[0] is x[1] evaluates to True. That means if you change one, the other is changed as well

  • @jinhoyoon2497
    @jinhoyoon2497 3 роки тому +4

    God this is the kind of stuff that makes me love Python. Thank you for this video! I’ve learned so much!

  • @yogiblak7819
    @yogiblak7819 2 роки тому

    You said "Tern-er-rary". LOL! I don't know why I find that so funny, but I do. 😂 Keep up the great work! I love your videos and how fast you fly through it without all the padding like most UA-camrs. You get in, explain it, show an example, and move on. The way it should be! Fan for life..

  • @ananthramvijayaraj4554
    @ananthramvijayaraj4554 3 роки тому +5

    hey, what happened to the audio from 11:41 to 11:44

  • @MaBuSt
    @MaBuSt 3 роки тому

    WRT the f-string segment of the video, another way of printing variables that is very often used in early python (and thus everywhere in legacy code) is the format, bracket string:
    money = '$10.00'
    print("We have {} left to spend".format(money))
    I used to use f-strings exclusively until some of my code was getting deployed into < python 3.0 environments and causing all kinds of issues.
    If you know you are deploying into a brand new, all-modern python environment then there is no issue but if you are not sure then you should seriously consider format bracket strings.

  • @beanbag1415
    @beanbag1415 2 роки тому

    I''m learning python in a uni class and I love tips like this to make me seem smarter lmao.

  • @adityapundir6529
    @adityapundir6529 3 роки тому +2

    Thanks a lot Tim!

  • @user-do3pd1sk6c
    @user-do3pd1sk6c 3 роки тому +2

    12:10 You can also write 2 > 3 and 1 or 0 and that's pretty much the same thing

    • @not_vinkami
      @not_vinkami 3 роки тому +1

      Yes this works, but Python has given you the better way for the same action

    • @user-do3pd1sk6c
      @user-do3pd1sk6c 3 роки тому

      @Pínned by Téch With Tím haha nice scam bot

    • @not_vinkami
      @not_vinkami 3 роки тому +1

      @@user-do3pd1sk6c talking about how fake a scam bot can be

  • @cotekokotrees1527
    @cotekokotrees1527 Рік тому +1

    can you do ursina video

  • @yuchaofan
    @yuchaofan 3 роки тому

    great explanation of args and kwargs

  • @soniablanche5672
    @soniablanche5672 2 роки тому

    There is also formatted string
    'Hello, {}'.format(name)

  • @samuelosagie-aruya923
    @samuelosagie-aruya923 3 роки тому +1

    This channel inspires me please keep up the good work.

  • @Sebastian-uv7jf
    @Sebastian-uv7jf 3 роки тому

    7:49 You can also do this:
    x = [0] * 100

  • @Diamond_Hanz
    @Diamond_Hanz 3 роки тому +2

    Thanks Tim

  • @xwhitelight
    @xwhitelight 3 роки тому +1

    Very very useful! Thank you Tim.

  • @rverm1000
    @rverm1000 3 роки тому

    Nice I didn't know any of them except for f string

  • @SimonLam2024
    @SimonLam2024 3 роки тому

    Wow, I didn't know the For/While Else and other useful stuff. Thank you!

  • @darrorpsk6148
    @darrorpsk6148 3 роки тому

    thank you so much for putting the compilation list up front.

  • @akira_asahi
    @akira_asahi 2 роки тому

    Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.

  • @ronin2963
    @ronin2963 2 роки тому

    Best example of Lambda I've seen

  • @JeroenStiers
    @JeroenStiers 2 роки тому +1

    A very well paced and the exact level of detail for such a video. For a future version, you can maybe include the enumerate function to loop over a list while keeping track of the index (to do something every x elements - like printing a message)

  • @popoffs5273
    @popoffs5273 2 роки тому

    If you want to check if something is in a list, instead of using a loop, you could use
    if target in search:

  • @MrAdrien914
    @MrAdrien914 3 роки тому

    I love to use kwargs for django models, makes everything so much faster to write, read and manage in case you change the model

  • @h8handles
    @h8handles 3 роки тому

    You really post a bunch it is great to see

  • @level-onegamer8873
    @level-onegamer8873 2 роки тому

    Thank you Tim
    That explain so much for me👍

  • @deniskomoda6423
    @deniskomoda6423 Рік тому

    Thanks for another amazing video!
    I think I had an "AHA!" moment here, and I tried looking somewhere else if someone had already asked about it, but I found out I cannot even phrase it properly to render precise results in my search.
    I'll try to phrase here.
    Regarding the *args, if you use name, say, a list with another name besides args, you can still use the * to unpack the values exactly the same way, right?
    Up till now, I was attached to the idea that *args you could only use * to unpack iterables using specifically the variable name args, which I decided to test, and it is not true.
    So, is it a convention at all to use the variable name args when * unpacking with *args? Or is *args is just a way to refer to this technique of unpacking interables with *?

  • @sleepy314
    @sleepy314 3 роки тому

    Great. Just right speed and content.

  • @Gabirell
    @Gabirell 3 роки тому

    Thank you! Good work!

  • @senaysew8231
    @senaysew8231 3 роки тому +1

    Kind man! Thank U!

  • @sinasmith7521
    @sinasmith7521 3 роки тому

    your contents are really amazing man ...Thanks !

  • @unknownman5296
    @unknownman5296 3 роки тому

    700k🎉🎉🎉🎉congratz tim

  • @emaddeve20
    @emaddeve20 3 роки тому

    This Video is so helpful. Thank You Tim

  • @dominikwodarczyk6714
    @dominikwodarczyk6714 3 роки тому

    Better than chain_list = itertools.chain(lst, lst2) is
    chain_list = (*lst, *lst2)

  • @troeteimarsch
    @troeteimarsch 3 роки тому

    that was eye opening, thank you!

  • @TheUnofficialMaker
    @TheUnofficialMaker 2 роки тому

    Great tips, thanks.

  • @b391i
    @b391i 3 роки тому +2

    Keep going buddy 😎👍

  • @asaf7132
    @asaf7132 3 роки тому

    thanks for all the videos! may I ask to make some full time episodes on useful modules in python like intertools and random and much more I don't even know 😅

  • @ryu8777
    @ryu8777 2 роки тому

    awesome as usual. My bookmarks are full because of your great videos ))

  • @archsapostle1stapostleofth738

    11:29 be really careful with the nested list multiplication. It creates n copies of links to the same list, so if you change one list, you will change them all

  • @architech5940
    @architech5940 2 роки тому

    Great tutorials! However, it's hard to believe that One can start a 100k career from tutorials and 3 month boot camps.. So if you were to create a road map for individuals pursuing a dev job as a self taught person, what would this road map be? How likely am I to land a job assuming I did posses the necessary skills? What are dev interviewers expecting from the interviewee? What should I avoid in learning code so as not to waste my time. Much appreciated!

  • @danbrown6698
    @danbrown6698 2 роки тому

    This video is of extremely useful! You explain things pretty clear with great examples👍

  • @mightye6669
    @mightye6669 3 роки тому

    pretty cool vid. thx mate

  • @ingluissantana
    @ingluissantana 2 роки тому

    great video!!! Thanks!!!!

  • @zeroxiph
    @zeroxiph 2 роки тому +1

    What font do you use?

  • @dnasredine
    @dnasredine 2 роки тому

    not bad, the swap in 1 line

  • @elishababalola5895
    @elishababalola5895 2 роки тому

    Watch till the end😉 a bonus shortcut found✌✌

  • @sanjay.s4916
    @sanjay.s4916 3 роки тому

    wooow ! awesome tricks and thanx for the video

  • @IntangirVoluntaryist
    @IntangirVoluntaryist 2 роки тому

    what editor are you using that runs it right out of it and puts errors on the line where they errored?

  • @Lunolux
    @Lunolux 3 роки тому

    nice video, thx for the explaination of *args, **kwargs,
    for else and while else ; this is cool, first time i see that in programmation langage

  • @Xaminn
    @Xaminn 3 роки тому +1

    To Tuple, or not to Tuple. That is the argument.

  • @alandavidbanderas5899
    @alandavidbanderas5899 3 роки тому

    06:07 | Multiple Assignment I'm literally working in a python code that in some point does exactly that, awesome tip!

  • @bunmiade9767
    @bunmiade9767 3 роки тому

    Hello Tim. Your illustrations are very good. May I use your video for my class? I teach Python also.

  • @Damqq
    @Damqq 3 роки тому

    What do you think about the pydash module? Have you ever used it? Is it helpful to you? Maybe consider to make a video about that module? I think it's pretty handy ^^

  • @nayanjha5375
    @nayanjha5375 3 роки тому +1

    Thanking Tim

  • @muktadirulislammahi6542
    @muktadirulislammahi6542 3 роки тому

    Though I knew much of those, It was quite helpful!!!
    Thanks tim!❣️

  • @AmodeusR
    @AmodeusR Рік тому

    The unpacking lacked a little bit of clarity. In javascript, it's called destructuring and, when destructuring an object you need to wrap it with curly braces, and refer to the object key, in which you get the value from it. You didn't clearly explain, in python we do the same, with the olny caveat that it returns the keys, not the values and we'd need to do dict.items() to get the items?

  • @RashidKhan-je8ys
    @RashidKhan-je8ys 3 роки тому

    Thank you

  • @dillonbarnes232
    @dillonbarnes232 3 роки тому +3

    Looks like an awesome video and super useful! *_Also, was the first comment._*

  • @peasant5983
    @peasant5983 2 роки тому

    cool stuff, thanks Tim

  • @elatedbento
    @elatedbento 2 роки тому

    What is that gorgeous colorscheme?

  • @deldarel
    @deldarel 2 роки тому

    so itertools chain is like a zip generator?

  • @mywebuniversity
    @mywebuniversity 2 роки тому

    Very good 👍

  • @MrJamestreynolds
    @MrJamestreynolds Рік тому

    I'd like to know how Tim gets the execution duration of the script at the bottom of the terminal. Is this a built in feature of vscode? Ie: "Finished in xxxxms"

  • @Darkev77
    @Darkev77 3 роки тому

    Is there a way to unpack an iterable with an unknown number of entries? Or is it necessary to know the number of elements before hand?

  • @MH-oc4de
    @MH-oc4de 2 роки тому

    I know you're just trying to find an example to make a point, but .... The right way to check for element in a list is:
    if target in search:
    // do something
    else:
    ...

  • @DrDeuteron
    @DrDeuteron 2 роки тому

    12:50 is
    x = int(2 > 3)
    too clever for python?

  • @not_vinkami
    @not_vinkami 3 роки тому +1

    I'm surprised that you didn't talk about enumerate(lst)

  • @amit12000
    @amit12000 3 роки тому

    Nice video Tim

  • @sorachii8932
    @sorachii8932 3 роки тому +1

    This was awesome. I consider myself an advanced python programmer yet I found valuable information in this video. Thanks a lot👍🏾

  • @wesleydcm
    @wesleydcm 3 роки тому

    Very util! Thanks!

  • @MakarovAlex98
    @MakarovAlex98 3 роки тому

    Hey, Tim, hey!
    Thank you very much for your videos and tutorials. You make very useful content. Please give advice on what books and resources to use to study Python. I want to learn how to create bots and applications on a python. I’d really appreciate your help

  • @codershorts
    @codershorts 3 роки тому

    What’s difference in itertools chain and list extend ?

  • @kostiantynjohanlanguedoc3155
    @kostiantynjohanlanguedoc3155 3 роки тому

    Thank You 😊 very much...