7 Simple Tricks to Write Better Python Code




[ad_1]

[https://www.buymeacoffee.com/cogsci] Visit http://python.cogsci.nl/ for more tutorials! Seven solutions to common small problems that Python programmers often solve in a suboptimal way. This video is understandable for beginners, but also contains useful advice for experienced programmers.

– 0:50 Using enumerate()
– 3:22 Using zip()
– 6:35 Tuple unpacking
– 9:40 Default dict values
– 12:42 For … else
– 16:13 File reading with ‘with’
– 19:40 Exception handling

Source


[ad_2]

Comment List

  • Sebastiaan Mathôt
    November 13, 2020

    One of the ways I use try: finally is when returning after a function. I have a function that modifies the data of a particular (interchangeable) file, and saving it after returning it is a must. So, I use a try: finally to save it 🙂

  • Sebastiaan Mathôt
    November 13, 2020

    I'm impressed that you play the bongos while coding.

  • Sebastiaan Mathôt
    November 13, 2020

    8:00 Ever heard of Haskell?

  • Sebastiaan Mathôt
    November 13, 2020

    Python по скайпу. Научу мыслить нестандартно. Решаем задачки, строим утилиты, игры. Data Science и всё, что с эти связано. Телега у меня в контактах. Напиши мне

  • Sebastiaan Mathôt
    November 13, 2020

    At last, someone explained what finally actually does.

  • Sebastiaan Mathôt
    November 13, 2020

    Is ur name dick?

  • Sebastiaan Mathôt
    November 13, 2020

    https://youtu.be/DSCif4aXhrQ
    Please subscribe my cancel … need support

  • Sebastiaan Mathôt
    November 13, 2020

    Thanks for the information.

  • Sebastiaan Mathôt
    November 13, 2020

    12:53 why not just use if and else? Seems to me easier to read than your for/else.
    if needle in haystack:
    pass
    else:
    pass

  • Sebastiaan Mathôt
    November 13, 2020

    shorter code != easier to read

  • Sebastiaan Mathôt
    November 13, 2020

    additionally you can use a ternary operation:
    print('Found!') if needle in haystack else print('Not found!')

    Which is wicked easy on the eyes

  • Sebastiaan Mathôt
    November 13, 2020

    I thought the else after the for loop was an extension of the if statement inside the loop, not an extension of the for loop itself.

  • Sebastiaan Mathôt
    November 13, 2020

    Wowwww, just wowww

  • Sebastiaan Mathôt
    November 13, 2020

    What the editor did you use?

  • Sebastiaan Mathôt
    November 13, 2020

    Great stuff

  • Sebastiaan Mathôt
    November 13, 2020

    Goeie video! Je hebt me dik geholpen met mn informatica project

  • Sebastiaan Mathôt
    November 13, 2020

    I know you're trying to illustrate for . . . else, but the same code can be written in one line:
    print("Found!") if needle in haystack else print("Not found!")

    This is Python's ternary operator, which uses the form
    doA if CONDITION else doB

  • Sebastiaan Mathôt
    November 13, 2020

    Merci !!!

  • Sebastiaan Mathôt
    November 13, 2020

    View this vid to clean up your Python, and to check if Pimpin' is in fact, 'easy'. Spoiler Alert: It's not.

  • Sebastiaan Mathôt
    November 13, 2020

    1. enumerate
    2. zip
    3. x,y = y,x
    4. dict.get(value-KeyExist, value-KeyNotExist)
    5. for-else loop
    6. for line in f: … (directly loop text file)
    7. try-except-else-finally

  • Sebastiaan Mathôt
    November 13, 2020

    We all know that the easiest way to print a message is to assume the worst and use a ternary to create a substring of the best case scenario:

    >>> print("Not Found!"[0 if needle not in haystack else -len("Found!")::])

    ♪ I did it myyyy waaaay ♫

  • Sebastiaan Mathôt
    November 13, 2020

    Ah, so pleasant to get the eardrums hammered by your typing. I suppose you moved on ever since, right?

  • Sebastiaan Mathôt
    November 13, 2020

    if you do for i in zip(x,y) doesnt the zip executes with every loop inside that for? Isnt it better to calculate zip beforehand (assuming we dont have dynamic lists)?

  • Sebastiaan Mathôt
    November 13, 2020

    #slightly better
    cities = ['Marseille', 'Amsterdam', "New York", "London"]

    for city in cities:

    print(city)

  • Sebastiaan Mathôt
    November 13, 2020

    I don’t get it. I tried it: if “dick” in “dicktionary”: return True. It returned true. But the dictionary was empty!

  • Sebastiaan Mathôt
    November 13, 2020

    It's still usefull even after 4 years, and some of these tips will be life savers in projects where the logic become very complicated. Thank you !

  • Sebastiaan Mathôt
    November 13, 2020

    I guess if you have been programming in C and C++ for years before starting Python you are most likely going to solve these problems the "bad way" lol.

  • Sebastiaan Mathôt
    November 13, 2020

    Pro tip: Invest to a proper microphone that doesn't have auto compression and pick up all your typing

  • Sebastiaan Mathôt
    November 13, 2020

    You dutch?

    Lol 369 comments

  • Sebastiaan Mathôt
    November 13, 2020

    Good as Gold Mate. Thanks.

  • Sebastiaan Mathôt
    November 13, 2020

    What is the name of that cute program you use?

  • Sebastiaan Mathôt
    November 13, 2020

    It cost you $0 to pick that name

  • Sebastiaan Mathôt
    November 13, 2020

    Really useful thanks for your time!

  • Sebastiaan Mathôt
    November 13, 2020

    if needle in haystack:
    print('Found!')
    else:
    print('Not Found!)

  • Sebastiaan Mathôt
    November 13, 2020

    Hi, what IDE are you using in this video? Thanks

  • Sebastiaan Mathôt
    November 13, 2020

    Please, next time dont use microfone to hit keys on your keyboard, use your fingers =D. That aside, great video.

  • Sebastiaan Mathôt
    November 13, 2020

    Great video

  • Sebastiaan Mathôt
    November 13, 2020

    goeie tips en heel goed uitgelegd! Bedankt!

  • Sebastiaan Mathôt
    November 13, 2020

    That is a nice looking IDE.. What is it?

  • Sebastiaan Mathôt
    November 13, 2020

    Ahh the keyboard sound is so disturbing

  • Sebastiaan Mathôt
    November 13, 2020

    Here's how to write efficient Python code: LATEST 2019 —- https://youtu.be/LI5O6rfe7zI

  • Sebastiaan Mathôt
    November 13, 2020

    .i. = 51

  • Sebastiaan Mathôt
    November 13, 2020

    Great Video .Thanks!

  • Sebastiaan Mathôt
    November 13, 2020

    1. enumerate
    2. zip
    3. x,y = y,x (tuple)

    4. ((get)) method
    5. in
    6. …

    *Speaking of `if-else` statement, do you even know this?*

    def foo():
    for i in range(3):
    print(i)
    else:
    print('done')

    it's a dumb trick, but **you can use that for doing a specific job after something is done**. (like finally statement)

Write a comment