10 Tips to Become REALLY Good at Python

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

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

  • @ArjanCodes
    @ArjanCodes  11 дней назад +1

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

  • @mrrobotman5299
    @mrrobotman5299 11 дней назад +26

    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'

    • @M3MYS3LF1979
      @M3MYS3LF1979 3 дня назад

      Dynamic fields are sweet. Makes stdout much prettier if you aren't using something like rich

  • @mikelauderdale4845
    @mikelauderdale4845 10 дней назад +9

    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. ☺

  • @TheSadilek
    @TheSadilek 6 дней назад +1

    You’re an amazing teacher and I’ve learned so much from you. Please never top producing videos. Much respect en bedankt!

  • @jmreagle
    @jmreagle 11 дней назад +60

    I wish there were chapter/section marks.

    • @cetilly
      @cetilly 10 дней назад +3

      There are. Expand the video description

    • @denisquarte7177
      @denisquarte7177 10 дней назад +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.

  • @gonecoastaltoo
    @gonecoastaltoo 10 дней назад +7

    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 10 дней назад +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.

  • @damymetzke514
    @damymetzke514 11 дней назад +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 10 дней назад

      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".

  • @kilianklaiber6367
    @kilianklaiber6367 11 дней назад +1

    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.

  • @omarcrosby
    @omarcrosby 10 дней назад +1

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

  • @jlinenberg
    @jlinenberg 5 дней назад

    Thank you @ArjanCodes for this video, really useful and enjoy seeing it. Another tip (for people with anxiety like me) could be a good idea split the video in sections, maybe a section for each tip, so people can jump in directly with the tip we need to reinforcement.

    • @ArjanCodes
      @ArjanCodes  6 часов назад

      Hi, usually I add chapters to each video. I've noticed that something went wrong in the last few videos. If should be fixed now. :)

  • @ulyssep2997
    @ulyssep2997 11 дней назад +1

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

  • @sylvainprive1754
    @sylvainprive1754 10 дней назад +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 9 дней назад

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

  • @elefantsnablar
    @elefantsnablar 10 дней назад +11

    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!

  • @mathman316
    @mathman316 11 дней назад +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.

  • @DrGreenGiant
    @DrGreenGiant 10 дней назад +1

    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.

  • @xiggywiggs
    @xiggywiggs 11 дней назад +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!

  • @pascalhessler2803
    @pascalhessler2803 7 дней назад

    I like partial functions. They can simplify the code greatly if you have a function with many arguments that are reused often.

  • @ross-spencer
    @ross-spencer 7 дней назад

    f-strings are great but I have struggled with performance in the past where f-strings were slower than plain concat. In the end, and in that instance, I made my error handling more robust and went for the less safe option.

  • @danilshein4612
    @danilshein4612 11 дней назад +1

    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

  • @evgeniyevgeniy8352
    @evgeniyevgeniy8352 11 дней назад +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  11 дней назад +2

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

    • @rafiullah-zz1lf
      @rafiullah-zz1lf 11 дней назад

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

    • @kilianklaiber6367
      @kilianklaiber6367 11 дней назад +1

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

  • @ayoubarahmat
    @ayoubarahmat 3 дня назад

    this video refresh my memory in python

  • @ms070965
    @ms070965 10 дней назад

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

  • @krzysiekkrzysiek9059
    @krzysiekkrzysiek9059 11 дней назад

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

  • @mosesodalo5074
    @mosesodalo5074 9 дней назад

    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

  • @IdPreferNot1
    @IdPreferNot1 10 дней назад

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

  • @Gigusx
    @Gigusx 10 дней назад

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

    • @omarcrosby
      @omarcrosby 10 дней назад +1

      yeah that one made me say woah!!!

  • @RedSpark_
    @RedSpark_ 10 дней назад +3

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

    • @Naej7
      @Naej7 10 дней назад

      What does it do then ? When is it closed ?

    • @arnoutstandaert
      @arnoutstandaert 9 дней назад

      ​@@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.

  • @dmitryutkin9864
    @dmitryutkin9864 8 дней назад

    Nice vid - thanks for sharing

  • @royjanik1615
    @royjanik1615 10 дней назад +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 9 дней назад +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.

  • @marcin2x4
    @marcin2x4 9 дней назад

    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!

  • @spacegirl1108
    @spacegirl1108 10 дней назад

    usefull tips. Thx Arjan.

  • @orgadish
    @orgadish 10 дней назад +1

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

    • @chatcharinsangbutsarakum5963
      @chatcharinsangbutsarakum5963 10 дней назад

      Because `sum` requires `Iterable`?

    • @ИванАхременко
      @ИванАхременко 10 дней назад

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

    • @orgadish
      @orgadish 10 дней назад +1

      Ah yes, it requires Sized AND Iterable = Collection

  • @yickysan
    @yickysan 10 дней назад

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

  • @mdata.youtube
    @mdata.youtube 10 дней назад

    Thank you, great video!

  • @CNich90
    @CNich90 10 дней назад

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

    • @ArjanCodes
      @ArjanCodes  8 дней назад +1

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

    • @CNich90
      @CNich90 8 дней назад

      @ Thank you!!!

  • @twentytwentyeight
    @twentytwentyeight 9 дней назад

    Love the thumbnail, Arjan 😂😂😂

  • @bogaczew
    @bogaczew 10 дней назад

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

  • @dmbrv
    @dmbrv 11 дней назад

    Thanks for the video

  • @abraham_o
    @abraham_o 9 дней назад

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

  • @rafiullah-zz1lf
    @rafiullah-zz1lf 11 дней назад +11

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

  • @conceptrat
    @conceptrat 10 дней назад

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

  • @buchi8449
    @buchi8449 10 дней назад

    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.

  • @devin865
    @devin865 10 дней назад

    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.

  • @rafiullah-zz1lf
    @rafiullah-zz1lf 11 дней назад

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

  • @gweb214
    @gweb214 11 дней назад +4

    Ooo Arjan using Jupyter now finally ❤

  • @mohl-bodell2948
    @mohl-bodell2948 9 дней назад

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

  • @manuelstausberg8923
    @manuelstausberg8923 11 дней назад +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 10 дней назад

      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 10 дней назад

      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 9 дней назад

      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.

    • @EvgenyLushev
      @EvgenyLushev 8 дней назад

      @@mac68tm Yes -> instance method, No -> class method. Or static method. I just do not clearly see an upside of a standalone tightly-couple function in such scenario.

    • @mac68tm
      @mac68tm 8 дней назад

      @ I'm not sure what you mean by `tightly-couple function`. It's a generic function that calculates a discount (could be for a shopping cart, a subscription, etc.). There is no coupling as such. So if everybody gets the same discount why would I want to clutter a class with another method when it doesn't change the applied discount from instance to instance?

  • @Klej0aka0Klej
    @Klej0aka0Klej 10 дней назад

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

  • @rafiullah-zz1lf
    @rafiullah-zz1lf 11 дней назад +1

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

  • @uscan
    @uscan 9 дней назад

    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

  • @nightvideoshoots3351
    @nightvideoshoots3351 11 дней назад +1

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

    • @clasdauskas
      @clasdauskas 10 дней назад

      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 10 дней назад

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

  • @simondrew2914
    @simondrew2914 10 дней назад

    You missed documentation....

    • @materialMammal
      @materialMammal 10 дней назад

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

  • @janvanwijk5979
    @janvanwijk5979 3 дня назад

    Cool list! An additional tip for tip #1 (flattening the matrix): in python, it can be done even simpler, like this: flattened = list(itertools.chain.from_iterable(matrix))

  • @horselogy
    @horselogy 6 часов назад

    Actually great Python developers stays away from comprehensions, they are killer of the readability.

  • @AndreaDalseno
    @AndreaDalseno 10 дней назад

    Actually, banana is as long as cherry 😊

  • @cabanford
    @cabanford 10 дней назад

    Tip #1: learn Go.

  • @jhg12989
    @jhg12989 10 дней назад

    darn no table of contents ;()

  • @MsKepher
    @MsKepher 9 дней назад

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

  • @irlshrek
    @irlshrek 10 дней назад +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 10 дней назад +1

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

  • @habahrami
    @habahrami 10 дней назад

    Python zipped

  • @lukashk.1770
    @lukashk.1770 2 дня назад

    IMHO all these are begginer's topic