10 Python Basics You Should Know!

Поделиться
HTML-код
  • Опубликовано: 10 июл 2024
  • 10 Must Know Python Basics and More Tips And Tricks. How many did you already know?
    Get my Free NumPy Handbook:
    www.python-engineer.com/numpy...
    ✅ Write cleaner code with Sourcery, instant refactoring suggestions in VS Code & PyCharm: sourcery.ai/?... *
    ⭐ Join Our Discord : / discord
    📓 ML Notebooks available on Patreon:
    / patrickloeber
    If you enjoyed this video, please subscribe to the channel:
    ▶️ : / @patloeber
    ~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
    🖥️ Website: www.python-engineer.com
    🐦 Twitter - / patloeber
    ✉️ Newsletter - www.python-engineer.com/newsl...
    📸 Instagram - / patloeber
    🦾 Discord: / discord
    ▶️ Subscribe: / @patloeber
    ~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~
    🅿 Patreon - / patrickloeber
    #Python
    Timeline:
    00:00 - Intro
    00:08 - 01 Remove elements in List while looping
    01:33 - 02 If _name_ == "__main__" do?
    02:56 - 03 Check if a file or directory exists
    03:58 - 04 Find the index of an item in a List
    04:35 - 05 Execute a Program or System Command
    05:13 - 06 Merge two Dictionaries
    05:55 - 07 Create a nested directory
    06:29 - 08 Difference classmethod and staticmethod
    07:54 - 09 Difference _str_ and _repr_
    08:55 - 10 Concatenate two Lists
    ----------------------------------------------------------------------------------------------------------
    * This is an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏

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

  • @bobsalita3417
    @bobsalita3417 2 года назад +38

    After every video I think, "Wish I had watched this when I first started Python. Life would have been much easier."

    • @patloeber
      @patloeber  2 года назад +2

      Glad you like it!

    • @miketharipr
      @miketharipr Год назад +2

      Build a time machine ... in Python!

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

    Another great collection of useful tips. Very pithy, so if you’re not particularly familiar with a concept, you need to examine it more closely. A very effective way of presenting. Thanks!!

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

    Thank you! Just thank you!

  • @antonikaminski8664
    @antonikaminski8664 6 дней назад

    the best python video i've seen, thank you man. it could be linked in PEP or something

  • @CodeWithTomi
    @CodeWithTomi 2 года назад +1

    Great video and congrats on 50k!

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

    Very informative and well edited!

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

    The last one is truly helpful !

  • @user-or7ji5hv8y
    @user-or7ji5hv8y 2 года назад +1

    Great video. So clearly explained and concise.

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

      Thank you very much!

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

    Great video and explanations! Looking forward to more videos!

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

      Thanks for watching!

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

    Very great and excellent video! I learned a lot from it.
    Lot’s of greetings, Dennis 🇳🇱

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

    simply awesome!

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

    Great video, subscribed.

  • @deutschersyrer
    @deutschersyrer 2 года назад +10

    Thank you very much!
    I just wanted to share a note regarding removing elements from the list:
    For the examples you gave on "Removing elements in the list while looping", there is a big deference in:
    1. creating a copy of the list with list slicing.
    2. using list comprehension and assigning that directly to the list.
    which is...
    imaging that you have:
    a = [1, 2, 2, 3, 4]
    and you have another list pointing to the first list reference:
    b = a
    You have two scenarios:
    1. if you assigned a sliced copy of that first list to a list comprehension:
    a = [1, 2, 2, 3, 4]
    b = a
    a[:] = [x for x in a if not even(x)]
    you are modifying the list "in-place". Thus, "b" will be equal to the NEW list "a" >> both a and b references are pointing to the new list "a".
    2: if you assigned the original list directly to a list comprehension:
    1. if you used list slicing:
    a = [1, 2, 2, 3, 4]
    b = a
    a = [x for x in a if not even(x)]
    you are modifying the list "out-of-place". Thus, "b" will still point to the OLD "a" >> b is the old a but a is now new a.
    This is not recommended since you might forget about b (or any other list which is pointing the original list) unless you are aware of that.
    Thank you very much again you!

    • @patloeber
      @patloeber  2 года назад +3

      Yes thank you very much for clarification :) I could not fit all of this into this video, but yeah you are right!

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

      @@patloeber You've covered the gist of it which was perfect. I learn a lot from your videos :)

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

      This is EXACTLY the kind of thing that blows my head apart. I mean, I get it, I have to read it, re-read it, think a bit, then re-read it again... I guess what I'm trying to understand, what is the preferred method IF, you wish the original list to retain it's contents while getting a newlist 0f not evens? I need to read up to comprehend list comprehension. I guess the other thing is create some lists and step through, print results and review the outcomes.

  • @Param3021
    @Param3021 2 года назад +6

    8:38 Nice rule of thumb, with this example of developers and customers, I have cleared my confusion.
    Thank you Patrick Sir 😊

    • @patloeber
      @patloeber  2 года назад +1

      Glad it was helpful!

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

      Agreed wholeheartedly. I actually "got" that one, lol.

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

    this is really helpful to me. Thanks!

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

    Thank you for making this It's very useful

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

      Glad it was helpful!

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

    Thank you very much for this phantastic video.

  • @war-c0mmander
    @war-c0mmander 2 года назад

    Thank you :)

  • @justanotherrandomguywithou2047
    @justanotherrandomguywithou2047 2 года назад +2

    Vielen Dank für die zahlreichen hervorragenden Python Videos, wobei dieses hier keine Ausnahme ist. Vor allem mit deinen Pytorch Videos hast du mir Neuronale Netze deutlich einfacher gemacht. Nochmals vielen Dank Patrick.
    Bisher habe ich es aber leider versäumt dir dies zu schreiben.

    • @patloeber
      @patloeber  2 года назад +1

      Danke, das freut mich!

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

    Hello Patrik, thank you very much. I'm glad to find your channel. I am programming and learning Python since 3 Year, and I am wondering why I didn't find your channel till now. Thanks to twitter.

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

    This is great stuff. The ultimate test of my ability will be the ability to "stick with you" without hitting pause and/or rewinding repeatedly. As a newb, I find things can go over my head REAL quick. Especially in references to things like 8 - classmethod vs static method... You are for certain a "no messing around kinda guy" on this stuff. Straight, to the point, and move through the material. If we were in a class, I'd be that guy who always jammed my hand in the air and your response would be "Yes Jack, what is it now". It's amazing how one statement can lead to a MILLION questions. Nevertheless, thank you, this is GOLD. Always worthwhile.

    • @KanuKing20
      @KanuKing20 2 года назад +1

      As a noob myself I can assure you that this is not noob material or python basics at all. The title is a little bit misleading.

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

    thanks for such content

    • @patloeber
      @patloeber  2 года назад +1

      Glad you enjoy it!

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

    You are awesome! 👏

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

    nice video Patrick, have a great day.

    • @patloeber
      @patloeber  2 года назад +1

      Thanks, same to you!

  • @dougsigman9497
    @dougsigman9497 2 года назад +24

    Your correct versions of number 01 are very good, definitely more pythonic, and probably more efficient than mine.
    But there is an old solution that I just tried which also works. I used an exact copy of your original code with one change to the for loop line:
    for item in a[::-1]:
    This change loops through the reversed version of the list. That way when you remove an even item, the odd items that may exist after that even item in the non-reversed list will end up in their correct places.
    Like I said, it's an old "trick" from when I was writing in C (not C++) on MS-DOS.

    • @patloeber
      @patloeber  2 года назад +3

      Thanks! Yes looping in reversed order is also possible :)

    • @arkie87
      @arkie87 2 года назад +2

      I stumbled upon that trick when I was first learning python and running into issues modifying lists while iterating through them. I feel like iterating over a copy is more robust and requires less thought.

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

      @@arkie87 Looping backwards is better practice for code quality. Why allocate a whole array if you don't have to. There is a difference between code and good code and choosing which method to do it makes the whole difference. Although I would suggest using traditional for loop for traversing backwards as reversing a list is also time consuming. These small optimizations come in handy when you are working with limited processing and memory power like embedded systems.

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

    Nice video bro 🔥

  • @zacky7862
    @zacky7862 2 года назад +1

    The BEST python instructor. Thanks for this.
    ps. I like the theme you use on that tutorial. May I know which theme is that? is it available for vscode?

    • @patloeber
      @patloeber  2 года назад +1

      Thank you! It's the Blackboard theme and yes it's available for VS Code

  • @MFM88832
    @MFM88832 11 месяцев назад

    Great list. Just for number 6, wouldn't zip() be the best method?

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

    King, you dropped your crown 👑

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

    i love all your videos and they really have been helpful . Can someone please tell what IDE he uses for python in his advanced python concepts playlist.

    • @patloeber
      @patloeber  2 года назад +1

      VS Code. I have a video about my setup here on this channel

  • @banana-breakfast
    @banana-breakfast 2 года назад +9

    For #1 you could just loop backwards to remove elements. This is simpler and avoids copying the array

    • @flamendless
      @flamendless 2 года назад +1

      I was about to say this. Yes this is much better than creating a copy of a list and is much efficient in memory and performance

    • @LNTraCricEd
      @LNTraCricEd Год назад +1

      How will you loop backwards? Can you please enlighten me?

    • @datbubby
      @datbubby Год назад +1

      @@LNTraCricEd index using [::-1]

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

    Thnx

  • @Mufti199
    @Mufti199 2 года назад +1

    Great video. Try using "with" to open files. Makes life so much easier and you don't have to close it later either because it does it automatically for you.

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

      Hi Gazi, a question with that (coming from a newb). Is that the "pythonic" way? Aka, even though it does it automatically (garbage cleanup?) will many recommend we make explicit calls to close vs assuming cleanup?

    • @teodorlamort3864
      @teodorlamort3864 Год назад +1

      @@TheJacklwilliams when you use a with statement, the created object is closed after the with block is exited. This can happen either by continuing on to the code following it, by returning a value from a function, or when an exception is raised. Either way, you're guaranteed that it will close. It's the pythonic and recommended way for all kinds of objects that need closing.

    • @TheJacklwilliams
      @TheJacklwilliams Год назад

      @@teodorlamort3864 Many thanks Teodor and, of course I can see why then this is the “pythonic way”.

  • @MrAI_Rohan
    @MrAI_Rohan 2 года назад +2

    Great video. You can also use .extend() method for last one, It accepts any kind of iterable and add it to the list.
    I didn't knew how to deal with 2, 5 and 7. Do you know any tutorial for dealing with directories?

    • @patloeber
      @patloeber  2 года назад +3

      Yep nice tip! extend() is an in-place update, though :) ... Hmm no, maybe I should create one :D

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

      @@patloeber Looking forward to it :)

  • @7Trident3
    @7Trident3 2 года назад

    Very nice channel! Your enunciation, and precision with the english language, adds to the clarity. Just a wild guess, you are also a German speaker? Thank you for giving!

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

      Yes I am, can't hide my accent :D Thank you!

    • @7Trident3
      @7Trident3 2 года назад +2

      @@patloeber My wife's family is German, that accent associated with engineering, is a good thing!

    • @patloeber
      @patloeber  2 года назад +1

      @@7Trident3 haha thank you! :)

  • @madorops6043
    @madorops6043 2 года назад +1

    How i use a sprite? Where i put the images?
    Pls help

  • @tylercoombs1
    @tylercoombs1 Год назад

    with subprocess if you want to see the results you have to write shell=True, after your list of commands.
    I think the os library is inherently more vulnerable and poses security issues, this is one of the main reasons why people recommend pathlibs and subprocess

  • @ThePirateHistory
    @ThePirateHistory 2 года назад +1

    When will a ready-made Python production course be available?

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

      What type of application would you be interested in?

  • @huyphamle8040
    @huyphamle8040 2 года назад +1

    For 1st example, I suggest another case with `a = list(filter( lambda x: x%2!=0, a))`

    • @patloeber
      @patloeber  2 года назад +1

      Oh yes the good old filter method :)

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

    In last example if we have to print 1,3,2,4 then what will be the code ?
    Pls somebody help me

  • @player-eric
    @player-eric 2 года назад

    Hi! I want to ask a small question about Python, i. e. is it possible to get the name of a variable inside via a function, like, str = name(a), # str = `a`?

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

      Why would you want the name of a variable? Seems a bit like an xy problem, if you're still interested in getting all variables available to you, you can use globals() to get all globally defined classes, functions and variables, or local() to do the same but only limited to the local namespace

    • @towu
      @towu 2 года назад +1

      Essentially, could you tell us more about why you're trying to find the name of a variable?

  • @tfr
    @tfr 2 года назад +1

    For beginners: When he mentions "Dunder" (dee-under) he means double underscore (__)

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

    Which software did you use to write the code and make the presentation in these videos ?
    It seems to be some kinda background with nice color for python code, not an actual IDE :)

    • @patloeber
      @patloeber  2 года назад +1

      good observation! These are actually Screenshots made with carbon.now.sh/

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

      @@patloeber Thanks :)

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

    how to find the index(for the particular item) in a set data structure?

    • @patloeber
      @patloeber  2 года назад +1

      A set is an an unordered collection of unique elements, so the items don't have indices. You can convert it to a list and then call .index()

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

    Which editor is it ?

    • @patloeber
      @patloeber  2 года назад +1

      These are actually Screenshots made with carbon.now.sh/

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

    With the right beat, this would be a future rap hit.

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

      that's a new comment for me😂

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

      You rap at 77bpm. I may even upload a video to prove it lmao. Just listen to your video at the same time as this video and you'll see what I mean. It'd kinda cool tbh ruclips.net/video/aehB8f5l_2Y/видео.html

  • @SagarKumar-fx4ew
    @SagarKumar-fx4ew 2 года назад

    sir i get this error for your ai snake video please help sir 🙏🙏🙏🙏🥺🥺🥺
    pygame.error: Application didn't initialize properly, did you include SDL main.h in the file containing your main() F unction?

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

    Quick Python Refactoring Tips (30 Second videos): ruclips.net/video/D4xLPfetZRY/видео.html

  • @legitjimmyjaylight8409
    @legitjimmyjaylight8409 Год назад

    Or just loop backwards as you remove.

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

    I have a feeling you did not truly understand the class and static method, I'm confused as to what their actual purpose is

  • @rakeshhalijol
    @rakeshhalijol 2 года назад +1

    1st view ❤️

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

    it's my birthday today 👦👦

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

      Happy Birthday then!

  • @parshuram3272
    @parshuram3272 Год назад

    you make everyone getting lazy in other languages🤣🤣🤣🤣

  • @flamendless
    @flamendless 2 года назад +1

    Why do you say "d-under" instead of "dunder" 😂