5 Common Python Mistakes and How to Fix Them

Поделиться
HTML-код
  • Опубликовано: 16 янв 2025

Комментарии • 214

  • @coreyms
    @coreyms  5 лет назад +120

    If my voice sounds a bit off then it's because I'm getting over a cold. I hope everyone finds this overview of these common mistakes useful. To save you time, I put a list of the common mistakes 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/coreyschafer2

    • @akashshetty6422
      @akashshetty6422 5 лет назад +4

      Take care sensei !

    • @sandeepvk
      @sandeepvk 5 лет назад +3

      Kindly make these videos shorter max 15 min. I can assure you more views

    • @danielturunen7237
      @danielturunen7237 5 лет назад +1

      THANK YOU! I surmised that tabs and spaces don't mix because i'm just starting out with programming and in spite of looking I could not find a definitive anser to this question. It would have saved me a metric ton of headache had you made this video sooner so, FYI, its YOUR fault that my head hurts so much! :) Thanks! :D

    • @richaverma320
      @richaverma320 5 лет назад

      hey your voice is perfect but you increased the speed and it is creating problem

    • @coreyms
      @coreyms  5 лет назад +5

      @@richaverma320 I didn't increase the speed. I just naturally talk a bit fast

  • @archstampton5910
    @archstampton5910 5 лет назад +152

    "Default arguments are only executed once when the function is declared " You made my day, thanks Corey !

    • @nosahama
      @nosahama 5 лет назад +17

      Not when the function was declared, but when it was first run/called.

    • @gildaslefur2435
      @gildaslefur2435 4 года назад +2

      @@nosahama yes, thank you. This makes more sense!
      [Edit] Sorry for ressurecting this 1-year old comment.

    • @ReddoX30
      @ReddoX30 4 года назад

      Yes, it helped me from a lot of trouble!
      Thanks Corey!

    • @sakon4466
      @sakon4466 4 года назад

      lol

  • @alexweissnicht9545
    @alexweissnicht9545 5 лет назад +301

    For mobile users:
    1) Indentation and Spaces - 0:45
    2) Naming Conflicts - 4:12
    3) Mutable Default Args - 10:05
    4) Exhausting Iterators - 16:35
    5) Importing with * - 22:13

    • @coreyms
      @coreyms  5 лет назад +35

      Mind if I ask you how you did that? I didn't know I could get clickable timestamps on mobile.

    • @alexweissnicht9545
      @alexweissnicht9545 5 лет назад +15

      @@coreyms They are only clickable in the comment section, the ones in the description don't work 🙃

    • @alexweissnicht9545
      @alexweissnicht9545 5 лет назад +4

      @@coreyms Besides that they are the same

    • @coreyms
      @coreyms  5 лет назад +56

      Ah, interesting. Thanks for the reply! I'll be sure to add description timestamps to my pinned comments from here on out. I appreciate the tip.

    • @alexweissnicht9545
      @alexweissnicht9545 5 лет назад +18

      @@coreyms I appreciate the content🙃

  • @kaydenstevens5193
    @kaydenstevens5193 4 года назад +29

    Dude your channel is fantastic. I've taken two college courses so far where we used python and I always wished the professors would teach us the fundamental tricks of the trade. They didn't. They only taught about the big concepts like Encapsulation, Inheritance, Polymorphism, Algorithms, Data Structures etc.. which is obviously important to learn but I feel like the basics are so critical yet overlooked in the curriculum. Thankfully your channel is available for us to learn. Thanks!

  • @farleyfarleyfarley
    @farleyfarleyfarley 5 лет назад +47

    Thanks for all the work you're putting in on these lessons, Corey! I love seeing a RUclips alert that you've posted a new video.

  • @FirdausAziz
    @FirdausAziz 5 лет назад +16

    Thanks Corey. That empty list as default argument lesson is really great. I appreciate it very much.

  • @markjago2269
    @markjago2269 4 года назад +7

    Great video, but there is a subtle fallacy: in Mutable Default Args at 13:39, Corey says, incorrectly, that you can equivalently write "if not emp_list" instead of "if emp_list is None". With the "if not emp_list", if we feed the add_employee function with an empty list emps, the name will not be appended to emps. Thus, "... is None" and "not ..." are not always interchangeable.

    • @ememmeme8722
      @ememmeme8722 4 года назад +1

      blasphemy! who sent you infidel?

  • @noxlaoy5119
    @noxlaoy5119 5 лет назад +3

    Pitfall No. 2 was literally me a couple of weeks ago! :) I tried out a package and _of course_ I named my testing script file after the package itself!
    Thanks for making this video, I really appreciate your effort. And it shows: this video feels concise but still in-depth; I can easily follow your thoughts and arguments. This is one of the best tutorials videos I have seen so far. This very video is what makes youtube such an amazing place!

  • @toki_doki
    @toki_doki 5 лет назад +8

    That naming conflict issue caused me so much confusion in the past. Nice tips Corey.

  • @MarcMcRae
    @MarcMcRae 4 года назад +3

    Always learn something new from your videos, even stuff I didn't know I wanted..lol. Great videos Corey mate!

  • @garydunken7934
    @garydunken7934 5 лет назад +3

    Nice, I love it. I'm learning new things in Python all the time. Please keep up these videos. Thanks Corey.

  • @erikjohnson9112
    @erikjohnson9112 5 лет назад +7

    When I saw this title I was going to suggest mutability understanding/awareness, especially with regards to default values. Bingo, it already made your list. Nice.
    As a programmer coming from C++ and learning Python later, this was the most important unexpected thing I learned from the Learning Python book (a good book for learning Python from a single book).

    • @erikjohnson9112
      @erikjohnson9112 5 лет назад +1

      Also I've been away from Python for a bit and only lightly used generators so exhausted generators related to changes from Py2 to Py3 was new info for me.

  • @narendrasudrik5073
    @narendrasudrik5073 3 года назад +1

    You are excellent teacher Corey. Many thanks for crisp and clear information with apt words.

  • @sarthakvashisth9817
    @sarthakvashisth9817 5 лет назад +2

    Awesome video Corey. I just got the notification and watched it. Your videos are very good. I just shared your channel link to all my colleagues and friends who truly wanted to learn python. Keep up the good work brother. ..!

  • @edydon
    @edydon 3 года назад +1

    Great work - the mutable default argument issue is commonly asked on quizzes

  • @masterlifestyle4591
    @masterlifestyle4591 4 года назад +1

    Love the video Corey keep them coming!

  • @juangamboa2646
    @juangamboa2646 4 года назад +1

    Thanks Corey, your videos have helped me to build clear python foundations. I always get to your channel for clarification and learning! Excellent work

  • @finix7419
    @finix7419 2 года назад

    thank you ,been a short sweet ride with your beginner tutorial and already feel like I learned alot ,now time to move to the longer 143 part playlist !

  • @melkoss92
    @melkoss92 3 года назад

    Really useful stuff! Thanks Corey!

  • @ItzSenaCrazy
    @ItzSenaCrazy 5 лет назад +2

    Nice video!!
    Continue with this quality.
    Thank you!

  • @MustbeTheBassest
    @MustbeTheBassest 4 года назад +1

    Wow, this was amazingly helpful!
    I had no idea about that default args problem...
    And the iterator exhaustion issue seems super helpful for future hair ripping sessions!

  • @MrTigerstyle80
    @MrTigerstyle80 5 лет назад +3

    You should cover circular import dependencies in one of your videos. That’s always a good refresher.

  • @olha_
    @olha_ 5 лет назад +2

    This cleared up some things for me, thanks so much Corey!

  • @vivac4893
    @vivac4893 2 года назад

    this video you made is one of best to resolving our problems during learning your course

  • @gabriel_felippe
    @gabriel_felippe 5 лет назад +1

    Always the best! Thank you Corey!

  • @JoshyHJoshy
    @JoshyHJoshy 5 лет назад +1

    Great tutorial. Thank you Corey.

  • @Arrowtake
    @Arrowtake 5 лет назад +1

    Yet another Great video. Always learning somthing new. Thanks!

  • @ShuiYueSketchbook
    @ShuiYueSketchbook 4 года назад

    You're really helping me out! I am super grateful!

  • @khaled-dz8357
    @khaled-dz8357 5 месяцев назад

    the best pythonist ever. thank you corey

  • @samehmahmoud7
    @samehmahmoud7 5 лет назад +1

    Thanks Corey, useful video

  • @jingyuchang1885
    @jingyuchang1885 5 лет назад +2

    I did notice your voice is different than normal. I was thinking it was my computer's issue. Well, great video again!! Hope you get better soon!!

  • @zprelude9935
    @zprelude9935 4 года назад

    Impressive to watch these tips which I'm not aware so long time..especially naming convention potential risks...Thank you !

  • @squigglyunicorn7777
    @squigglyunicorn7777 4 года назад +1

    Thank you, very helpful!

  • @neilfpv
    @neilfpv 4 года назад +1

    Amazing gotchas! Thank you sir!

  • @jaikishank
    @jaikishank 4 года назад

    Thanks . The tips were quite useful.

  • @codegeek8256
    @codegeek8256 5 лет назад +1

    I love your videos...please keep them coming they are very helpful

  • @truthhorizon5851
    @truthhorizon5851 4 года назад

    Thank you very much .This was very helpful to me.

  • @marco.nascimento
    @marco.nascimento 5 лет назад +1

    Great tips!!

  • @nurashams4093
    @nurashams4093 5 лет назад +4

    thank u for uploading...i like ur django series....besides ur presentation and voice is so much standard for an ideal lecturer. could u plz upload some video on pygame, threading and multiprocessing.?..a new series may be....

  • @gauthamambethkar4483
    @gauthamambethkar4483 4 года назад

    Thanks corey. I had this problem of saving the file name same as the module in standard library and could not find out what the error is. Thanks again for saving my time

  • @alapierre1
    @alapierre1 3 года назад +4

    When a function expects an argument to be a reference to a function and you mistakenly add ()’s to the function name which then calls the function, it can be difficult to find the problem in your code.

  • @bdafeesh
    @bdafeesh 5 лет назад +2

    Woow I didn't know about the default argument one. That's bizarre behavior you don't see in other languages...I need to go back and check some things now lol

  • @Talon_24
    @Talon_24 5 лет назад +1

    Stumbled aboud the second one a while ago, took waay too long to figure out what happened there..
    asterisk-import: another example that can happen is if you import sys and os, both have a 'path' member, these two might be found a little more often than glob and html
    Thanks for the video! :D

  • @baconsledge
    @baconsledge 5 лет назад +1

    great tips!

  • @Victor_Marius
    @Victor_Marius 5 лет назад +1

    Thanks a lot for that setting! I thought tabs were already translated to spaces (at least when writing html and js). Now I don't have to worry about tabs in python neither

  • @zabiullahs.z9867
    @zabiullahs.z9867 4 года назад +1

    I would be the happiest man in the world if you make it a series

  • @therimalaya
    @therimalaya 5 лет назад

    Looks simple but could give a lot of headaches. Thanks for the video.

  • @orisphera
    @orisphera 3 года назад

    18:20 I would also say that unzipping in a for loop (for name, hero in identities) doesn't work in Python 2 either

  • @morethanprogrammer7594
    @morethanprogrammer7594 5 лет назад +2

    This is the best resource for improving at Python.
    PS. Maybe a vid on practice interview questions in Python? Very relevant at the moment (for me haha).
    Thanks once again!

    • @coreyms
      @coreyms  5 лет назад +2

      I have an older video on Python Interview Tips. You can find it by searching my channel. I really would like to make an update video though, because I feel like it's not one of my better videos. I would definitely do it differently if I remade it today. I do plan on making one at some point in the next couple of months though.

    • @morethanprogrammer7594
      @morethanprogrammer7594 5 лет назад

      ​@@coreyms Thanks for the reply. I've searched the channel for "Interview questions" I believe, saw that video there yeah, but didn't watch it yet.
      I was thinking more actual practice questions, but again maybe you have that in the vid. I'm gonna watch that one for now.

  • @mr.anderson5393
    @mr.anderson5393 4 года назад +1

    Good day sir. Do you have any videos on string , bytes, encoding , and decoding?

  • @peluso_palit
    @peluso_palit 5 лет назад +1

    Thanks for Skillshare. I just signing up to explore the courses :)

  • @JohnPurcell
    @JohnPurcell 5 лет назад +1

    Learned a bunch! Thanks!
    A question about default arguments. Overuse of NoneType causes a lot of bugs in my team's code. We've insisted in many places to initialize optional args to avoid type switching. I was not aware of the buggy behavior you outlined, but the `if val is None` pattern adds a lot of boilerplate for more complex functions. Do you have any more advanced notes on how to keep my mid-level pythonistas from getting burned?

  • @mahmedtuny8174
    @mahmedtuny8174 5 лет назад +1

    Thanks

  • @lindaphan9234
    @lindaphan9234 5 лет назад

    Thank you so much!

  • @s.baskaravishnu22
    @s.baskaravishnu22 4 года назад

    Many thanks

  • @PanicProvisions
    @PanicProvisions 5 лет назад +1

    Aaaargh, you bastard. I paused the video at the first example and was staring at the code, wondering where the hell the problem was supposed to be! xD

  • @TrumanBurbonk
    @TrumanBurbonk 5 лет назад +2

    Great video! I should watch more of Corey's videos.

  • @dalchemistt7
    @dalchemistt7 5 лет назад

    Naming your string as str or a list of things as list is a sure shot way of getting crazy...

  • @clayy.walker2108
    @clayy.walker2108 4 года назад

    it's useful thanks

  • @buuqmarabo2778
    @buuqmarabo2778 5 лет назад +1

    thanks!

  • @ivanabregu1
    @ivanabregu1 5 лет назад

    Thanks man, did you do some video about PEP8?

  • @hoefkensj
    @hoefkensj 2 года назад

    the weird thing is that i often use default mutable (kw)arguments exactly for that reason, the "data" stays with the function, but it "remembers" its past runs, by the first part i mean it saves you having to pass a variable around form one function to another wich can be a hasle to do if the function is getting called in multple places but every call needs to be aware of what happened before , the goto way of negotiating this would be to use a global but indeed globals.... and theire problems , one of them being you dontalways think to check for them or remember/find everywhere they are set or changed, but using a mutable (kw)argument is always there with the function definition. (sidenote : i avoid writing classes for stuff that shouldnt be a class or even stuff that can be done without a class like the plague wich wich would be the goto way of OOP-style programmers instead of globals)

  • @baspass8578
    @baspass8578 5 лет назад

    Hi Corey, please make video on multiple inheritance and common problems associated with it

  • @nagaranjith9147
    @nagaranjith9147 5 лет назад +1

    Hello Corey... Could you please make videos on data structures and algorithms

  • @shaiavraham2910
    @shaiavraham2910 5 лет назад

    I just realized that help actually prints the help messages and doesn't return them as a string because it printed None on 25:50. Good to know.
    Also, you could have used to "from os import *" and show how reading a file fails because of the clash with os.open. Good video anyways.

  • @MmmBopsPops
    @MmmBopsPops 5 лет назад

    14:52 - Wow, that is REALLY annoying. Coming from PHP, this would have driven me mad, and probably still will! Thanks for sharing.

  • @சதீஷ்கண்ணன்
    @சதீஷ்கண்ணன் 5 лет назад +1

    I would like to know some channel teaches Java as neat as Corey does here in Python.. 👏👏
    People can suggest your options pls.. 😊😊

  • @shanvantharunmozhi
    @shanvantharunmozhi 3 года назад

    the list thing was very useful

  • @angeljiminian5305
    @angeljiminian5305 5 лет назад

    Thank you for your videos.
    I am about 1 1/2 months new to Python. I saw this video and went back to look at some of the self-learning I've been doing. I found a way, actually, I am sure I copied some code and modified it today to see if it would work. I can't tell you why it works but it works. I guess I better figure out why it works so I can feel confident in coding.
    I am using Pycharm latest version. I am using Python 3.7 on a Windows 10 machine.
    import time
    from datetime import datetime
    def display_time():
    t = time.strftime('%Y-%m-%d (%I:%M:%S %p)')
    print(t)
    time.sleep(1)
    display_time()
    time.sleep(1)
    display_time()
    time.sleep(1)
    display_time()
    My result was the result you got after you fixed it.
    Please, some smart person explain why it works, I don't have the knowledge to do it.

    • @coreyms
      @coreyms  5 лет назад

      You’re not using arguments to your function there, so everything within the function will get run every time the function is run.

    • @angeljiminian5305
      @angeljiminian5305 5 лет назад

      @@coreyms is it pythonic to do it the way I did it or is it frowned on?

  • @ruslash
    @ruslash 2 года назад

    u said that tabs cannot b used for indents but in yr code it says that indentation error was found in line 6 which was the line u used spaces for indentation? 2:20

  • @RK_97
    @RK_97 4 года назад

    Hi Corey, is there a Numpy playlist in your contents, i couldn't find one in your website and in youtube

  • @luis96xd
    @luis96xd 5 лет назад +1

    This is and excellent video, well explained, Thanks!

  • @omar72999
    @omar72999 5 лет назад

    One of the problems I faced early on was that I was installing packages using PIP, and then when I would try to import those packages with VS Code it'd tell me that these packages were not installed. After doing some digging I ended up finding that I was installing those packages for an older version of Python (3.6), and trying to compile my code with a 3.7 interrupter. Maybe you should mention this in one of your future videos. I feel like this could be an issue that some people will struggle with for a while.

    • @coreyms
      @coreyms  5 лет назад +1

      Hey there. Good point. I have done a video on that topic “PATH issues”. But you’re right, it would be good to have pointed that out a bit more

    • @omar72999
      @omar72999 5 лет назад

      Corey Schafer oh my apologies. I didn’t know that there was already a video on this topic. Thank you I’ll give it a watch!

  • @jims408
    @jims408 5 лет назад +1

    Very helpful, thanks for sharing! I've been using Python for years, mostly v2 and this helps push me to switch 100% to Python v3 so I can curb my old bad habits.

  • @playgoods
    @playgoods 5 лет назад +1

    thank you :)

  • @prabhathkota107
    @prabhathkota107 5 лет назад +2

    Default arguments was a surprise, good one 👍

  • @Evangelistabryanantigua
    @Evangelistabryanantigua 5 лет назад +1

    Hey I created a simple turtle screen but when the screen pops up is says not responding. Any advice?

  • @Pecos0
    @Pecos0 5 лет назад +1

    Good stuff. Only criticism is that it's Spider-Man. ;)

  • @viswanathbodapati5711
    @viswanathbodapati5711 3 года назад

    Hey Dude.!! Can u please help to sort a list in an ascending order ..with taking two loops...

  • @abail7010
    @abail7010 5 лет назад +2

    Wow! I actually have a developer job since 5-6 months and the thing with the default list screwed me up 😂
    Great video, you’re really helping people out!

    • @Communist-Doge
      @Communist-Doge 2 года назад

      How's your job going today, 3 years later? :D

  • @Sam-uh8lb
    @Sam-uh8lb 3 года назад

    I didn't understood what program you suggest to help us checking the errors

  • @SusanAmberBruce
    @SusanAmberBruce 5 лет назад

    Can you explain how to set up an exit function, to exit from a running python3 program from a users input (say shift plus key combination) without raising an error and giving a clean exit? I am thinking about a program running in idle.

  • @aaronfisher5989
    @aaronfisher5989 3 года назад

    is there a command that deletes all indentation so you can re-indent, regardless of editor?

  • @mrmuranga
    @mrmuranga 5 лет назад +1

    useful

  • @reef2005
    @reef2005 4 года назад

    Regarding the emp_list=[], it's run once. But why its value is kept for the next run? This is a variable that is local to the function and should disappear at end of each run of the function.

    • @trueriver1950
      @trueriver1950 3 года назад

      Two points that interact in ways that seem quirky till you "get" the Python idiom.
      The default value is remembered from creation of the function.
      The default given and remembered is mutable: that means if you change the value you have changed the default.
      In C terms think of it as the address which at the time of creation points to an empty linked list. If you add to the list the address remains the same but the contents vary. The implentation is not quite the same, but that's the basic idea.
      That idea is helpful in making the transition from C (at least I found it so) but try to move on as soon as you can to thinking in Python terms: that that's just what mutable objects do: a mutable object can have different values over time.

  • @karanparekh2329
    @karanparekh2329 5 лет назад +2

    Liked before "Hey there..."

  • @isbestlizard
    @isbestlizard 4 года назад

    don't use 'if not emp_list:' that would also be true if you passed an empty list, meaning it would ignore an empty list you passed in, and always create a new one itself which might be surprising

  • @736939
    @736939 5 лет назад +8

    Corey, please start Scikit-Learn tutorials!

    • @gh0oo
      @gh0oo 5 лет назад

      Please don't. There are millions of tutorials on Scikit-Learn and it is likely not his field. Please keep doing your awesome Python and devops oriented tutorials which you're perfect at. I have personally learnt so much from you.

    • @736939
      @736939 5 лет назад

      @@gh0oo there are million tutorials of Python, but everybody looking for Corey's tutorials!

  • @abhiganta
    @abhiganta 5 лет назад

    Saved me from too much frustration...

  • @NathanTallack
    @NathanTallack 5 лет назад

    Mutable default args make recursion memoization nice and easy. ;)

    • @trueriver1950
      @trueriver1950 3 года назад

      They also make it easy to get unintended recursive effects...

  • @ginopornasdoro3081
    @ginopornasdoro3081 5 лет назад

    can you make tutorial about getting certificate in django heroku deployment?

  • @leophysphile2898
    @leophysphile2898 5 лет назад

    I have one question about the default argument. Why it reset the list to none each time you run the function?it contradicts with what you said that it runs the default argument one and only once.

    • @coreyms
      @coreyms  5 лет назад +1

      It is still setting the values to the default value every time, but they are only evaluated once. So if it is set to a value like None then it will get set to None by default if no arguments are provided. But if it's set to an empty list then it will create that new list once, and then it will get set to that same list by default if no arguments are provided. Since lists are mutable, you will be able to see that original list being modified. You won't be able to tell with immutable data types because they can't be modified.
      I really like this explanation in this StackOverflow answer... I should have incorporated it into the explanation:
      stackoverflow.com/questions/10676729/why-does-using-arg-none-fix-pythons-mutable-default-argument-issue

  • @jamesharushi409
    @jamesharushi409 5 лет назад

    17:01 wade isn’t a hero he’s a villain who goes after other villains

    • @g.deepakkrishnaa3847
      @g.deepakkrishnaa3847 3 года назад +1

      Wade aka deadpool is an anti hero. So , he's technically still a hero

  • @vahidx1265
    @vahidx1265 7 месяцев назад

    Great

  • @kareemjeiroudi1964
    @kareemjeiroudi1964 4 года назад

    Can you make a video about naming variables - programmer's hardest challenge 😅😂?

  • @inline-SGT
    @inline-SGT 5 лет назад

    @Corey Schafer i followed your sublime 3 steup video and setup as it mention but whenver i run code without any import statement i get error like this
    /usr/local/bin/python3: can't find '__main__' module in ''
    please help me to fix

  • @Kemachris
    @Kemachris 4 года назад

    haha i ran into that second one, was playing around with the random library, and saved the file as random, thought i broke something was about to uninstall the whole thing until i found this answer.

  • @dennisalabi
    @dennisalabi 5 лет назад

    Thanks a lot for the effort you put into your quality videos, your teaching style is awesome but please can you do something on Django e-commerce website?

  • @DrFlox
    @DrFlox 5 лет назад

    I still don't understand the mistake with default arguments...
    emp_list is supposed to be a local variable of that function, how can it survive multiple function calls?

    • @coreyms
      @coreyms  5 лет назад

      There is still some confusion about this from several people. Perhaps I will make a clarification video and show this more in-depth.

    • @Pulsar77
      @Pulsar77 5 лет назад

      The default value is stored at a memory address that is created only once, when the function is declared. Every time you call the function, you create a new local variable emp_list, but it will always point to that particular memory address by default. The local variable doesn't survive between function calls, but the default value that it points to does.

  • @xaco56
    @xaco56 4 года назад

    Instead of capturing the iterator items in a list, another way to access the items again is to call zip again.