10 Tips to Become REALLY Good at Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025

ความคิดเห็น • 97

  • @ArjanCodes
    @ArjanCodes  10 วันที่ผ่านมา +1

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

  • @mrrobotman5299
    @mrrobotman5299 10 วันที่ผ่านมา +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 2 วันที่ผ่านมา

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

  • @mikelauderdale4845
    @mikelauderdale4845 9 วันที่ผ่านมา +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. ☺

  • @jmreagle
    @jmreagle 10 วันที่ผ่านมา +59

    I wish there were chapter/section marks.

    • @cetilly
      @cetilly 9 วันที่ผ่านมา +3

      There are. Expand the video description

    • @denisquarte7177
      @denisquarte7177 9 วันที่ผ่านมา +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.

  • @elefantsnablar
    @elefantsnablar 9 วันที่ผ่านมา +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!

  • @gonecoastaltoo
    @gonecoastaltoo 9 วันที่ผ่านมา +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 9 วันที่ผ่านมา +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 10 วันที่ผ่านมา +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 9 วันที่ผ่านมา

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

  • @jlinenberg
    @jlinenberg 4 วันที่ผ่านมา

    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.

  • @TheSadilek
    @TheSadilek 5 วันที่ผ่านมา

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

    • @ArjanCodes
      @ArjanCodes  5 วันที่ผ่านมา

      Thank you!

  • @kilianklaiber6367
    @kilianklaiber6367 10 วันที่ผ่านมา +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.

  • @ulyssep2997
    @ulyssep2997 10 วันที่ผ่านมา +1

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

  • @xiggywiggs
    @xiggywiggs 10 วันที่ผ่านมา +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!

  • @mathman316
    @mathman316 10 วันที่ผ่านมา +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.

  • @omarcrosby
    @omarcrosby 9 วันที่ผ่านมา +1

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

  • @RedSpark_
    @RedSpark_ 9 วันที่ผ่านมา +3

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

    • @Naej7
      @Naej7 9 วันที่ผ่านมา

      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.

  • @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 8 วันที่ผ่านมา

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

  • @ms070965
    @ms070965 9 วันที่ผ่านมา

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

  • @DrGreenGiant
    @DrGreenGiant 9 วันที่ผ่านมา +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.

  • @evgeniyevgeniy8352
    @evgeniyevgeniy8352 10 วันที่ผ่านมา +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  10 วันที่ผ่านมา +2

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

    • @rafiullah-zz1lf
      @rafiullah-zz1lf 10 วันที่ผ่านมา

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

    • @kilianklaiber6367
      @kilianklaiber6367 10 วันที่ผ่านมา +1

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

  • @Gigusx
    @Gigusx 9 วันที่ผ่านมา

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

    • @omarcrosby
      @omarcrosby 9 วันที่ผ่านมา +1

      yeah that one made me say woah!!!

  • @royjanik1615
    @royjanik1615 9 วันที่ผ่านมา +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 8 วันที่ผ่านมา +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.

  • @krzysiekkrzysiek9059
    @krzysiekkrzysiek9059 10 วันที่ผ่านมา

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

  • @pascalhessler2803
    @pascalhessler2803 6 วันที่ผ่านมา

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

  • @mosesodalo5074
    @mosesodalo5074 8 วันที่ผ่านมา

    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

    • @ArjanCodes
      @ArjanCodes  7 วันที่ผ่านมา

      Glad it was helpful!

  • @dmitryutkin9864
    @dmitryutkin9864 7 วันที่ผ่านมา

    Nice vid - thanks for sharing

    • @ArjanCodes
      @ArjanCodes  7 วันที่ผ่านมา

      Thanks for watching!

  • @danilshein4612
    @danilshein4612 10 วันที่ผ่านมา +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

  • @ayoubarahmat
    @ayoubarahmat 2 วันที่ผ่านมา

    this video refresh my memory in python

  • @ross-spencer
    @ross-spencer 6 วันที่ผ่านมา

    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.

  • @spacegirl1108
    @spacegirl1108 9 วันที่ผ่านมา

    usefull tips. Thx Arjan.

    • @ArjanCodes
      @ArjanCodes  7 วันที่ผ่านมา

      Glad it was helpful!

  • @IdPreferNot1
    @IdPreferNot1 9 วันที่ผ่านมา

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

    • @ArjanCodes
      @ArjanCodes  7 วันที่ผ่านมา

      Great to hear!

  • @mdata.youtube
    @mdata.youtube 9 วันที่ผ่านมา

    Thank you, great video!

    • @ArjanCodes
      @ArjanCodes  7 วันที่ผ่านมา

      You are welcome!

  • @marcin2x4
    @marcin2x4 8 วันที่ผ่านมา

    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!

  • @CNich90
    @CNich90 9 วันที่ผ่านมา

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

    • @ArjanCodes
      @ArjanCodes  7 วันที่ผ่านมา +1

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

    • @CNich90
      @CNich90 7 วันที่ผ่านมา

      @ Thank you!!!

  • @orgadish
    @orgadish 9 วันที่ผ่านมา +1

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

    • @chatcharinsangbutsarakum5963
      @chatcharinsangbutsarakum5963 9 วันที่ผ่านมา

      Because `sum` requires `Iterable`?

    • @ИванАхременко
      @ИванАхременко 9 วันที่ผ่านมา

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

    • @orgadish
      @orgadish 9 วันที่ผ่านมา +1

      Ah yes, it requires Sized AND Iterable = Collection

  • @rafiullah-zz1lf
    @rafiullah-zz1lf 10 วันที่ผ่านมา +11

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

  • @twentytwentyeight
    @twentytwentyeight 8 วันที่ผ่านมา

    Love the thumbnail, Arjan 😂😂😂

  • @dmbrv
    @dmbrv 10 วันที่ผ่านมา

    Thanks for the video

  • @yickysan
    @yickysan 9 วันที่ผ่านมา

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

  • @bogaczew
    @bogaczew 9 วันที่ผ่านมา

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

  • @conceptrat
    @conceptrat 9 วันที่ผ่านมา

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

  • @abraham_o
    @abraham_o 8 วันที่ผ่านมา

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

  • @gweb214
    @gweb214 10 วันที่ผ่านมา +4

    Ooo Arjan using Jupyter now finally ❤

  • @manuelstausberg8923
    @manuelstausberg8923 10 วันที่ผ่านมา +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 9 วันที่ผ่านมา

      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 9 วันที่ผ่านมา

      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 8 วันที่ผ่านมา

      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 7 วันที่ผ่านมา

      @@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 7 วันที่ผ่านมา

      @ 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?

  • @rafiullah-zz1lf
    @rafiullah-zz1lf 10 วันที่ผ่านมา

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

  • @mohl-bodell2948
    @mohl-bodell2948 8 วันที่ผ่านมา

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

  • @janvanwijk5979
    @janvanwijk5979 2 วันที่ผ่านมา

    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))

  • @buchi8449
    @buchi8449 9 วันที่ผ่านมา

    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 9 วันที่ผ่านมา

    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.

  • @nightvideoshoots3351
    @nightvideoshoots3351 10 วันที่ผ่านมา +1

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

    • @clasdauskas
      @clasdauskas 9 วันที่ผ่านมา

      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 9 วันที่ผ่านมา

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

  • @Klej0aka0Klej
    @Klej0aka0Klej 9 วันที่ผ่านมา

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

  • @AndreaDalseno
    @AndreaDalseno 9 วันที่ผ่านมา

    Actually, banana is as long as cherry 😊

  • @rafiullah-zz1lf
    @rafiullah-zz1lf 10 วันที่ผ่านมา +1

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

  • @simondrew2914
    @simondrew2914 9 วันที่ผ่านมา

    You missed documentation....

    • @materialMammal
      @materialMammal 9 วันที่ผ่านมา

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

  • @jhg12989
    @jhg12989 9 วันที่ผ่านมา

    darn no table of contents ;()

  • @cabanford
    @cabanford 9 วันที่ผ่านมา

    Tip #1: learn Go.

  • @uscan
    @uscan 8 วันที่ผ่านมา

    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

  • @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 9 วันที่ผ่านมา +1

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

  • @MsKepher
    @MsKepher 8 วันที่ผ่านมา

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

  • @lukashk.1770
    @lukashk.1770 วันที่ผ่านมา

    IMHO all these are begginer's topic

  • @habahrami
    @habahrami 9 วันที่ผ่านมา

    Python zipped