10 Tips to Become REALLY Good at Python

Поділитися
Вставка
  • Опубліковано 26 січ 2025

КОМЕНТАРІ • 82

  • @ArjanCodes
    @ArjanCodes  2 дні тому +1

    👉 Try Brilliant for free for 30 days and get 20% off an annual subscription: brilliant.org/arjancodes/

  • @jmreagle
    @jmreagle 2 дні тому +45

    I wish there were chapter/section marks.

    • @cetilly
      @cetilly 2 дні тому +1

      There are. Expand the video description

    • @denisquarte7177
      @denisquarte7177 День тому +1

      There are it's called put the transcript in Gen AI and save lifetime from that lying bastard telling you maximizing watch time would be benifital to you instead of him.

  • @mrrobotman5299
    @mrrobotman5299 2 дні тому +21

    My favorite f-string trick is dynamic fields. I think it's only the width and precision fields but it's a nice feature. For example:
    >>> width = 10
    >>> pre = 2
    >>> val = 3.14159
    >>> f"{val:>{width}.{pre}}"
    ' 3.14'

  • @mikelauderdale4845
    @mikelauderdale4845 День тому +6

    So much great advice here - thank you Anjan! I'm old, and started coding in the 1970's when memory and processor speed were very limited resources. So pretty much every program became an exercise in being super efficient. e.g., using INT vs FLOAT was a big deal. So I really do appreciate the type annotation habit, and comprehension as well. As I am now working my way into some advance Python techniques, I am learning to both love and hate the extendability of it. Channels like this are like comfy blankets. ☺

  • @gonecoastaltoo
    @gonecoastaltoo 2 дні тому +6

    10:10 comprehensions are nearly always a better choice than map and filter - you can do both operations with a cleaner syntax that will be more familiar to non-functional programmers.

    • @materialMammal
      @materialMammal День тому +1

      From an FP perspective map and filter tend to be chained methods on classes that implement iterable. The process of chaining methods is just not the same in python when it comes to map and filter. As someone who loves FP in other languages, I'd choose comprehensions over the map function in python any day. So I agree. I barely use map or filter in python.

  • @elefantsnablar
    @elefantsnablar 2 дні тому +5

    You put out great content but this video feels more oriented towards beginner/intermediate programmers than what the title makes it seem. I’d love a video covering some more advanced topics!

  • @damymetzke514
    @damymetzke514 2 дні тому +14

    One of my favorite features I learned somewhat recently is the "yield from" syntax. Within a generator function, you can write "yield from my_iterable". This will then yield all values from that iterable. This can be a list, dictionary, or even another generator. It doesn't come up for me that often, but it occasionally does when working with nesting logic.

    • @aHandfulOfR
      @aHandfulOfR 2 дні тому

      Me 2) Did nicely with pytest fixtures: had a generator method as a constructor for a dozen of similar fixtures, which "yield from" generator, with parameters and standard actions "before" and "after".

  • @RedSpark_
    @RedSpark_ День тому +3

    13:46 The sqlite context manager doesn't actually close the database connection!

    • @Naej7
      @Naej7 День тому

      What does it do then ? When is it closed ?

    • @arnoutstandaert
      @arnoutstandaert День тому

      ​@@Naej7You still need to manually close it. The context manager only takes care of auto-committing or rolling back the transaction contained in the block... a bit counter-intuitive, but it is what it is.

  • @mathman316
    @mathman316 2 дні тому +1

    I first turned to type annotations as a way to document and understand someone else's code base which used variable names like "x" and "y". Having type annotations and docstrings gives you almost-free API documentation.

  • @royjanik1615
    @royjanik1615 2 дні тому +2

    I’m still trying to wrap my head around when to make something a standalone function. Like, my instinct would have been to make that calculate discount function a part of the shopping cart class.

    • @mac68tm
      @mac68tm День тому +2

      Your instinct may feel right but is `calculate_discount` specific to an instance of that class or can be applied to any instances of that class? Ask yourself if different accounts have different discounts applied to the shopping cart:
      * if the answer is Yes, than it makes sense to have `calculate_discount` as a method of that class
      * if the answer is No, so the same discount is applied to every customer shopping cart, than it makes more sense to be a function than a method
      That's my 2 penny thought on the subject.

  • @ulyssep2997
    @ulyssep2997 2 дні тому +1

    Thank you Arjan, your videos are always so helpful and well-crafted!

  • @omarcrosby
    @omarcrosby День тому +1

    Thanks again Arjan, these tips were helpful. I would love to hear your input on dataclasses vs pydantic models.

  • @xiggywiggs
    @xiggywiggs 2 дні тому +1

    Nice! I didn't know about any of those other built-ins thats handy as hell! also secret 11th tip is jupyter notebooks! I hadn't considered how great they would be as a place to collect coding tips and tricks!

  • @evgeniyevgeniy8352
    @evgeniyevgeniy8352 2 дні тому +6

    Jupyter Notebook displays the result of executing last row in a cell, so you can just type
    squares
    in last row instead of
    print(squares)

    • @ArjanCodes
      @ArjanCodes  2 дні тому +2

      Good suggestion! I’m not a big notebook user, so that’s good to know 😊.

    • @rafiullah-zz1lf
      @rafiullah-zz1lf 2 дні тому

      @@evgeniyevgeniy8352 lol. Just because @arjancodes is a software engineer doesn't mean he can fix the blue screen.

    • @kilianklaiber6367
      @kilianklaiber6367 2 дні тому +1

      @@ArjanCodes You can also create code cells in python modules that execute independently using #%%.

  • @kilianklaiber6367
    @kilianklaiber6367 2 дні тому

    I really like your tips. I am glad that I knew around 90% of the commands and features you showed in this short video. I am still struggling with deciding when to you Classes or not, still your advice is helpful.

  • @sylvainprive1754
    @sylvainprive1754 2 дні тому +1

    9/10 for me ;) ! Still struggling with standalone functions instead of methods. I started recently by watching to your vidéos ! Thanks for your work

    • @justliberty4072
      @justliberty4072 14 годин тому

      From the perspective of someone who learned to program in the 1970's, before OOP, this is a remarkable statement!

  • @ms070965
    @ms070965 День тому

    Very interesting as always. Réel réassured to use most of them

  • @orgadish
    @orgadish День тому +1

    18:35 Using len(numbers) means you need Sized not Iterable.

    • @chatcharinsangbutsarakum5963
      @chatcharinsangbutsarakum5963 День тому

      Because `sum` requires `Iterable`?

    • @ИванАхременко
      @ИванАхременко День тому

      @@chatcharinsangbutsarakum5963 sum requires Iterable, len requires Sized, so the variable should be annotated as Collection.

    • @orgadish
      @orgadish День тому

      Ah yes, it requires Sized AND Iterable = Collection

  • @marcin2x4
    @marcin2x4 23 години тому

    Can you make a video on overloading special methods in Python?
    Also, __getattr__ vs __getattribute__ would be a good topic to cover.
    Thx for all you hard work!

  • @krzysiekkrzysiek9059
    @krzysiekkrzysiek9059 2 дні тому

    These kinds of videos are amazing, just perfect👌. So much useful and practical knowledge in 27 min.

  • @dmitryutkin9864
    @dmitryutkin9864 53 хвилини тому

    Nice vid - thanks for sharing

  • @spacegirl1108
    @spacegirl1108 День тому

    usefull tips. Thx Arjan.

    • @ArjanCodes
      @ArjanCodes  4 хвилини тому

      Glad it was helpful!

  • @mosesodalo5074
    @mosesodalo5074 23 години тому

    This is a good video.
    Some of the things that might be thrown around in a code review, i am able to see them in action.
    Ill practice on them

  • @Gigusx
    @Gigusx 2 дні тому

    Had no idea about the debugging syntax in f-strings, that's pretty cool!

    • @omarcrosby
      @omarcrosby День тому +1

      yeah that one made me say woah!!!

  • @danilshein4612
    @danilshein4612 2 дні тому

    For an imutable DTOs i often use classes derived from NamedTuple instead of dataclasses. It is quite similar but has less memory footprint (if it is imortant) and has built-in as_dict method that is quite handy for serialisation

  • @IdPreferNot1
    @IdPreferNot1 2 дні тому

    Great video, thx! Really helped round out my jumbeld self taught understanding of python with a better framework understanding.

  • @mdata.youtube
    @mdata.youtube 2 дні тому

    Thank you, great video!

  • @CNich90
    @CNich90 День тому

    Can you link to those notebooks? Seems like a fantastic quick reference! Awesome video

    • @ArjanCodes
      @ArjanCodes  3 хвилини тому

      Hi, the link with the code is in the description :)

  • @DrGreenGiant
    @DrGreenGiant День тому

    Coming from embedded, type hints are so helpful to us with a strongly typed brain to comprehend code!
    I just wish they'd fix Queue subtyping so I don't have to use quotes to define what type the messages are.

  • @yickysan
    @yickysan День тому

    I use f strings with sql. But only for read only queries and to dynamically pass arguments like dates to queries.

  • @dmbrv
    @dmbrv 2 дні тому

    Thanks for the video

  • @bogaczew
    @bogaczew День тому

    know bulit-in funcions is a good advice for any language

  • @conceptrat
    @conceptrat День тому

    @4:30 Right there! I knew it! You're a time travelling Pythonista 😂😢

  • @twentytwentyeight
    @twentytwentyeight 14 годин тому

    Love the thumbnail, Arjan 😂😂😂

  • @rafiullah-zz1lf
    @rafiullah-zz1lf 2 дні тому +9

    Well well well. He didn't use click baits. The title should have been learn python in 5 minutes 😂

  • @manuelstausberg8923
    @manuelstausberg8923 2 дні тому +1

    @ArjanCodes I have a question about the calculate_discount() method you show at 26:50 because I often find myself in a similar situation:
    How do you decide whether to make that a function, or make it a method of the class that it takes as argument (ShoppingCart in this example)?

    • @kiraleskirales
      @kiraleskirales 2 дні тому

      Yes, that part is unclear to me as well. calculate_discount expects an object from a specific class, thus is conceptually linked to that class. Why not implementing it as a method?

    • @EvgenyLushev
      @EvgenyLushev 2 дні тому

      I'd like to hear Arjan's comment on this as well. In this situation, I would definitely make it a method in the class. Would be great to learn from a pro why function is better.

    • @mac68tm
      @mac68tm День тому

      Ask yourself if different accounts have different discounts applied to the shopping cart:
      * if the answer is Yes, than it makes sense to have `calculate_discount` as a method of that class
      * if the answer is No, so the same discount is applied to every customer's shopping cart, than it makes more sense to be a function than a method
      That's my 2 penny thought on the subject.

  • @abraham_o
    @abraham_o 22 години тому

    Is it just me or did Arjan just pronounced Repository funny?
    PS: I love watching your videos.

  • @gweb214
    @gweb214 2 дні тому +4

    Ooo Arjan using Jupyter now finally ❤

  • @devin865
    @devin865 2 дні тому

    So i'm trying to get a better understanding of type hints, but your example is one reason why I get annoyed with them. The type hint Iterable indicates the function could accept any iterable, but that's not entirely true. The sum function CAN accept any iterable but the len function needs a sequence (or an object that has __len__ method). So if you pass in a generator to your function, which is definitely an iterable object, it would throw a type error.

  • @buchi8449
    @buchi8449 День тому

    I am interested in advice about these techniques from the opposite point of view-when we should NOT use them.
    I sometimes see overuse of some of these techniques, such as a context manager without context to manage or list comprehension to just iterate an operation without taking returned values.

  • @mohl-bodell2948
    @mohl-bodell2948 День тому

    You missed lambda functions and passing functions as parameters. Immensely useful in many situations; many of the features you show are built with them.

  • @rafiullah-zz1lf
    @rafiullah-zz1lf 2 дні тому

    You summed up my python knowledge in a video. And here i thought i could be a software developer some day😅

  • @simondrew2914
    @simondrew2914 День тому

    You missed documentation....

    • @materialMammal
      @materialMammal День тому

      Not entirely. Type hints was covered. Docstrings were not. But yeah. I'd have loved to see something on docstrings

  • @rafiullah-zz1lf
    @rafiullah-zz1lf 2 дні тому +1

    Just a thought make how to write a more advanced software.

  • @Klej0aka0Klej
    @Klej0aka0Klej День тому

    map and filter should not be used specially in cases you showed, that's what comprehension is for

  • @nightvideoshoots3351
    @nightvideoshoots3351 2 дні тому +1

    Python creators:
    Oh let’s create language without types
    Python developers after 20 years:
    😂

    • @clasdauskas
      @clasdauskas 2 дні тому

      Exactly!
      Let's have a language which isn't C/C++/add your own to the list .... "ooh, look, it's almost the same as ...!"

    • @squishy-tomato
      @squishy-tomato 2 дні тому

      even though it's dynamically typed, it was always strongly typed

  • @AndreaDalseno
    @AndreaDalseno День тому

    Actually, banana is as long as cherry 😊

  • @jhg12989
    @jhg12989 2 дні тому

    darn no table of contents ;()

  • @uscan
    @uscan 14 годин тому

    I don’t think encouraging nested loops in one liners is good to recommend. If you need to debug, you will have to unroll it anyways.
    I think if you are using python it’s not for speed, use explicitly clear code, not one liners that are harder to understand.
    Source: import this

  • @cabanford
    @cabanford День тому

    Tip #1: learn Go.

  • @MsKepher
    @MsKepher День тому

    The content feels too basic, while quite slow. Lately I have the feeling topics are explained quite shallowly

  • @irlshrek
    @irlshrek 2 дні тому +1

    the best thing experienced python developers can do to become better at coding in python is get comfortable coding in rust and using it's idioms

    • @gonecoastaltoo
      @gonecoastaltoo 2 дні тому +1

      or a LISP variant like scheme - correctly applying functional idioms will up your python game

  • @habahrami
    @habahrami День тому

    Python zipped