
Trey Hunner
@treyhunner
Followers
22K
Following
18K
Media
799
Statuses
14K
I share weekly Python tips in my newsletter & I run Python Morsels 🐍🍪 💌: https://t.co/s83bzhUjHC 🐘: https://t.co/Q5KO3SJBqI 🦋: https://t.co/HrIl3uLJ0L
San Diego, CA
Joined February 2014
I share a #Python tip every Wednesday in the @PythonMorsels newsletter. Recent tips included:. • avoiding complex string joins via print.• making more user-friendly classes.• making whitespace-sensitive multiline strings. Subscribe for a weekly tip 💌.
pythonmorsels.com
A weekly Python newsletter designed to fill-in gaps in your Python skills.
1
5
45
New video on the 3 ways to check your operating system in #Python. os.name is the least granular. sys.platform has well-documented values. platform.system() is the a runtime check (the others check the OS it was built for, not the one it's running on).
0
1
7
There's not always a one-to-one relationship between high-level operations and individual dunder methods. Read more 👉 #Python.
pythonmorsels.com
It's best to avoid calling dunder methods. It's common to define dunder methods, but uncommon to call them directly.
0
0
4
"Just as creating a function can give a name to an otherwise unnamed block of code, sometimes there's also an opportunity to create a new variable to clarify code." #Python.
pythonmorsels.com
When do you need a comment in Python and when should you consider an alternative to commenting?
1
1
4
So the mapping part of this comprehension has an "inline if" within it. Read more 👉 #Python.
pythonmorsels.com
While list comprehesions in Python don't support the else keyword directly, conditional expressions can be embedded within list comprehension.
0
0
7
Comments can be great, but try to avoid using them when a docstring, a better function name, a better variable name, or a new variable or function might be better. Read the full article: Avoid over-commenting in Python.▸ #Python.
pythonmorsels.com
When do you need a comment in Python and when should you consider an alternative to commenting?
0
1
1
Do you ever forget how to format a number like 1,234.50 in #Python?. Forget the weird f-string syntax for zero-padding and space-padding strings?. Me too. Over the weekend, I made a tool to help: Paste an example string to see the format spec you need.
5
2
17
I just added to my Python quick resource bookmarks list. I also realized it can act as a Python docs URL shortener: All my #Python quick resource bookmarks: Thanks @y2mango!.
raindrop.io
Quick cheat sheets and interactive tools for your day-to-day Python coding
0
1
3
If we have two lists and we wanted to know whether the items in these two lists are the same, we could use the equality operator (==). Read more 👉 #Python.
pythonmorsels.com
You can check whether iterables contain the same elements in Python with equality checks, type conversions, sets, Counter, or looping helpers.
0
0
4
Dunder methods essentially act as a sort of "hook", allowing us (Python programmers) to customize the behavior of built-in Python behaviors. Read more 👉 #Python.
pythonmorsels.com
It's best to avoid calling dunder methods. It's common to define dunder methods, but uncommon to call them directly.
0
0
1
In the 1960s, teleprinters used a carriage return (CR) to move the printhead back to the beginning of the line, and they used a line feed (LF) to move to the next line. Read more 👉 #Python
0
0
1
Loops that involve early break conditions can often be rewritten to use a helper utility that does the early break work for you, but not always (and sometimes it doesn't make them more readable). Read more 👉 #Python.
pythonmorsels.com
Python's break statement is handy for breaking out of a loop. But break statements can often be replaced by a more readable looping helper function.
0
0
2
New screencast on startswith/endswith & removeprefix/removesuffix in #Python. Don't use slicing or the various strip methods when there's a better way to check for (and remove) prefixes/suffixes.
pythonmorsels.com
Python's strings have methods for checking whether a string starts or ends with specific text and for removing prefixes and suffixes.
0
0
1
Whenever I see a dunder method called within code, I think "something low-level is going on here". #Python.
pythonmorsels.com
It's best to avoid calling dunder methods. It's common to define dunder methods, but uncommon to call them directly.
0
0
0
Unlike, JavaScript, C, Java, and many other programming languages we don't have traditional C-style for loops. Read more 👉 #Python.
pythonmorsels.com
Any reversible iterable can be reversed using the built-in reversed function whereas Python's slicing syntax only works on sequences.
0
0
6
My weekly office hour session is starting in 4 hours: Not sure when to use a class? Not understanding something about decorators? Have another #Python pondering?. Join me!
pythonmorsels.com
Join Trey for 13 office hours sessions. Free for Python Morsels subscribers!
0
0
1
To compare the items in a list with the items in a tuple, we could convert the list to a tuple or the tuple to a list, and then check for equality. Read more 👉 #Python.
pythonmorsels.com
You can check whether iterables contain the same elements in Python with equality checks, type conversions, sets, Counter, or looping helpers.
0
0
2
In Python, we love duck typing, which means we often care about the behavior of an object more than the type of that object. Read more 👉 #Python.
pythonmorsels.com
You can check whether iterables contain the same elements in Python with equality checks, type conversions, sets, Counter, or looping helpers.
0
0
5
Need some #Python help?. I'm holding an office hour session tomorrow @ 4pm US/Pacific Time. Join here for free:
pythonmorsels.com
Join Trey for 13 office hours sessions. Free for Python Morsels subscribers!
0
0
1
So we can use any(), all(), or Python's "in" operator to check conditions. but what if we need to get the actual value that matches rather than just a boolean indicating whether there was a match? #Python.
pythonmorsels.com
Python's break statement is handy for breaking out of a loop. But break statements can often be replaced by a more readable looping helper function.
0
0
7
Need a lookup table in #Python?. You need a dictionary!.
pythonmorsels.com
Python's dictionaries act as lookup tables which map keys to their values.
1
1
2