10 Python Tips and Tricks For Writing Better Code

Поділитися
Вставка
  • Опубліковано 24 бер 2019
  • This video is sponsored by Skillshare. Be one of the first 500 people to sign up with this link and get 2 free months of Skillshare Premium:
    skl.sh/coreyschafer
    In this Python Programming video, we will be going over 10 tips and tricks for writing Pythonic code. Here are the timestamps for each topic we will cover...
    1) Ternary Conditionals - 0:34
    2) Underscore Placeholders - 2:13
    3) Context Managers - 4:25
    4) Enumerate - 6:50
    5) Zip - 8:52
    6) Unpacking - 13:02
    7) Setattr/Getattr - 19:08
    8) GetPass - 26:24
    9) Python dash m - 29:18
    10) Help/Dir - 33:17
    Context Managers Tutorial - • Python Tutorial: Conte...
    Sys.path Tutorial - • Python Tutorial for Be...
    ✅ Support My Channel Through Patreon:
    / coreyms
    ✅ Become a Channel Member:
    / @coreyms
    ✅ One-Time Contribution Through PayPal:
    goo.gl/649HFY
    ✅ Cryptocurrency Donations:
    Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
    Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
    Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
    ✅ Corey's Public Amazon Wishlist
    a.co/inIyro1
    ✅ Equipment I Use and Books I Recommend:
    www.amazon.com/shop/coreyschafer
    ▶️ You Can Find Me On:
    My Website - coreyms.com/
    My Second Channel - / coreymschafer
    Facebook - / coreymschafer
    Twitter - / coreymschafer
    Instagram - / coreymschafer
    #Python

КОМЕНТАРІ • 986

  • @coreyms
    @coreyms  5 років тому +434

    To save you time, I put a list of all of the tips and the timestamps for each in the description of the video. And again, this video was sponsored by Skillshare. If you'd like to check out Skillshare with 2 free months of Skillshare Premium, you can use my link here:
    skl.sh/coreyschafer

    • @knownwolf
      @knownwolf 5 років тому +4

      Thank you for the timestamps!

    • @mrityunjayahiremath3951
      @mrityunjayahiremath3951 5 років тому

      Hey Corey, please make a video on MRO (Method Resolution Order) in Python

    • @almohtarifchannel6702
      @almohtarifchannel6702 5 років тому

      Corey Schafer how we can loop 2 lists witout same lenth

    • @coreyms
      @coreyms  5 років тому +12

      Like I was saying in the video, zip will stop after the shortest list is depleted. zip_longest from the itertools module will go to the end of the longest list and have filler values for the shorter lists. Watch my video on itertools to see an example.

    • @dharaneeshwaranravichandra6159
      @dharaneeshwaranravichandra6159 5 років тому

      Please make a video on "How Django processes a request
      ?"

  • @expat2010
    @expat2010 3 роки тому +890

    I'm 79 years old, been studying Python for about 6 months, and I got to tell you I get so damned exited with the stuff I learn from you - no other teacher, paid or free, has come close. I just hate to tear myself away from videos like this! I don't have a lot of money, but I'm gonna send you a few bucks - buy a toy for your beautiful dog. :)

    • @deeshu3456
      @deeshu3456 3 роки тому +38

      God bless you... ☺️✨. Shine on...

    • @tobycherry47
      @tobycherry47 3 роки тому +166

      @@joshkanyinda5569 tf is wrong with you. he's doing whatever he wants, making the most of life, unlike you obviously.

    • @NetEng89
      @NetEng89 3 роки тому +146

      @@joshkanyinda5569 What a useless comment...

    • @ididntgetthejoke3587
      @ididntgetthejoke3587 3 роки тому +31

      @@NetEng89 agreed

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

      tf is wrong with you

  • @BohdanKokovych
    @BohdanKokovych 4 роки тому +476

    Enjoy!
    1) 0:34 - ternary operator ;
    2) 2:13 - Underscores in Numeric Literals + about f-string;
    3) 4:29 - context manager ;
    4) 6:49 - enumerate function ;
    5) 9:01 - zip function ;
    6) 13:46 - unpacking tuples (use asterisk and underscore);
    7) 19:11 - getting and setting attributes to object (setattr, getattr );
    8) 26:24 - getpass instead input (read from keyboard) ;
    9) 29:22 - run python with dash m in terminal (python -m ...);
    10) 32:32 - built-in functions (help, dir etc)

    • @eujnia
      @eujnia 4 роки тому

      loviuuuuuuuuu

    • @ardonjr
      @ardonjr 4 роки тому +16

      @@eujnia This is literaly in the description

    • @eujnia
      @eujnia 4 роки тому +6

      @@ardonjr can u act like a normal person and just ignore me?

    • @za6604
      @za6604 4 роки тому +2

      @@ardonjr Mobile users can't click the timestamps in the description.

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

      @@za6604 Uh yes I can? I'm typing this from my mobile phone..

  • @Demonslay335
    @Demonslay335 5 років тому +759

    Dude, underscores can be used to delim numbers!? Literally life-changing tip, thank you!

    • @MykhailoK-sh6sm
      @MykhailoK-sh6sm 5 років тому +5

      Same XDDD

    • @zahirjacobs716
      @zahirjacobs716 5 років тому +22

      only python 3.6+

    • @elghazouanimohammed6639
      @elghazouanimohammed6639 5 років тому +3

      Can you give me an exemple where you need that please ?

    • @Demonslay335
      @Demonslay335 5 років тому +28

      @@elghazouanimohammed6639 I deal with cryptography and thus large numbers a lot. Having some human readable constants is nice, such as 2**32 if it were written out. Sometimes it even just helps for understanding "oh, it's like a billion" without having to break out the cursor to count each zero.

    • @bsmaheshkumar5328
      @bsmaheshkumar5328 4 роки тому +1

      It is used to separate the multiple string. Ex. full_name = 'John'

  • @wolfisraging
    @wolfisraging 5 років тому +255

    My favourite Channel for learning python

    • @fefemorte
      @fefemorte 5 років тому +1

      Mine too, extremely good explanations

    • @medi7573
      @medi7573 5 років тому +3

      And best on UA-cam

    • @sandeepvk
      @sandeepvk 5 років тому +3

      Indeed ! he is quick to the point and no baloney

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

      @@dr.rijjaayaz8160 Sorry, but the narrative voice is not nearly as pleasant as Corey Schafer's

  • @EVGrinds
    @EVGrinds 4 роки тому +23

    I've been coding in Python over a year now, and was expecting to only see one or two new things. BUT I LEARNED SO MUCH MORE !
    Thank You!

  • @brainplot
    @brainplot 5 років тому +24

    As a Python beginner - but not a beginner in programming - this video was great! Awesome explanation, thank you!

  • @petursteinn9718
    @petursteinn9718 5 років тому +52

    Your content is so unbelievably good! Thank you so much for all that you've taught me.
    You speak so clearly, describe concepts so well, demonstrate with perfection, you're incredible!

  • @blazecoolman
    @blazecoolman 5 років тому +23

    Corey, just thanks, man. That is all I can say for the content that you make here. I have learned so much from you

    • @coreyms
      @coreyms  5 років тому +1

      Thanks for the comment! Glad you've learned a lot!

  • @jerrymiller1590
    @jerrymiller1590 4 роки тому +9

    You are the best teacher ever Corey! Learning tips and tricks like these can save a programmer a lot of time, so I really appreciate videos like these

  • @caio-jl6qw
    @caio-jl6qw 5 років тому +5

    Those tips on unpacking were mindblowing to me; i never knew we could use those syntaxes. Great video!

  • @isidaruk1408
    @isidaruk1408 5 років тому +25

    The work you do is priceless. Thank you so much, Corey! You are really wonderful teacher, who keeps to inspire me and others to move toward being a better developers.

    • @coreyms
      @coreyms  5 років тому +2

      Thanks for the kind words!

  • @TAlfonsoK
    @TAlfonsoK 5 років тому +7

    Your videos are amazing. So well explained. I feel I learn to code better every time I watch your videos, even if I already know the modules you introduce. Thank you so much!

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

    His tutorials are exponentially much better than most of the Udemy & Coursera courses.
    You're doing a Great job Corey you're having an impact ❤️ Keep it up.

  • @JamBear
    @JamBear 4 роки тому +2

    This is incredible. I absolutely love your delivery - you are such a good speaker. I've used your videos as a refresher and I want to thank you so much for your great archive of helpful videos!

  • @hungvunguyen8929
    @hungvunguyen8929 5 років тому +15

    As a new Python learner, I find your videos are the most interesting, clear and throughout! Glad that you still making python videos. Thank you!

  • @smashies118
    @smashies118 5 років тому +43

    Wow. Figured I'd watch a random python tips video instead of memes and found exactly what I needed for one of my projects. Thanks man! Awesome vid!

  • @Ridistrict
    @Ridistrict 5 років тому +6

    Thank you for everything you do, Corey. Once again, I've learned an absurd amount of information in less than an hour.

  • @betaman2009
    @betaman2009 4 роки тому +2

    Excellent video. I learned some of these the hard way writing spaghetti code and I think it's refreshing to have a breakdown with examples like these. Great work!

  • @zperk13
    @zperk13 5 років тому +6

    I've been coding with python for a while, I've messed around with neural networks, yet somehow I didn't know some of these amazing tips. Thanks!

  • @archstampton5910
    @archstampton5910 5 років тому +22

    Fantastic, 40 minutes of my life well spent.

  • @kumarmayank8586
    @kumarmayank8586 5 років тому +23

    The best python teacher ...thanks corey sir..

  • @o.sunsfamily
    @o.sunsfamily 4 роки тому +3

    Reading about those things (as someone who started with C and co) it's easy to forget.
    This video is a great summary of all the helpful functionalities we forget about.

  • @ItzSenaCrazy
    @ItzSenaCrazy 5 років тому +10

    Dude!!
    What a great video!
    Thanks you so much.
    Please, continue bringing more videos

  • @ishpeace4886
    @ishpeace4886 5 років тому +6

    You are the best as usual Corey. Thank you so much for this valuable tutorial, you are really a life saver

  • @realchrishawkes
    @realchrishawkes 5 років тому +87

    Great video, thank you for sharing!

  • @nordy2010
    @nordy2010 5 років тому +2

    Thanks Corey! I'm in the process of learning Python, and this definitely helps. Well done, sir!

  • @md.akib5124
    @md.akib5124 5 років тому +2

    day by day I am falling more and more in love with your teaching style

  • @MegaTheDamir
    @MegaTheDamir 5 років тому +4

    Was sitting yesterday, passing values to object attributes(13of them), and ended up looping around dict and doing this:
    For item in dict:
    obj.attribute=item[‘index1’]
    obj.attribute2=item[‘index2’]
    Wish I knew about setattr...
    Thank you !!!Mr. Schafer

  • @anilkumarverma240
    @anilkumarverma240 5 років тому +3

    Hey! Corey that was great video tutorial . Get to learn lot of new things.
    Happy Pythoning.

  • @ChadInfeld
    @ChadInfeld 5 років тому +2

    Thank you for this one! Exactly what I needed to help advance my code refactoring sessions.

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

    Corey is a genius at knowing what it is we need to know and knowing how to explain it simply and clearly.

  • @brightsideofmaths
    @brightsideofmaths 5 років тому +3

    And I am still learning new cool stuff you can do in Python :) Thank you!

  • @AlexanderDemin
    @AlexanderDemin 5 років тому +3

    A tip about unpacking with the star is great. Thanks a lot.

  • @_indrid_cold_
    @_indrid_cold_ 4 роки тому

    Some of the best Python content out there. Absolutely love learning Python alongside your videos and teachings.

  • @antonlashov7893
    @antonlashov7893 5 років тому +2

    Hey, Corey! Greetings from Mother Russia 👋🏻 Thank you for what you are doing! No water, just a facts with super clear examples. You’re making UA-cam better!

  • @oj43085
    @oj43085 5 років тому +3

    Nice video... I didn't know about the start in enumerate. And that unpacking list bit was really good.

  • @jingyuchang1885
    @jingyuchang1885 5 років тому +3

    Great tips and tricks! The best python instructor!

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

    The quality of this content.. insane. Thanks, Corey.

  • @caseymccray3861
    @caseymccray3861 5 років тому +3

    Your videos are so high quality!
    All the info was very useful specially the dir(). God I wish i knew that earlier xD
    Thank you so much :)
    I followed your django tutorials and now have 4 good personal projects under my belt! Your tutorials were my foundation of Django.
    One thing I'd really want you to cover is unit tests. They seem to be a huge part of companies and I think its an important topic to cover and would be helpful to other viewers as well.
    Thank you so much once again for producing such high quality videos for us

  • @computer_sciences
    @computer_sciences 5 років тому +6

    Thanks Corey your channel isTHE best Channel for learning python
    please if it possible a playlist for Data structure & Algorithms in python

  • @bengbeng2005
    @bengbeng2005 4 роки тому +1

    so far, this is the best python channel i ever saw, best explanation, clean nice voice, thank you mr Corey

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

    Thank you for this trick, this is just I was looking for, and thank you for screen size, because there are tons of videos, where I can't see nothing, but your is great, thank you my friend.

  • @jamesjung4440
    @jamesjung4440 5 років тому +7

    thanks for the free knowledge, from an aspiring developer

  • @williamreames2228
    @williamreames2228 4 роки тому +4

    Although I didn't know about zip (8:52) and it does seem pretty helpful, it looks like you could get an equally organized structure if you use a dictionary instead since the two lists are inherently related and do something like:
    for name, hero in dictionary_name.items():
    In any case, great video and I definitely managed to learn some things

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

    This guys knowledge completely blows other tutorials out of the water!
    I know I’m late to the game Corey but your content is truly amazing thank you from 2023

  • @antonlinden5216
    @antonlinden5216 5 років тому +1

    Brilliant tips for typing code with high quality! New to Python!
    But i worked as a developer for a company for 1 year with AngularJS, I wish people cared more about this type of things!
    Thanks!

  • @minghaotao6259
    @minghaotao6259 5 років тому +3

    Best Python Learning Channel!

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

    Thanks mate. You just saved me tons of hours of hating myself. Great job!

  • @shanepython
    @shanepython 5 років тому +1

    Great tutorial! A weekly '10 tips & tricks' would be VERY welcome and popular. You are an excellent teacher. :)

  • @TheDrewCrawford
    @TheDrewCrawford 4 роки тому

    Excellent information as usual. Definitely picked up some new tricks here. Thanks for all you do Corey. You really have a gift for teaching.

  • @nasreddinehodja5930
    @nasreddinehodja5930 5 років тому +96

    UA-cam : 10 python tips and tricks...
    Me : Leave me alone please
    UA-cam : ...from Corey Schafer
    Me : *Instaclick*

    • @coreyms
      @coreyms  5 років тому +10

      Haha, thanks!

  • @starecekmichal
    @starecekmichal 5 років тому +3

    Great video! Many thanks. Strong emphasis on the statement: You don't have to know everything, but it's more important to be able to find information and use it.

  • @HaileeMiu
    @HaileeMiu 5 років тому +2

    Thanks for the timestamps! Makes for a great reference video to save. And more likely to find when searching

  • @I_Follow_Jesus
    @I_Follow_Jesus 5 років тому +2

    I'm just getting started by taking a couple online courses. Your videos really help to clarify the subject matter. You have the best Python videos on UA-cam.

    • @coreyms
      @coreyms  5 років тому +2

      Thanks! Good luck getting started in programming!

  • @ghazanfarabidi4137
    @ghazanfarabidi4137 5 років тому +3

    Great flow dude..

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

    I've worked with Python for many years, and some of the features mentioned here are only in the most recent versions of python (like f-strings). I work in environments that have older versions of python (even the now obsolete Python2.7) without these features I think it would have been helpful if the version of Python where the feature was introduced was mentioned in the video. This is not meant to be a criticism, this video is great!

  • @Zwiesel66
    @Zwiesel66 4 роки тому +1

    Thank you Corey, your tutorials are simply great. Always spot on with very good examples.

  • @WandererOfWorlds0
    @WandererOfWorlds0 4 роки тому +1

    I've worked with Python for a few years and have seen a lot of code out there but I didn't know all of the tips here. Thanks.

  • @santiagolerin
    @santiagolerin 4 роки тому +11

    First of all, I have to say that I'm very thankful for this videos.
    I am from Argentina and the part of "nosetuid" made me laugh a lot just because I instictibly separated it like "no se tu id" witch is Spanish for "I don't know your id"; I know, is silly, just wanted to share it with the comments people

  • @AundreL
    @AundreL 4 роки тому +7

    The package "Terminus" is a package that allows you to run the command line as a panel in sublime text 3. I personally use it in combination with WSL on my windows pc to simulate a Linux setup without having to run a VM.

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

    Corey, wanted to give a like for every tip&trick out there... Thanks a bunch!!!

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

    I clicked on this video thinking I would learn one or two things, but I've been shocked and impressed by each tip, this was very helpful!

  • @eeshsingh3336
    @eeshsingh3336 5 років тому +16

    Hi, can we please have tips on how can we make our code more reusable and understandable if we're working on a large scale project?
    Thanks. As always, the best tutorials on YT!

    •  5 років тому +4

      Comment your Code and split your Code into small classes and files

  • @aasensios
    @aasensios 4 роки тому +6

    from my_heart import thank
    thank.you('for this brilliant and plain way of explaining python usage')

  • @eduardogamboa9387
    @eduardogamboa9387 5 років тому +2

    Ran into the same setattr problem couple times before, great video!

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

    You got the best tutorials i have found so far thanks for the good work man

  • @cybergen2K
    @cybergen2K 5 років тому +8

    You were sent from heaven.

  • @kantulaev
    @kantulaev 5 років тому +3

    VEry cools zip tips

  • @SHONNER
    @SHONNER 5 років тому +2

    I love these videos you make. They inspire me to try more Python things in my code.

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

    Man, your explanations are so clear and the tips are actually helpful. Thank you very much!

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

    only 4 minutes in and I went off a wikipedia binge tangent that left me knowing everything about large number names in different scales and languages

  • @rohan352
    @rohan352 5 років тому +4

    Will u make a vid on tkinter or any other GUI?

  • @elizabethnjega5958
    @elizabethnjega5958 5 років тому +1

    Corey thanks for all your tutorials videos. Thy are really helpful.. Since I am a self taught without any background of programming..

  • @shegsdev
    @shegsdev 4 роки тому +1

    Just came across this channel and I subscribed right away.
    This is my second video. I love your teaching 👍

  • @dudang5814
    @dudang5814 5 років тому +69

    Just FYI, the underscore number separator only available from Python v3.6+

  • @TinkCSA
    @TinkCSA 5 років тому +4

    nose tuid xD. Exactly the way I read it as well the first time.

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

    The best programming teacher I've found in UA-cam!

  • @azr_sd
    @azr_sd 4 роки тому +2

    we need part2 Corey..It's really an awesome video.. Waiting eagerly for part2,3,4,5, --- infinity .. :) Keep posting awesome stuff Corey.

  • @coreycarter5668
    @coreycarter5668 5 років тому +44

    Have I told....you lately....that I love you? Hahaha

    • @shazkingdom1702
      @shazkingdom1702 5 років тому +1

      hahaha... he deserves the song... 🎶

  • @parkeranderson23
    @parkeranderson23 4 роки тому +9

    maybe this is just me being crazy, but i ALWAYS do loops like this:
    for i in range(len(names)):
    print(i, names[i])
    but idk

    • @Bryan-bh7cy
      @Bryan-bh7cy 4 роки тому

      nub...

    • @jankreitzscheck9914
      @jankreitzscheck9914 4 роки тому

      So do I. I'm not a professional programmer, this works for me, no idea whether it is the ideal way of doing things.

    • @ycliu4046
      @ycliu4046 4 роки тому +1

      This would be faster than enumeration. You could timeit yourself. I would prefer this one to enumeration

    • @krishnakumarshankar4576
      @krishnakumarshankar4576 4 роки тому

      Why can't you do this

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

      @Parker Anderson: your method is correct for sure but is not pythonic. The Python way of doing things tends to simplicity in code writing, hence the enumerate function. Even the fact that you can loop through a list with just "for item in items:" is something really easy to write and understand later.

  • @grmasdfII
    @grmasdfII 4 роки тому +1

    Very good, thank you. Helpful, concise and actually something new. UA-cam is full of Python tips/tricks/how-tos from amateurs that cover the first 5 pages of a good textbook, your tips are actually practically useful to beginners and intermediates alike.

  • @negarshahrokh9727
    @negarshahrokh9727 4 роки тому +1

    Thank you very much for sharing this, as a data analyst using python on a daily basis some of these would make my life a lot easier! :D

  • @sadbadmac
    @sadbadmac 4 роки тому +15

    These are like the new 10 commandments

  • @pepinote14
    @pepinote14 5 років тому +55

    Wait, Clark Kent is Superman?? no way, I've seen him, he weares glasses...

  • @gussta1
    @gussta1 4 роки тому

    I know this is an older video, but I found it really helpful. I'm pretty much making all the programming mistakes you're talking about....now I'm cleaning them all up...thanks.

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

    Thank you so much for setattr and getattr. It is amzing. Changed my whole codes.

  • @KevObispo
    @KevObispo 5 років тому +3

    NOSETUID got me. hahaha

  • @bernardo231
    @bernardo231 5 років тому +11

    how can I give you infinite likes?
    Hello from brazil!!!

  • @officesuperhero9611
    @officesuperhero9611 5 років тому

    Another incredible video. You never ever disappoint, Corey. Well done.

  • @poopcatapult2623
    @poopcatapult2623 5 років тому +1

    These actually helped me quite a lot and you presented the explanations well. Thank you.

  • @jaymeetvasava7668
    @jaymeetvasava7668 4 роки тому +7

    "nose tuid" I am gonna remember it this way now. 😂😂😂😂

  • @kickflipz
    @kickflipz 3 роки тому +6

    "Not all python modules are this thicc."
    Oh just like how not all youtube videos are this thicc? 🙂

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

    Thank you for providing such great content! After a brilliant explanation in the OOP series, I thought nothing else would surprise me :D but this video is from another universe

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

    As always, great stuff Corey!

  • @westfield90
    @westfield90 5 років тому +3

    I appreciate your videos. But there are 4000 UA-cam videos on fundamentals of python, however only about 5 on how to solve issues. When I take a class or an interview they ask me to solve a problem and not syntax questions. I wish there were more videos on how to solve problems so I can see how the logic works and how to approach solving the problem.

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

    The whole time my mind was like:
    *Whaaaaaaaaaaa-*

  • @TonyFlexPromo
    @TonyFlexPromo 5 років тому +2

    Totally awesome video! Thanks Corey!

  • @kelvinvalencio2
    @kelvinvalencio2 5 років тому +1

    I was told that unpacking is going to be very useful. Thanks for the concise video of its usage!!

  • @shashankshekhar624
    @shashankshekhar624 5 років тому +3

    I also saw nose-t-uid