Don't ever write Python code like this

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ธ.ค. 2020
  • My FREE guide on how to become a data engineer:
    karolinasowinska.notion.site/...
    I've spent a lot of time refactoring other people's code, and here's the summary: 6 Python mistakes to avoid!
    Why you should never write your Python code like that? There are a few reasons:
    -it looks unprofessional
    -it makes your code hard to read
    -your colleagues are secretly going to hate you
    -messy code will bite you later, when it starts failing and you have no idea why!
    So, join me on this Python code refactoring journey, and don't ever write Python code like this ever again! :) Don't worry if anything from this video looks overwhleming to you - these are not even Python beginners' mistakes, they are made by beginners and more advanced users of Python alike. But it's time to learn Python best practices if you want to rock! :)
    pep8 style guide: www.python.org/dev/peps/pep-0...
    Find me on instagram:
    @karo_sowinska
    And if you'd like to buy me a cup of coffee... :)
    ko-fi.com/karolina_sowinska
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 1.2K

  • @FenBender01
    @FenBender01 3 ปีที่แล้ว +1059

    Good tips on writing python code. When you edit a video, keep the code on the screen for more time. 2 seconds are not enough to read it.

    • @gelanghaarteweile3048
      @gelanghaarteweile3048 3 ปีที่แล้ว +148

      I know it's obvious but you do know you can pause the video?

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +173

      I prefer keeping the dynamic pace, but perhaps I exaggerated a little! Thanks :)

    • @isidrodeleon673
      @isidrodeleon673 3 ปีที่แล้ว +170

      @@gelanghaarteweile3048 We all know that video can be paused. But it is better to watch the video without pauses and understand it.

    • @dvtech4571
      @dvtech4571 3 ปีที่แล้ว +9

      I think its a niche thing. Most programmers should pause video, cause' we usually finds out our own solution, but non-programmers should ask for the solution, if you want to catch-up more people try to expand your niche 🤗
      Oh and little trick: try subs, even (better) in another lenguaje(s), youtube algorithm should do the rest 🤗
      Thanks for sharing!

    • @giogio182
      @giogio182 3 ปีที่แล้ว +17

      Yes, you can pause but sometimes she talks about something and shows only AFTER. It'd be nice to follow along.
      Also here (7:15) the editing starts before the transition has even ended. I mean, it's an obvious edit but maybe beginners need more details to follow.

  • @blazep5881
    @blazep5881 3 ปีที่แล้ว +649

    5:34 even if you move the variable inside the if __name__=="main" : block it's still a global variable. If you really don't want it to be a global var you'll need to define a main function and use it there and call main inside the if __name__ block.

    • @SkrtlIl
      @SkrtlIl 3 ปีที่แล้ว +47

      Came here to say this. Lots of python programmers mistake that if-block for a main function

    • @HexenzirkelZuluhed
      @HexenzirkelZuluhed 3 ปีที่แล้ว +13

      Upvote, so I don't need to point that out.

    • @tubeincompetence
      @tubeincompetence 2 ปีที่แล้ว +2

      huh.. nice to know. :)

    • @davidcalebpaterson7101
      @davidcalebpaterson7101 2 ปีที่แล้ว +6

      Can someone explain in detail why global variables are "bad" and how to move it to main, I am trying to learn that.

    • @HexenzirkelZuluhed
      @HexenzirkelZuluhed 2 ปีที่แล้ว +35

      @@davidcalebpaterson7101 Global variables contribute to "namespace pollution". You can accidentally reuse common names. Also code is generally more readable when your variable's definition and scope is as narrow as possible. If you use a variable only in one function you *know* it can only affect that piece of code. A global variable *might* affect many other places as well, you need to check to be sure. So it basically it makes your job easier in the long run not to use them if you don't need to.

  • @martijn3151
    @martijn3151 2 ปีที่แล้ว +134

    04:05 in all fairness, replace is not the same as changing the first letter of a string to some other letter. It replaces all occurrences in a string and requires prior knowledge of the value you want to change (in this case 'C'). The proper way to do this, unfortunately very convoluted in Python, is: changedstring = oldstring[:index] + newcharacter + oldstring[index + 1:].

    • @jeroenvermunt3372
      @jeroenvermunt3372 2 ปีที่แล้ว +1

      it's not convoluted if you just add it to a function with the index and new_characters as arguments :)

    • @adityad7602
      @adityad7602 2 ปีที่แล้ว

      True but the string only has one capital letter.
      Which doesn't affect anything else in this specific use case.

    • @daveit1337
      @daveit1337 2 ปีที่แล้ว +23

      Simple as that:
      my_name = 'K' + my_name[1:]

    • @legrinu
      @legrinu 2 ปีที่แล้ว +2

      @@adityad7602 you don't know. And If you know what String your changing the changing is Not neccessary

    • @elmiguel1982
      @elmiguel1982 2 ปีที่แล้ว +2

      Late to the party, but replace isn't convoluted as the documentation is pretty clear. You can pass a third argument to replace which occurrences you wish to replace: 0-N occurrence.
      str.replace(old, new[, count])
      Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
      The issue arrises when trying to replace an occurrence at a particular index. Then you would have to implement a method around the issue at hand.
      - Happy Coding.

  • @anotherbloodyhypocrite960
    @anotherbloodyhypocrite960 2 ปีที่แล้ว +6

    I love this style of video showing code that works and then critiquing it and explaining why it should be changed is a really useful thing to see as a newbie learning code. You've got yourself another subscriber!

  • @apmcd47
    @apmcd47 2 ปีที่แล้ว +42

    The only addition I'd make to your improvements is add a main() function. You mention the variable myAge being a global where it was originally defined, but my_name and my_age are both global variables while they are inside the if block. Also using the 'for ... in' construct isn't so much Pythonic as good programming practice in any scripting language that supports it, as it is easier to read. The only reason to use an index in that way would be to access multiple lists, but Python has the zip() function for such a case.

    • @MasterHigure
      @MasterHigure 2 ปีที่แล้ว +4

      You could also need more explicit access to the index. For those uses, you have the enumerate() function.

  • @minekpo1
    @minekpo1 3 ปีที่แล้ว +33

    5:18 I believe screaming snake case is used for constants, not global variables

    • @katalysmus
      @katalysmus 3 ปีที่แล้ว

      Yeah same

    • @vekyll
      @vekyll 3 ปีที่แล้ว +4

      They aren't that different, or at least they shouldn't be. If you need global names at all, they better be constant. Global variable that actually varies is worst of both worlds. :-]

    • @rawrproductions7961
      @rawrproductions7961 3 ปีที่แล้ว +2

      @@vekyll not necessarily true, here's an excerpt on when they would be useful, "global variables are something that should be used only when needed. I would rather use global variables than using some artificial constructs (like passing pointers around), which would only mask the real intent.
      Some good examples where global variables are used are singleton pattern implementations or register access in embedded systems." They're not the worst of both worlds, sometimes there are real applications to them being modifiable and not only constant. Very few, obviously, but they exist and have their place.

    • @vekyll
      @vekyll 3 ปีที่แล้ว +1

      @@rawrproductions7961 Since you mention "passing pointers around", you obviously aren't talking about Python. Yes, various languages have various criteria.

    • @nialltracey2599
      @nialltracey2599 2 ปีที่แล้ว

      That was my initial understanding too -- C constant conventions indicate to programmers that a variable should be treated as a constant.
      However, someone later pointed out that Python is a shell scripting language, and "screaming snake" is used for environment variables in shell scripting, and they aren't constants.

  • @alejandrocarvajal9068
    @alejandrocarvajal9068 2 ปีที่แล้ว +22

    Great video, I'm here a little late, One of the tips that could improve anyone's skill is to take a look a native functions and modules to avoid reinventing the wheel, most of the time the native modules have faster implementations for a task that you might need to solve.
    Personal tip: if you think you can do a better job and writing a better implementation than the native function, that just shows the lack of understanding and research skills.

    • @karolinasowinska
      @karolinasowinska  2 ปีที่แล้ว +1

      Good tip! You're right

    • @IHaveYourMilk
      @IHaveYourMilk 2 ปีที่แล้ว +4

      Do not agree with the "Personal Tip" for if it was generally true, we would have no new, better libraries at all (everyone would think they cant do better then the old stuff)...
      That said, I do agree that before implementing something (meaningful), you should research what is already done.

  • @sealkeen
    @sealkeen 2 ปีที่แล้ว +18

    6:48 1) Can you assign a value to the variable in the loop while iterating through some collection in a Pytonic Way?
    2) Can you also write a server-side component that deals with database migrations, handling web requests and implements a server API in Python and run the server using only Python?

    • @karolinasowinska
      @karolinasowinska  2 ปีที่แล้ว +21

      Yes you can!
      >>> for count, value in enumerate(values):
      ... print(count, value)

    • @mattacarregui3934
      @mattacarregui3934 2 ปีที่แล้ว

      @@karolinasowinska is the (values) next to enumerate just representing what is in enumerate or is that part of the syntax?

    • @intelchip_x86
      @intelchip_x86 2 ปีที่แล้ว

      @@mattacarregui3934 values is the name of a list, its just used as an example in the comment

    • @rafagd
      @rafagd 2 ปีที่แล้ว

      1) Kinda. You'd need to run a for i in range(len(array)): array[i] = something, or build a new array. Unless you mean a variable that is not the array, which is trivial you just do it.
      2) You mean, write Django?

    • @rafagd
      @rafagd 2 ปีที่แล้ว

      @@mattacarregui3934 enumerate is a function that returns an iterator to your array (named values in the example).
      Each iteration of the loop makes enumerate spit out a pair (index, values[index]).

  • @MartinJaszczuk
    @MartinJaszczuk 2 ปีที่แล้ว +6

    Seriously, these are really good tips. People coming from other languages may have a hard time with the Python way of doing for loops, I know it took me like a month to wrap my head around the current item, since you just name it in the loop and it's never used again. I first tried learning this when I tried to learn modern JavaScript, but didn't fully understand and use it until I revisited the topic in Python.
    Great video! Thank you!

    • @karolinasowinska
      @karolinasowinska  2 ปีที่แล้ว

      Oh yes, the Python loops also weren't very intuitive to me for some time! But now I cherish them haha ;) Thanks!

    • @pxolqopt3597
      @pxolqopt3597 2 ปีที่แล้ว

      I didn't understand function parameters for the longest time... like i used to never use functions and when I did I would misuse the hell out of them

  • @ayva1106
    @ayva1106 2 ปีที่แล้ว +3

    Reading the pep8 I didn't even realize how much stuff I picked up automatically from reading other people's code and didn't even need to think about

  • @kevinmcaleer28
    @kevinmcaleer28 2 ปีที่แล้ว +1

    Great video - Thanks Karolina!

  • @nikogonz1
    @nikogonz1 3 ปีที่แล้ว +6

    hey great video! you should do more of this! I love watching videos about what I should not be doing in python as I'm still learning.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +2

      My pleasure! Enjoy learning, Python is great ;)

  • @lordwelpa
    @lordwelpa 3 ปีที่แล้ว +69

    Also overusing "from X import *" is a plague. It seems to be a very benign thing but it could lead to serious issues. Been there, done it, spent a week debuging my code :)

    • @tobiasbergkvist4520
      @tobiasbergkvist4520 3 ปีที่แล้ว +13

      Actually, this is a great feature when developing your own modules. I often use this from a Jupyter environment while modifying Python-files I'm maintaining myself.
      You want to change a function in some random file (X)? Simply use "from X import *" and copy-paste the specific function into your notebook. Now you know that the redefined function will have all its dependencies from that file available. That also means you can start modifying the function while testing it, without worrying if it tries to call another function from that file.
      On a side-note, I think "from X import *" should never be used for third-party modules that you don't intend on debugging or modifying the source code of. It is a great debugging and development tool - but not something you want to leave in "finished code".

    • @MakeMoneyOnline-AI-ChatGPT
      @MakeMoneyOnline-AI-ChatGPT 3 ปีที่แล้ว +1

      yeah, the disadvantage is when debuging

    • @drheinrich940
      @drheinrich940 3 ปีที่แล้ว +3

      i must admit that i fail to understand why this is a bad habit. Could you elaborate ? I thought it was good practice beaucause it implied lighter library loading.

    • @MakeMoneyOnline-AI-ChatGPT
      @MakeMoneyOnline-AI-ChatGPT 3 ปีที่แล้ว +1

      @@drheinrich940 when you are working on a larger application, especially old codes without proper documentation (comments), it becomes little harder to trace back some codes and it takes more time to figure out which imports supplies a particular class or function, this is from my experience though.

    • @vekyll
      @vekyll 3 ปีที่แล้ว

      _One_ import * per module is ok. (So if you can trace everything else, what you cannot belongs to that module by elimination.) The problem is, it's hard to stop at just one. :-D

  • @alisterware8062
    @alisterware8062 3 ปีที่แล้ว +65

    Pep 8 is a style GUIDE, it is not set in stone although is is a very good guide & one of the 1st things is says is "A Foolish Consistency is the Hobgoblin of Little Minds" if working on your own you can set your own guide for what works best. if you are working as part of a team then you should use the standards of your team.

    • @chaosordeal294
      @chaosordeal294 2 ปีที่แล้ว +3

      A foolish little mind has the consistency of a hobgoblin.

    • @apmcd47
      @apmcd47 2 ปีที่แล้ว

      True, but I suspect that many new projects will use PEP-8 as their style guide rather than use an internal style because they will be using tools like PyLint and SonarLint to check their code for consistency, and it's easier to use the default settings than configure them for yours (unless somebody has a configuration file lying around).

    • @BreetaiZentradi
      @BreetaiZentradi 2 ปีที่แล้ว

      Always worth looking up WHY something is in there. 79 character line length for instance. The rational is that 80 chars or less prints on paper well and more importantly, Most screens are wide enough you can do a DIFF and side by side comparison. Can you set it to 150 or ignore it all together? Sure you can. But you will regret it when your trying to diff your code and your display is not 300+ characters wide.

    • @alisterware8062
      @alisterware8062 2 ปีที่แล้ว

      @@BreetaiZentradi Also when reading the eye can scan 80 columns & return to the next line easily, much greater than that & vision starts to hunt for the start of the next line making reading uncomfortable.
      The publishing industry have many "Guidelines" like this - developed over 100's of years of experience.

    • @BreetaiZentradi
      @BreetaiZentradi 2 ปีที่แล้ว

      @@alisterware8062 very good point. I like verbose_descriptive_snake_case_names but you hit the 79 character limit fast. You are forced into a vertical programming style. It works will for dffing later. I lose my long lines, but I end up with a better workflow.

  • @mgav006
    @mgav006 2 ปีที่แล้ว +2

    Just starting out with building my first few scripts using Python so thanks for the tip on the style guide. Wil incorporate it into my code :)

  • @ChewingGum113
    @ChewingGum113 3 ปีที่แล้ว +11

    Thank you for sharing these examples! There are some I'm guilty of making in the past that I learn the hard way. I like the last one about passing data types of the parameters through functions, it refers to duck typing issues and helps avoiding it.

  • @voxelfusion9894
    @voxelfusion9894 3 ปีที่แล้ว +4

    I did exactly that in the thumbnail, but I had good reason:
    I was making a little web scraper with selenium, and wanted a function that closes an annoying cookie banner.
    Depending on your country, that cookie banner may not appear or will have different buttons.
    Removing the cookie banner wasn't necessary, just nice to have, so if it actually failed, it didn't matter, so I passed.

    • @fierce1340
      @fierce1340 3 ปีที่แล้ว +1

      When you encounter an error, python will tell you the error (they're called Exceptions in python) that happened. You should always specifically check in your try except block for that exception because you won't be able to tell if you encounter a different error in the future. I do a lot of web scrapping with selenium and I've messed up a lot of collected data because I let every error pass through instead of just letting the program continue when it encountered the specific error I knew was going to happen.

    • @TinyMeatPete
      @TinyMeatPete 3 ปีที่แล้ว +1

      As someone who makes so many bots with Selenium, I can tell you; you do not need a try except loop barely ever. You could just make it an if statement that checks if the cookie popup has appeared and if it has not them move on. If you want a full opinion, I'll happily check the code.

  • @iamchesco
    @iamchesco 3 ปีที่แล้ว +3

    Awesome!!! informative and fun. Keep up the great content!

  • @Djzaamir
    @Djzaamir 2 ปีที่แล้ว +2

    I really loved the video, you explained all the issues in such an enjoyable fun manner. Thank you. I'll remember these :D

  • @justinharass1416
    @justinharass1416 2 ปีที่แล้ว +2

    I don’t think that the empty try/except block is “bad”. I can see uses for it. Like when you expect an error, and don’t want that error to stop the whole program.
    I do want to add that while leaving the except block empty is ok, just printing the error is much better instead of getting rid of the whole try/except block…

    • @martinmckee5333
      @martinmckee5333 2 ปีที่แล้ว

      Having it print the error is my preferred style if I do not have a specific need to handle the error immediately or I'm testing calling code to ensure that the errors I would be protecting against don't, actually happen.
      It avoids to completely unknown failure problem while preventing the errors from propogating.

  • @sprintwithcarlos
    @sprintwithcarlos 3 ปีที่แล้ว +46

    Even though Python does not enforce data types, your advice about using type hints is great. This is a very good video
    Happy 2021, hope your channel grows a lot because your content is quite good

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +4

      Thanks so much, that's very motivating to hear! :) All the best to you in 2021!

    • @AlexAcostaB
      @AlexAcostaB 3 ปีที่แล้ว +1

      Also that it is available from version 3.5 (PEP 484) according to documentation. Perhaps I'm wrong in my research. Great video btw :D

    • @MateuszAdamczyk
      @MateuszAdamczyk 11 หลายเดือนก่อน

      Data types in Python suck badly and they are not usable. Moreover, pep8 is not as important as consistency with existing codebase and team rules.

  • @medoubella
    @medoubella 3 ปีที่แล้ว +5

    Hello there,
    what if when for-looping, u need to access the index of the current element, is there any 'pythonic' way of doing that other than using the unpythonic i in range(len(list)) ?

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +8

      Great question, you should use "enumerate" function:)

    • @tonyy6026
      @tonyy6026 3 ปีที่แล้ว

      .find() works as well

    • @isidrodeleon673
      @isidrodeleon673 3 ปีที่แล้ว +1

      for i, element in enumerate(list):
      print("Index: ", i, " Value: ", elem)

  • @volodymyrsaruta4385
    @volodymyrsaruta4385 2 ปีที่แล้ว +2

    Great video and I agree with these! Thank you!
    I have a recommendation about recording yourself, you should always have few centimeters of empty space above your heard. I see from your newer videos that you didn't fix this problem yet

  • @tinongnjong1305
    @tinongnjong1305 2 ปีที่แล้ว

    Hey Karoline,
    You earned a new sub from this tutorial. Thanks very much for the time put into making it, and looking forward to watching future vid/tutorials 😁

  • @furiousfellow1583
    @furiousfellow1583 3 ปีที่แล้ว +6

    Some tip: whenever encounter a try,
    /except/pass, add a #todo statement with what the program should do in case of a error, you might never implement it but at least it looks like you are aware of it 😆

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +3

      Haha writing comments/todos is typically a very good idea!

  • @TWMist
    @TWMist 2 ปีที่แล้ว +3

    with the try statement would it not have been better to have the except catch and display the error?

  • @yeaminmsw2683
    @yeaminmsw2683 2 ปีที่แล้ว

    Amazing Guide for us. Thanks a lot Karolina Sowinska.

  • @Nedwin
    @Nedwin 2 ปีที่แล้ว +1

    Very nice sharing Karolina!

  • @CaptainCsaba
    @CaptainCsaba 3 ปีที่แล้ว +4

    I am only guilty for one of these but oh boy is it a big one for me. As Somebody who learned C# after Python I changed my style to the C# style of variables being camelcase and functions being pascalcase. I tried so long to create code with snakecase but it is just so unreadable for me. Functions and varibles have the same style, and it becomes bad to my eye to differenciate them. I want to move over this so bad but I just can't. I literally can't read the same long code as well in snake case as opposed to camel/pascal case.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +1

      You'll get used to it! ;)

    • @ZaidAlshattle
      @ZaidAlshattle 3 ปีที่แล้ว

      This makes two of us haha. Its a struggle

  • @AthanasiouApostolos
    @AthanasiouApostolos 3 ปีที่แล้ว +5

    As an ex java developer and soon to be data engineer I find your tutorials very useful. Thanks for uploading :)
    I assume ->None is the return annotation but does it give an error if we return a different data type like int or string?
    "run_this_baby" lol I will use it in production the next time.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +3

      I think it might let the undesired data type go silently. So it's more of a readability feature rather than a safety measure.
      Haha run_this_baby is a perfect name substitution for 'main' 😂

    • @paulvorderegger1522
      @paulvorderegger1522 2 ปีที่แล้ว

      IDK if this helps but if you're an ex java developer "None" is the equivalent of "null" in Java.
      Python usually doesn't give you errors if you return "the wrong type", because really, you can't return the wrong type. You can set a variable to a string and later assign an integer to it and everything is good.

    • @marcodoe4690
      @marcodoe4690 2 ปีที่แล้ว

      wait until you see how interfaces are written in python. it's my personal favourite meme.

  • @camadams9149
    @camadams9149 2 ปีที่แล้ว +2

    Great video! Just started learning Python today after spending the last few years mastering C & JavaScript
    Already excited that there is a language wide style standard

  • @transientaardvark6231
    @transientaardvark6231 2 ปีที่แล้ว +1

    Wow, thanks for the tip about try / except pass - I can now stop my Python scripts from inconveniently crashing like they normally do. I'll be making sure to always put that round my entire code from now on. I'm so glad the TH-cam algorithm pointed me here.

  • @jj-big-slay-yo
    @jj-big-slay-yo 3 ปีที่แล้ว +4

    Or just use black and isort for formatting. and then pylint or flake8 as an eslint replacement for python.
    And also a good tip: do not try to be too clever with your [incomprehensible without a long comment] code with binary operators and the clever but convoluted use of nested list comprehensions / generators with some lambdas or so.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว

      Great point about making the code as easy to read as possible!

  • @laurinneff4304
    @laurinneff4304 2 ปีที่แล้ว +3

    You forgot one thing: variables in an if name main block are still globals. All code should be moved into functions

  • @andrecarvalho-li9kd
    @andrecarvalho-li9kd 2 ปีที่แล้ว +1

    I'm just starting college for software engineering, and python is the first language i picked, didn't know a lot of the things you showed. Really helpful video, tysm.

  • @anton993
    @anton993 2 ปีที่แล้ว +2

    6:42 I think that for this fix you could also use the .join() method, which displays the elements of the list as strings and between spaces!
    So instead of having a for loop you could have:
    print("
    ".join(my_goods))

  • @hrudyplayz
    @hrudyplayz 2 ปีที่แล้ว +33

    Great video, i agree with most stuff mentionned here except for a few points, as well as add bit more precision:
    - Globals, even inside __main__ are still globals, they're just context-specific globals (which i agree are better than creating useless globals, no matter the context). In fact, even functions are globals in a sense, though they're handled in a better way.
    I wouldn't particularly tell people to avoid globals at all cost though. You should definitely avoid them if you make a library that is meant, for example, to be used with other projects (or even shared publicly). I feel like using them exclusively for a specific project is okay though. There's little chances that conflicts could happen or that it would have an impact if you handle your project correctly and don't just update every dependencies without checking.
    - The .replace solution to string indexing isn't great. It works in this particular example, but if you had multiple "C" characters it would mess all of them up. It is better to just write a replaceIndex method somewhere, that returns string[:index] + character to replace + [index + 1:].
    - The try except pass is indeed terrible. Using it to wrap the entire __main__ function is okayish. I mean it will work and will do stuff "as expected", but the program will just stop doing anything (crash if you will) instead of giving a useful output to both the dev and the end user. Basically, it's useless, not because it might mess with execution order and such (which definitely will happen if you use it anywhere other than the __main__ function), but because it just hides the fact that your program crashed, which is worse UX design than just letting the normal Python error happen.
    - The type indication is a good tip to give, i would just tell you to omit those if your function accepts multiple types for an argument for some reasons. I would also go against writing the return type this way, depending on the function (knowing that it returns None isn't generally useful for example). I would also recommend to use docstrings for this whenever possible, sometimes it may be better than plain type indications.
    - You should definitely use pythonic loops like shown in the video. I will also add that you should avoid using while loops for iteration in most languages. A while loop is slower performance-wise (as it needs to compare the values everytime) and harder to read and use as it needs both to have the initial index for the variable and the increment at the end of the loop. Some normal range for loops can be okay even in Python i feel, but if you use the value of the objects you iter for, definitely prefer the "for in" format. There's also the enumerate function that you can use as well, if you need both indexes and values.
    Please note that the opposite is true as well, don't write functions in a too "pythonic" way. Some people will tell you to use list comprehensions instead of for loops, and i'll disagree there, it's just a matter of taste, but for loops are definitely more readable than your usual comprehensions, you should use those but only on simple iterations, please try to avoid the nested comprehensions when normal for loops would do the same but more readably.
    - Finally, and i know this is controversial, but i disagree with your point on PEP-8. Your goal should definitely be to have a code as readable as possible, and sometimes PEP-8 goes against that. Some recommendations there actually make the code harder to read, and some are good things everyone should use. Rather than forcing yourself to use PEP-8, create your own style with coherence in mind. I, myself, personally use camelCase ("mixedCase") instead of snake_case too, i simply prefer the first one compared to the latter one which leads to way longer and harder to type variables (thank god autocomplete exists), i also like to use simple compound statements correctly spaced as blocks, which PEP-8 doesn't like.
    As for work on bigger, team-based projects, i would recommend writing a style guideline that most people in the team agree on instead of just using PEP-8. At worse, if people can't agree on something, just have the style required for some commonly edited or used parts of the code.

    • @raffimolero64
      @raffimolero64 2 ปีที่แล้ว

      > using camelCase in python
      😤

    • @hrudyplayz
      @hrudyplayz 2 ปีที่แล้ว

      @@raffimolero64 camelCase is better than snake_case, change my mind.

    • @Aoekin
      @Aoekin 2 ปีที่แล้ว

      definitely agree with the PEP-8 not all places I found out use just Pep-8, but have their own guidelines,.

  • @mario_luis_dev
    @mario_luis_dev 3 ปีที่แล้ว +85

    this was very informative. Coming from a C++ background I see that my Python skills are very un-Pythonic 😅 Need to get used to these pep-8 standards more

    • @slavkokaradzic86
      @slavkokaradzic86 2 ปีที่แล้ว

      Agreed lol

    • @Victor_Marius
      @Victor_Marius 2 ปีที่แล้ว +5

      If you don't follow pep8 it won't make you less professional. There are a lot of frameworks and libraries written in camel case for example.

    • @Victor_Marius
      @Victor_Marius 2 ปีที่แล้ว +4

      You do want your workplace wants you to and what you want

    • @georgeousthegorgeous
      @georgeousthegorgeous 2 ปีที่แล้ว +9

      nobody ever cares about these standards. code just needs to be clean, understandable and do it's work

    • @SuperNovaJinckUFO
      @SuperNovaJinckUFO 2 ปีที่แล้ว +1

      In my opinion, you should take pride in that XD (C++ all the way!)

  • @computerscience1017
    @computerscience1017 2 ปีที่แล้ว +1

    Great tips. Subscribed!

  • @danielcobia7818
    @danielcobia7818 2 ปีที่แล้ว +1

    Any good places to learn more about #6 (explicitly putting data types)? The books I've been reading haven't really touched on that aspect.
    I'm certainly glad I have never found out about the "pass" function, that does sound extremely foolish, even as a coding n00b. Of course I've been working in enterprise software support for quite a few years, this seems like something that should be obvious.

  • @playerscience
    @playerscience 2 ปีที่แล้ว +5

    I didn't even know that it *Is Possible* to define a variable type in python.
    Thanks 😊

    • @user-yy1iu3xo7d
      @user-yy1iu3xo7d 2 ปีที่แล้ว

      Type Hints was introduced in Python 3.5, for basic data types, you can add them directly after parameters or vairables. But for lists, tuples, iterators, dictionaries and other complex data types, you should import them from "types" package. Also for previous versions of Python, you can define Type Stubs files for telling intepreter what those variables or functions actually are.

    • @skylercurtis5399
      @skylercurtis5399 2 ปีที่แล้ว +2

      its important to note that this is called type hints, and as the name suggests, it just hints at the type. This is in no way used or acted upon in the program. If you define it as a string but then assign it a number, it wont stop you, or care, or even notice. These are there strictly for you to document your code. (and some unittesting and linting tools may check them to make QA testing easier)

  • @emanuelblei7699
    @emanuelblei7699 3 ปีที่แล้ว +4

    I found that topic and presentation of the video quite nice. However, I you would have shown the code for a bit longer. It's really hard to read otherwise.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว

      Ah, sorry, I just assumed people can pause when they need to see more detail, but you're right!

    • @emanuelblei7699
      @emanuelblei7699 3 ปีที่แล้ว +1

      @@karolinasowinska Yes, that is true. That might work best for the last part where you show the 'before' and 'after'. Maybe less so for the other parts where you show how you adjust the code. But, it is still a good video. Don't get me wrong on this.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว

      @@emanuelblei7699 I'm glad you liked it! Thanks for sharing your thoughts as well, I appreciate it :)

  • @bengeorge92
    @bengeorge92 3 ปีที่แล้ว

    Could we improve it further by adding def main(), or would that be more preferential?

  • @giovannicriscwcc4249
    @giovannicriscwcc4249 5 หลายเดือนก่อน +1

    very useful and well explained ! thanks Karolina !

    • @karolinasowinska
      @karolinasowinska  4 หลายเดือนก่อน

      Glad it was helpful!

    • @giovannicriscwcc4249
      @giovannicriscwcc4249 4 หลายเดือนก่อน

      @@karolinasowinska exception handling lead to the deepest fantasies 😂 I must admit I am a python "pass" keyword fan, and then I "pass" hours crying on debug

  • @drtheoretisch
    @drtheoretisch 3 ปีที่แล้ว +7

    I would have also changed the print params to print(f"My name is {x} I'm {y} years old, and these are my goods:") which makes it much more readable by applying string interpolation :)

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +1

      Good point! ;) Thanks!

    • @martinmckee5333
      @martinmckee5333 2 ปีที่แล้ว

      I love the string interpolation syntax, but I still generally use format() to support older Python versions.

  • @eliasjohanliavaag
    @eliasjohanliavaag 3 ปีที่แล้ว +7

    Had to check whether it was my fire alarm beeping while watching this. Hope you replaced its batteries! :D

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +2

      Hahah took me a few months, but yes!;)

  • @minhly650
    @minhly650 2 ปีที่แล้ว

    2:14, for this specific example that you show here, it does not make sense when passing errors if it occurs. However, there will be some cases that they do not want errors to block other code below, but they also do not care about the errors since they are not critical.

  • @ThorstenStaerk
    @ThorstenStaerk 2 ปีที่แล้ว +1

    I can't count the times I was debugging an error and it turned out it was just a camelCase mistake... is it replaceWithin or rePlaceWithin or rePlacewithIn...

  • @jonathan-._.-
    @jonathan-._.- 3 ปีที่แล้ว +36

    i was quite afraid for a moment youd found the python code i wrote so far :D

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +3

      Haha nah, I found my code from one year ago :D

    • @jonathan-._.-
      @jonathan-._.- 3 ปีที่แล้ว +4

      @@karolinasowinska moral of the story : dont look at old code , might leave permanent scars :3

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +2

      @@jonathan-._.- haha sweet oblivion is rarely a bad strategy! ;)

    • @jonathan-._.-
      @jonathan-._.- 3 ปีที่แล้ว +1

      @@karolinasowinska yeah elder scrolls oblivion - really good game :3

    • @lifestoriesfromearth6271
      @lifestoriesfromearth6271 3 ปีที่แล้ว

      do u have a recommendation book for this change

  • @0xCAFEF00D
    @0xCAFEF00D 3 ปีที่แล้ว +3

    For the replacement of C with K I would prefer 'K'+myName[1:] rather than replace in this example. The intent was to replace the first character, not every C with a K. They just happen to coincide. They should know both replace and string slicing of course.
    Specifically for a video like this the people who don't know about string mutability are probably served better knowing how to do what the code tried to without error.

    • @vekyll
      @vekyll 3 ปีที่แล้ว

      I disagree. The usual phrase in natural language is "Karolina with a 'K'", and while it is completely implicit _what_ to replace, I'd strongly argue it is "Carolina with a C written K", not "Carolina with first character written K". "Bianca with a K" is "Bianka", not "Kianca", right? :-D

    • @0xCAFEF00D
      @0xCAFEF00D 3 ปีที่แล้ว

      @@vekyll You've missed my point. I'm not saying replace isn't a good feature. But it's for actually replacing a set of things with another thing. Like .replace(' ',''). If a name has multiple of a character you'd like to replace. But if you just want to replace one of those you'll have to include some of the surrounding characters in both the old and new substr in the replace.
      That's why I think showing the slicing method is important. If we could just show one method we'd show that one because string slicing couldn't deal with every case. The rookie would find replace eventually. While they might end up doing crazy replace tricks if that's all they know.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +1

      Perhaps you're right, I didn't think about it. Hopefully it doesn't confuse anyone though!

    • @vekyll
      @vekyll 3 ปีที่แล้ว

      @@0xCAFEF00D Yes, I probably missed your point, since to me, Python is executable pseudocode. When you want to put K in Karolina, you put it _instead of a fixed letter_, not _in a fixed position_. Do you agree on that?
      Your second argument is nonsense, at least according to my experience (which includes teaching hundreds of people about Python). Beginners are incredibly diverse, and yes, there are a good number of them that do everything with indexing instead of using more appropriate methods.

    • @vekyll
      @vekyll 3 ปีที่แล้ว

      @@karolinasowinska He isn't right, he just needs some time to understand it. :-)

  • @purpetualbeats
    @purpetualbeats 2 ปีที่แล้ว +1

    Thanks these are some great tips!

  • @TCW_Consulting
    @TCW_Consulting 2 ปีที่แล้ว

    Been coding some months now but never knew about Pep8 - very helpful! Good video

  • @yuanyuan23191212
    @yuanyuan23191212 2 ปีที่แล้ว +3

    Mistake #5: unpythoic looping -----> sometimes we need to use the index of the element. For example, compare current element with next element, then decide if break the loop. Wouldn't it be necessary to use the index here?

    • @karolinasowinska
      @karolinasowinska  2 ปีที่แล้ว +3

      The pythonic thing is to use the enumerate function for that :)

  • @laszlofazekas5700
    @laszlofazekas5700 3 ปีที่แล้ว +6

    About snake_case and PEP8: The most important thing is that to stay consistent. For instance in my case: I always use camelCase with methods and snake_case for functions/variables.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว

      True. Using a mixture of many styles is definitely the worst thing someone can do!

  • @rdubitsk
    @rdubitsk 3 ปีที่แล้ว +1

    Thanks for great video. I use try except when getting data from APIs or webscraping. As long as the except returns a specific message you can trace down the error.
    If I don't do this the whole page will crash. Its likely due to data missing or website changing. Why is this bad and what is the alternative?

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +2

      Hey! Capturing the error and returning a message is a perfectly valid thing to do. The problem is with the "pass" statement, which silences the error!

  • @brockobama257
    @brockobama257 2 ปีที่แล้ว

    I just wrote a try except pass and it works because I want to filter out the errors. The error corresponds to a url not having table data and I don’t want to save null data.

  • @donjindra
    @donjindra 3 ปีที่แล้ว +8

    The worst mistake, bar none, is that most programmers seem to think their favorite language is self commenting. It isn't. Please learn to thoroughly comment your code, folks.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +1

      Really good point!

    • @james4727
      @james4727 3 ปีที่แล้ว

      trying to review your own code for exams taught me the importance of this hahaha. Now I write comments as if im having a conversation to myself.

    • @eklipsegirl
      @eklipsegirl 3 ปีที่แล้ว

      Good variables’ and functions’ names are better than excessive comments.

    • @donjindra
      @donjindra 3 ปีที่แล้ว +1

      @@eklipsegirl Names are important, but you're naive if you think that helps much. I see this mindset a lot and it's why so much code is so hard to maintain. Good comments do not simply restate what's already there in the code, or find perfect names. You should tell us in plain language what the function is supposed to do, how it's supposed to do it, and maybe even why this solution (out of many possible solutions) was chosen in the first place. Otherwise the second programmer to look at the function has to trust your code has implemented what you intended to implement. Experienced programmers know that this is not necessarily the case. Bugs are everywhere. So he has to "reverse engineer" your intent. Even if he can do this reliably, it takes time. The original programmer could have and should have saved him this time.

    • @james4727
      @james4727 3 ปีที่แล้ว

      @@donjindra this ^^^ I try to make your code as unambiguous as possible.

  • @marioloera6210
    @marioloera6210 3 ปีที่แล้ว +3

    cool video, perhaps you can use f-strings, a mix of use single-quoted strings and double-quoted strings: "Welcome to my program" 'Carolina'

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +2

      I'd always recommend sticking to either single or double! ;)

    • @dragoneater2008_
      @dragoneater2008_ 2 ปีที่แล้ว

      F strings aren’t like that. You can use
      Name=input(‘please enter your name: ‘)
      Print(‘welcome to my program {name}.’

  • @DEtchells
    @DEtchells 2 ปีที่แล้ว +1

    Just now starting to learn Python, YT put this in my recommended list. Super useful, I’m gonna try hard to make my code fully Pythonic! :-)

  • @osnofla13ify
    @osnofla13ify 2 ปีที่แล้ว

    Welp, I definetly do like 5 of this mistakes. Thanks, I actually love this kind of videos.
    I study physics, so most of the programming I've learned by myself, I've become quite good in algorithms, but definetly lack the knowledge of stylizing my code in the right way.

  • @aniketpurohit8613
    @aniketpurohit8613 3 ปีที่แล้ว +4

    f-strings can be a useful addition! :)

  • @SealedKiller
    @SealedKiller 3 ปีที่แล้ว +3

    I love how the accent sometimes goes to a perfect british accent.

  • @hadavid2454
    @hadavid2454 3 ปีที่แล้ว +1

    If we make functions that takes no parameter and doesn't return anything, is it still good style practice to write -> None on the definition header?

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +2

      I would keep it for consistency, but I could have chosen a better example to be honest!

  • @christiencodes3086
    @christiencodes3086 2 ปีที่แล้ว +1

    The try except bit speaks to my soul

  • @fan_juggler
    @fan_juggler 3 ปีที่แล้ว +4

    About try-except, for example, when you are writing something like TUI interface with curses, there are one hundred ways a user can crash the program by messing with the terminal, so it's much cleaner to just write try-except than trying to catch every possible error, for each of which you would need to write 'pass' anyway :)

  • @pyroghost11
    @pyroghost11 2 ปีที่แล้ว +3

    Wow, you seem to be a great teacher based on how enthusiastically talk about this topic! Awesome

    • @karolinasowinska
      @karolinasowinska  2 ปีที่แล้ว +1

      Ha, or I just enjoy performing! Thanks though!

  • @denn9484
    @denn9484 2 ปีที่แล้ว +1

    I love your attitude and energy. Nothing about this video is boring :) Learned a bit too! Sub'd.

  • @LittleGnawer
    @LittleGnawer 2 ปีที่แล้ว

    Nice tips, although I never wrote Python code. Why, do you think, is it critical to alter a string directly after construction? What sense has another variable?

  • @jakman85
    @jakman85 2 ปีที่แล้ว +3

    C# programmer here. The loop you showed that was "unpythonic" is also not good practice in C#. You would use either LINQ ForEach to use lambda expressions or use a foreach block with similar syntax to your python code.

  • @kafarahat
    @kafarahat 3 ปีที่แล้ว +20

    In my opinion, using global variables is not bad, actually there are cases where it can be pretty useful.
    I would rather say that global variable manipulation is a bad practice with very small number of exceptions that I have seen.
    Btw, nice channel. Subbed.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +2

      Of course, there are cases when it's useful to have global variables. But in general, people tend to abuse it! Glad you like the channel! :)

  • @felixszopos-papp1478
    @felixszopos-papp1478 2 ปีที่แล้ว

    Regarding that first mistake.. The pass instruction exists to be a temporary placeholder while implementing code. It would only be a mistake to use it there, if you actually left that except block to be implemented down the line.

  • @michaelbyron1166
    @michaelbyron1166 2 ปีที่แล้ว

    Thank you for the excellent lesson. I learned several important lessons from this. Thank you for producing and sharing this.

  • @Rickety3263
    @Rickety3263 3 ปีที่แล้ว +3

    Great video! I changed the battery in my smoke detector but then I realized I was good

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +2

      Haha wow, what a quick action on your side. It took me half a year!

  • @HawkmasterStambaugh
    @HawkmasterStambaugh 2 ปีที่แล้ว +4

    The camel case bit is laughable. I've seen both from a lot of people in python throughout several jobs.

    • @PbPomper
      @PbPomper 2 ปีที่แล้ว

      I guess that's because snake case is hardly used outside of Python. Most OOP languages use a mixture of camel and pascal case to distinguish beween classes, methods and variables. Snakecase is usually just used for constants.

  • @redsnow123456
    @redsnow123456 3 ปีที่แล้ว +1

    Thanks Karolina. Any idea on how write unit tests on etl scripts running over excel files stored locally?

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +1

      You probably need to mock the structure of the file, so create a small file with the same columns and example data and place it in your repo. :)

  • @Ayoubased
    @Ayoubased 2 ปีที่แล้ว +1

    Interesting video, I like the idea. I wonder if you can do a similar videos in the future and go on specific more than concepts.

    • @karolinasowinska
      @karolinasowinska  2 ปีที่แล้ว +1

      That's a good idea. I might give it a go;) Thanks

  • @NyscanRohid
    @NyscanRohid 2 ปีที่แล้ว +3

    "What are you doing in `except`?"
    `pass`: "Mind your own business!"

  • @hassainehassen5700
    @hassainehassen5700 3 ปีที่แล้ว +3

    Thank you , you have directed me to pep8 style

  • @vikramvijay3264
    @vikramvijay3264 2 ปีที่แล้ว +1

    It was Like a piece of cake ...thank you very much ...I'm an new subscriber 💐

  • @ThiagoOliveira-ex3vw
    @ThiagoOliveira-ex3vw 2 ปีที่แล้ว

    Such a good and organised content, thank you very much!!

  • @theresatonev2504
    @theresatonev2504 3 ปีที่แล้ว +3

    Thanks for the tips!
    P.S. I think you need to change the batteries in your smoke detector. XD

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว

      Hahah you're right, everyone keeps telling me this, and I am the only one who cannot hear that beeping anymore:D

    • @neiliusflavius
      @neiliusflavius 3 ปีที่แล้ว

      @@karolinasowinska ah, good to know it's not mine beeping!

  • @weltkaiserendzeit2417
    @weltkaiserendzeit2417 3 ปีที่แล้ว +3

    Well to be honest I wasn't aware that you could specify variable types in the arguments and output of a function even though I've been coding in Python since I was like 7
    It is astonishing how much left there is to learn

    • @tubeincompetence
      @tubeincompetence 2 ปีที่แล้ว +1

      As I wrote in another comment. I don't think Python really does anything with this information anyway and you'd need a separate type checker for it. And it's something introduced in 2015 so I can easily see people not knowing about it yet. It just exists there if we want the user to see it most of the time (at least in your own private projects/smaller codebases).

  • @yan_afrukh
    @yan_afrukh 2 ปีที่แล้ว

    I was never aware about those pep8 guidelines. Thanks for this enlightenment, as a new student who's discovering python, respecting norms since the beginning would save me a lot of time later

  • @anzonyquisperojas4800
    @anzonyquisperojas4800 3 ปีที่แล้ว +1

    I really enjoy all your videos. Astonishing information

  • @rodrigoamoedo8523
    @rodrigoamoedo8523 3 ปีที่แล้ว +4

    wellcome to my life...
    even worst, my friends nested nested nested loops, are a pain in my back

  • @owlcatxx
    @owlcatxx 3 ปีที่แล้ว +4

    Love this video! As a newb python programmer, now I can pretend I know what I'm doing :D
    (Oh and for your fire safety, I hope you changed the battery in your fire alarm after filming his video! We can hear it beeping!)

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +1

      Hahah I have to finally do it... you're the 100th person to notice this. :D

    • @JeffreyChadwell
      @JeffreyChadwell 3 ปีที่แล้ว

      Thank you! I thought I was hearing things.

    • @vekyll
      @vekyll 3 ปีที่แล้ว +1

      try:
      make a youtube video about Python
      except FireAlarmBatteriesEmpty:
      pass # ;-PP

  • @davidg5898
    @davidg5898 2 ปีที่แล้ว +2

    Good to know. In my physics undergrad days we started on C languages and then were taught Python, and we just used all of the C conventions. I'm going to try and get used to Python style when working in it.

  • @zimcoder
    @zimcoder 2 ปีที่แล้ว

    for the array example, C# has the foreach way of looping through. it's basically the same thing as you show there.

  • @rhalfik
    @rhalfik 2 ปีที่แล้ว +3

    Jesteś bardzo dobrym nauczycielem.

  • @eklipsegirl
    @eklipsegirl 3 ปีที่แล้ว +4

    There is a legitimate reason to use camel case: working with PyQT. Its classes and functions are all camel-cased, so it makes sense to follow along.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว

      I agree, it makes sense then!

    • @nysaea
      @nysaea 2 ปีที่แล้ว

      I'd argue there's another good reason, within reason and assuming consistency throughout the code: I will never understand why anyone would think snake case makes names more readable than camel case. Code written in snake case is barely legible to me because it visually tricks my brain into seeing several entities instead of one.
      It's one of those things where PEP8 start leaving common sense and safety territory and enter aesthetic suggestion territory, and it bothers me that anybody would use those particular recommendations as gospel.

  • @mcalister1911Ab
    @mcalister1911Ab 2 ปีที่แล้ว

    you have to use the function param name as run_this_baby (name = my name , age = my_age) -> this is more readable
    also that replace is possibly dangerous because it will change all the "C" inside the string.You have to add the my_name.replace("C" , "K" , count = 1) -> only will change the first C on the string
    the return in the run_this_baby function is redundant

  • @gabrielsantos-bn1di
    @gabrielsantos-bn1di 3 ปีที่แล้ว

    And if our function returns a non primitive type (like in POO paradigm, that a function could return a class type value)?

  • @caitlinmclaren2695
    @caitlinmclaren2695 3 ปีที่แล้ว +4

    Thanks for your awesome videos, but: based on Robert Martin's "Clean code" book, it's better to use class variable than passing function parameters. Instead of that, You should have fixed the biggest problem with the code: "run_this_baby". Moreover, you can loop with or without indexes in Java, Python, and almost any other programming languages.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +2

      Good point, I was trying to be funny with "run_this_baby", but effectively I introduced an anti-pattern. Oops, thanks for pointing it out though. Of course, function names should be representative of what the functions do.

    • @alpers.2123
      @alpers.2123 3 ปีที่แล้ว +1

      Clean Code is not clean

    • @The_Lawnmower_Man
      @The_Lawnmower_Man 2 ปีที่แล้ว +1

      To elaborate on the point about " _you can loop with or without indexes in Java_ ":
      Modern Java, modern C++, and C# all support this index-less style of for-loop, which sometimes is colloquially called a "for-each loop", and particularly in C++ is called a "range-based for loop". (The actual keyword used for this kind of loop is "for" in both C++ and Java, whereas in C# it's "foreach" instead... and in addition to that, C++ also has a standard-library function named "for_each", which is meant for use in functional-programming situations & takes a function-value to be used instead of a conventional loop-body.) And, same as in Python, it's generally preferred to use this style of loop wherever possible when writing new code in those languages.
      Furthermore, even *old* versions of Java and C++ had the concept of an "iterator object" which could be used as the loop-variable when looping through a collection-object such as a std::vector or a std::list in C++ or an ArrayList or a LinkedList in Java. (In old-style Java, iterator objects were mainly intended to be used with "while" loops, whereas in old-style C++ they were intended to be used with C-style "for" loops.) I.e. an iterator object could be used *instead of* using a plain integer as the loop-variable.

    • @tomwilson2112
      @tomwilson2112 2 ปีที่แล้ว

      @@The_Lawnmower_Man Yeah, I saw this as not a great example, because there's nothing unique about the Python FOR loop... it's just a "for each" in any other reasonably modern language.

  • @thefelixgan
    @thefelixgan 2 ปีที่แล้ว +4

    Data Saving Mode:
    Mistakes
    1. Try and except: pass Block
    2. Mutating a string
    3. Using the camelCase for function and variable names
    4. Using global variables
    5. Unpythonic looping
    6. Not specifying data types

  • @davidaw104
    @davidaw104 2 ปีที่แล้ว

    I don't understand if this type of error logging is much better then having the console prompt you where the error comes from. After all it shows you where the error comes from with an arrow key.

  • @twismfamily
    @twismfamily 2 ปีที่แล้ว +1

    I'm just starting out as a coder and I really value your expertise on this matter, however as a brand new newbie to coding how do I stay away from these mistakes if that's what i'm taught via coding boot-camp or googling online help?

    • @karolinasowinska
      @karolinasowinska  2 ปีที่แล้ว +1

      I'd say - don't worry too much about being perfect. Focus on solving problems first, but also keep in mind and try to incorporate some advice from this video!

    • @karolinasowinska
      @karolinasowinska  2 ปีที่แล้ว

      @@Crashawsome Absolutely fair point

  • @GeneralPet
    @GeneralPet 3 ปีที่แล้ว

    I have a bit of a problem when specifying argument types. Say I have 2 objects that need a reference to each other. In the classes lets add a method to add the other object as an attribute. In whichever class I defined first, I will get an error saying that Class2 type doesn't exist. This is absurd to me, because I can just write the 2 classes in different files and import the other class in each file. Just because I need to specify the argument type >:(

  • @blackhole6310
    @blackhole6310 2 ปีที่แล้ว

    The reason for the "unpythonicity" of the while loop here is not just because python has a nice looping function that handles variables and whatnot, it's mainly because the function for is using the C implementation instead of calling the C interpreter multiple times (for each manual comparisons and assignment). This is the first reason for using the for loop in the first place, however you are right when saying it's less prone to mistakes although while loops have a different purpose.

  • @BrunoAraujo-po2lm
    @BrunoAraujo-po2lm 2 ปีที่แล้ว +1

    Got one subscriber, good job! Nice fun interaction and explanation.

  • @rogervanschie8647
    @rogervanschie8647 3 ปีที่แล้ว +1

    I've never seen the '-> None' in any code before. I'm assuming that casts the output of the function? If so, what if your function could output multiple types, for example an int or a list, what then?

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว +1

      Then you specify everything that the function returns, e.g. ->List[str,str] (first you must import List from typing).

    • @rogervanschie8647
      @rogervanschie8647 3 ปีที่แล้ว +1

      @@karolinasowinska I really think what you're covering here is important, and something that I personally am actively working on improving. But I think your pace and "broad strokes" is going to go over the heads of those that need it. Perhaps a slower paced series on PEP 8? I'd definately watch that. Not knowing those "rules" is one of the main things holding me back from contributing to projects on GitHub.

    • @karolinasowinska
      @karolinasowinska  3 ปีที่แล้ว

      @@rogervanschie8647 I'll think about doing that, thanks for the feedback!

    • @blazep5881
      @blazep5881 3 ปีที่แล้ว

      @@karolinasowinska list defines that the function returns a list. I think what you're looking for is union. Eg Union(str, int) says the function can return either a string or an int