treyhunner Profile Banner
Trey Hunner Profile
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
Don't wanna be here? Send us removal request.
@treyhunner
Trey Hunner
3 years
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
@treyhunner
Trey Hunner
2 days
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
@grok
Grok
4 days
Join millions who have switched to Grok.
183
384
3K
@treyhunner
Trey Hunner
2 days
There's not always a one-to-one relationship between high-level operations and individual dunder methods. Read more 👉 #Python.
Tweet card summary image
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
@treyhunner
Trey Hunner
3 days
"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.
Tweet card summary image
pythonmorsels.com
When do you need a comment in Python and when should you consider an alternative to commenting?
1
1
4
@treyhunner
Trey Hunner
6 days
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.
Tweet card summary image
pythonmorsels.com
When do you need a comment in Python and when should you consider an alternative to commenting?
0
1
1
@treyhunner
Trey Hunner
13 days
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
@treyhunner
Trey Hunner
15 days
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!.
Tweet card summary image
raindrop.io
Quick cheat sheets and interactive tools for your day-to-day Python coding
0
1
3
@treyhunner
Trey Hunner
16 days
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.
Tweet card summary image
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
@treyhunner
Trey Hunner
17 days
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.
Tweet card summary image
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
@treyhunner
Trey Hunner
18 days
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
Tweet media one
0
0
1
@treyhunner
Trey Hunner
19 days
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.
Tweet card summary image
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
@treyhunner
Trey Hunner
19 days
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.
Tweet card summary image
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
@treyhunner
Trey Hunner
20 days
Whenever I see a dunder method called within code, I think "something low-level is going on here". #Python.
Tweet card summary image
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
@treyhunner
Trey Hunner
23 days
Unlike, JavaScript, C, Java, and many other programming languages we don't have traditional C-style for loops. Read more 👉 #Python.
Tweet card summary image
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
@treyhunner
Trey Hunner
24 days
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
@treyhunner
Trey Hunner
24 days
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.
Tweet card summary image
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
@treyhunner
Trey Hunner
25 days
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.
Tweet card summary image
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
@treyhunner
Trey Hunner
25 days
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
@treyhunner
Trey Hunner
26 days
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.
Tweet card summary image
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
@treyhunner
Trey Hunner
26 days
Need a lookup table in #Python?. You need a dictionary!.
Tweet card summary image
pythonmorsels.com
Python's dictionaries act as lookup tables which map keys to their values.
1
1
2