Explore the World's Best Ideas
Join today and uncover 100+ curated journeys from 50+ topics. Unlock access to our mobile app with extensive features.
Hallelujah! That is what I thought when I learned about the Python 3.6+ update that includes a new way of formatting strings: the Python formatted string literal. String formatting in Python has come a long way.
F-strings consider everything within { curly brackets } as an expression, and with these expressions, we can do simple arithmetic but also functions and method calls!
3
54 reads
Making your Python code as re-usable as possible should be one of your main concerns. But what if you’re working on a Unix platform and your colleague is working on Windows?
The path delimiter on Windows is \
, but on my Linux or Mac system, it is /
. Avoid dealing with these nuances by using the built-in os
library:
2
29 reads
Unpacking variables are probably most used for functions that return multiple variables, such as in the example below.
But it is also useful for data types that contain multiple items. The only important notion here is that, if not otherwise defined, variable unpacking results in tuples
.
The _
operator is an unnamed variable , essentially a variable that you’re not interested in and won’t be doing anything with, for instance in the following case:
2
21 reads
Dictionaries are great data-types for storing values
with an attribute field known as the key
, in so-called key-value pairs
. When extracting key-value pairs from dictionaries, avoid running into KeyError
exceptions with the .get
method instead of the more traditional [key]
method. The .get
method provides a default value if the key is not present.
In line 6-10
our program stops running because of the KeyError
exception, but in line 11-14
our program continues, using the default 'undefined'
string that is set in line 12
as the second argument of the .get
method.
When you have the possibility of assigning a default
value for accessing key-value pairs in a dictionary, always try to use the .get
method to avoid your program from stopping prematurely, especially when in production.
2
17 reads
I love the zip
function and it has saved me countless (nested) loops. I use it mostly for iterating over two data types at the same time, where I need the indexes to be equal.
You can do this with any data type or generator. For instance, you could create dictionaries without looping over the separate lists, as such:
Later on, we’ll introduce the *args
operator, which in combination with the zip function
is very powerful!
2
18 reads
List, tuple, and dictionary comprehensions are ways to code more efficiently: do the same in fewer lines of code.
Both lines 2-4
and lines 13-15
are compressed in single-line expressions in line 6
and line 17
. This saves up unnecessary loops and creates a cleaner codebase.
2
14 reads
the *
prefix operator was added to the multiple assignment syntax, allowing us to unpack the remaining items in an iterable.
The **
operator does something similar, but with keyword arguments. The **
operator allows us to take a dictionary of key-value pairs and unpack it into keyword arguments in a function call.
3
18 reads
IDEAS CURATED BY
Learn more about computerscience with this collection
Understanding machine learning models
Improving data analysis and decision-making
How Google uses logic in machine learning
Related collections
Similar ideas
4 ideas
An introduction to Python Type Hints
peps.python.org
3 ideas
Introducing Sanic: The Python Asynchoronous Web Framework
mausamadh.medium.com
1 idea
Thinking Recursively in Python – Real Python
realpython.com
Read & Learn
20x Faster
without
deepstash
with
deepstash
with
deepstash
Personalized microlearning
—
100+ Learning Journeys
—
Access to 200,000+ ideas
—
Access to the mobile app
—
Unlimited idea saving
—
—
Unlimited history
—
—
Unlimited listening to ideas
—
—
Downloading & offline access
—
—
Supercharge your mind with one idea per day
Enter your email and spend 1 minute every day to learn something new.
I agree to receive email updates