I help folks sharpen their Python skills with https://PythonMorsels.com 🐍🍪
YIMBY. 95% vegan.
Avoid manually merging dictionaries. Use "a | b" instead. 🧵
Let's say you have 2 dictionaries:
context = {"language": "en", "timezone": "UTC"}
more_context = {"title": "Home", "breadcrumbs": ["Home"]}
And you'd like to merge them together...
#Python #DailyPythonTip
Avoid manually merging dictionaries. Use "a | b" instead. 🧵
Let's say you have 2 dictionaries:
context = {"language": "en", "timezone": "UTC"}
more_context = {"title": "Home", "breadcrumbs": ["Home"]}
And you'd like to merge them together...
#Python #DailyPythonTip
Instead of reaching for match-case as a switch-case replacement, I'd recommend using a dictionary.
Also see today's #DailyPythonTip on replacing if-elif chains with dictionaries.
Instead of reaching for match-case as a switch-case replacement, I'd recommend using a dictionary.
Also see today's #DailyPythonTip on replacing if-elif chains with dictionaries.
Consider using dictionaries to replace if-elif chains. 🧵
#Python #DailyPythonTip
Consider using dictionaries to replace if-elif chains. 🧵
#Python #DailyPythonTip
Use dictionaries for grouping items (by using a dictionary of lists, sets, or other data structures). 🧵
This week's tips are all about dictionaries.
#Python #DailyPythonTip
Use dictionaries for grouping items (by using a dictionary of lists, sets, or other data structures). 🧵
This week's tips are all about dictionaries.
#Python #DailyPythonTip
Avoid overusing the term "method". 🧵
In Python, the words "function" and "method" are not interchangeable.
All methods are functions, but not all functions are methods.
#Python #DailyPythonTip
Avoid overusing the term "method". 🧵
In Python, the words "function" and "method" are not interchangeable.
All methods are functions, but not all functions are methods.
#Python #DailyPythonTip
When accepting any number of objects to a function, consider when to accept an iterable and when to use * instead. 🧵
#Python #DailyPythonTip
When accepting any number of objects to a function, consider when to accept an iterable and when to use * instead. 🧵
#Python #DailyPythonTip
If you're missing function overloading from another programming language, try default argument values as an alternative. 🧵
Default arguments are not a full replacement for function overloading, but they can often be used to reach a similar end.
#Python #DailyPythonTip
If you're missing function overloading from another programming language, try default argument values as an alternative. 🧵
Default arguments are not a full replacement for function overloading, but they can often be used to reach a similar end.
#Python #DailyPythonTip
Instead of assigning a lambda function to a variable, use a "def" statement. 🧵
Instead of this:
square = lambda n: n**2
Write this:
def square(n): return n**2
#Python #DailyPythonTip
Instead of assigning a lambda function to a variable, use a "def" statement. 🧵
Instead of this:
square = lambda n: n**2
Write this:
def square(n): return n**2
#Python #DailyPythonTip
When calling "functions", think in terms of "callables". 🧵
Most Python programmers occasionally use the word "function" to describe a class.
So when you see the word "function", unless we're talking about a function definiton, think "callable".
#Python #DailyPythonTip
When calling "functions", think in terms of "callables". 🧵
Most Python programmers occasionally use the word "function" to describe a class.
So when you see the word "function", unless we're talking about a function definiton, think "callable".
#Python #DailyPythonTip
This is a game I often play with my students to demonstrate that the word "function" is a bit fuzzy in the #Python world.
This is a game I often play with my students to demonstrate that the word "function" is a bit fuzzy in the #Python world.
Good news: if you're in SF & have free time tomorrow afternoon you can come hang out with me and @anthrocypher.bsky.social in a popup casual learning event ❤️
luma.com/rdw3dq4h
Good news: if you're in SF & have free time tomorrow afternoon you can come hang out with me and @anthrocypher.bsky.social in a popup casual learning event ❤️
luma.com/rdw3dq4h
Remember that in Python, functions are objects. 🧵
If you need to call a bit of code later, you don't need to make a class with a method... you could just make a function!
#Python #DailyPythonTip
Remember that in Python, functions are objects. 🧵
If you need to call a bit of code later, you don't need to make a class with a method... you could just make a function!
#Python #DailyPythonTip
Don't sleep on Python's keyword arguments / named arguments. 🧵
When you have the choice between a positional argument or a keyword argument, choose a keyword argument if it clarifies the argument's purpose.
#Python #DailyPythonTip
Don't sleep on Python's keyword arguments / named arguments. 🧵
When you have the choice between a positional argument or a keyword argument, choose a keyword argument if it clarifies the argument's purpose.
#Python #DailyPythonTip
When processing potentially large untrusted files, don't iterate over them. 🧵
#Python #DailyPythonTip
When processing potentially large untrusted files, don't iterate over them. 🧵
#Python #DailyPythonTip
If you need to preserve the original line endings when reading or writing a file, use newline="" when opening your file. 🧵
#Python #DailyPythonTip
If you need to preserve the original line endings when reading or writing a file, use newline="" when opening your file. 🧵
#Python #DailyPythonTip
Consider file modes outside of read and write: you may (very occasionally) want a different mode. 🧵
#Python #DailyPythonTip
Consider file modes outside of read and write: you may (very occasionally) want a different mode. 🧵
#Python #DailyPythonTip
When processing small files line-by-line, just read the whole thing into memory. 🧵
Reading files line-by-line with a "for" loop is a good practice for files that could be large, but for small files I would recommend reading the whole file at once.
#Python #DailyPythonTip
When processing small files line-by-line, just read the whole thing into memory. 🧵
Reading files line-by-line with a "for" loop is a good practice for files that could be large, but for small files I would recommend reading the whole file at once.
#Python #DailyPythonTip
Read large files line-by-line by looping over them. 🧵
If you are working with a file that could be very large (such as a log file that could be gigabytes in size), don't read the whole file all at once.
#Python #DailyPythonTip
Read large files line-by-line by looping over them. 🧵
If you are working with a file that could be very large (such as a log file that could be gigabytes in size), don't read the whole file all at once.
#Python #DailyPythonTip
In #Python (largely thanks to our embrace of duck typing), looping is looping.
In #Python (largely thanks to our embrace of duck typing), looping is looping.
All existing users will now see 64 quiz questions in Jumpstart.
In total, Jumpstart includes 60 instructional videos, 64 quizzes, & 48 exercises. It's now VERY hands-on.
All existing users will now see 64 quiz questions in Jumpstart.
In total, Jumpstart includes 60 instructional videos, 64 quizzes, & 48 exercises. It's now VERY hands-on.
Consider printing to files instead of writing. 🧵
If you're converting objects to strings to write them to a file, I'd use print() instead.
And if you're putting newlines in string literals in your write() calls, I'd consider switching to print().
#Python #DailyPythonTip
Consider printing to files instead of writing. 🧵
If you're converting objects to strings to write them to a file, I'd use print() instead.
And if you're putting newlines in string literals in your write() calls, I'd consider switching to print().
#Python #DailyPythonTip