There are lots of little tricks that can make life easier for a Python coder. Some of these will be known to you, but I’m sure there are at least a couple that you haven’t seen before.
Iterators make for very elegant for-loops in Python, and also make comprehension possible. Even though it requires some work to understand all the inner workings, they are actually very easy to use in practice!
To understand what a Python iterator is, you need to know two terms:
Iterator — An object that can be iterated, meaning we can keep asking it for a new element until there are no elements left. Elements are requested using a method called __next__
.
Iterable — An object that implements another special method, called __iter__. This function returns an iterator.
As stated above, a Python iterator object implements a function that needs to carry the exact name __next__
. This special function keeps returning elements until it runs out of elements to return, in which case an exception is raised of type StopIteration
. To get an iterator object, we need to first call the __iter__
method on an iterable object. …
One of Python’s biggest strengths is the enormous number of user-contributed packages available for free. Python packages can be offered anywhere but are most commonly published in the Python Package Index.
The Python Package Index, PyPI for short, contains more than 270K packages. Let’s explore some of the most-used packages and find out how to install them using a tool called pip.
Just to get a taste of what’s available, here are seven packages I personally like and use a lot.
In mathematics, there’s a concept called set-builder notation, also called set comprehension. Inspired by this principle, Python offers comprehensions, too. In fact, list comprehensions are one of the defining features of Python. They allow us to create concise, readable code that outperforms the uglier alternatives like for
loops or using map()
.
We’ll first look at the most well-known type: list comprehensions. Once we’ve got a good grasp of how they work, you’ll also learn about set comprehensions and dictionary comprehensions.
Originally published in Python Land’s Python Tutorial on list comprehensions.
A list comprehension is a language construct. It’s used to create a list based on an existing list. Sounds a little vague, I know, but after a few examples, that ‘ah-ha!’ …
Python has a long history, starting around 1991 with its first release in a newsgroup called alt.sources
. Since then, we all know how omnipresent the language has become. Last year, Python ranked second in Redmonk’s list of the most popular programming languages. And I can tell you… this year won’t be different. Here’s why Python will stay among the top languages in 2021.
Originally published on Python Land. Make sure to check out our Python tutorial.
Python has a very vibrant community, and it’s very well maintained. …
I’ve recently switched from Grav CMS to WordPress on one of my sites, Python Land. I jotted down some lessons learned and some advanced tips while setting up this WordPress site that I’d like to share with anyone interested.
I’m not new to WordPress, and I’m certainly not new to running websites. I’ve been doing it for 25 years. My first sites used tables to structure the pages, CSS wasn’t invented yet, and SEO didn’t exist either. Yeah… I guess I’m that old (and wise, hopefully).
Anyways... without further ado, here we go!
Picking the right theme is hard; don’t underestimate it. There are many good-looking themes, but it’s hard to judge a book by its cover. There are many crappy themes that may look nice on the surface but are horribly coded and inflexible. They are built to sell, not to keep working flawlessly in the long run. …
When you finish a new version of your Python application, how do you build and deploy it? Do you change the version number, upload the new files to production, and be done with it?
There are better ways! Continuous Integration/Continuous Delivery (CI/CD) is the pinnacle of good software engineering practices. It’s the point where all other good practices come together.
CI/CD bridges the gap between development and operations because it automates and enforces important steps in building and deploying software. It ensures quality and takes human errors out of the loop.
This article will take an existing application and create a CI/CD pipeline for it. You’ll see how you can set up a professional CI/CD pipeline in under 15 minutes! …
Some things you don’t learn from books and schools. These lessons come right from the work floor; I learned them the hard way!
I love this one because it makes the ones hearing it (your manager) think for themselves:
Do you know how to force keyword arguments, create a function decorator, create anonymous functions, or unpack an array or dictionary into a function's arguments? Here are four advanced tricks regarding Python functions.
For more cool Python tricks, visit the author's website: Python.land
Keyword arguments have a number of advantages:
That’s nice, but you probably already knew these things. What you might not know is that you can also force keyword arguments. The details are described in PEP 3202, but it comes down to using an asterisk before the arguments you want to force as keyword arguments. Or, before everything, forcing all arguments to be keyword…
The dictionary is one of Python’s most powerful data types. In other programming languages and computer science in general, dictionaries are also known as associative arrays. They allow you to associate keys to values.
Let’s look at how we can create and use a dictionary in the Python REPL:
>>> phone_nrs = { 'Jack': '070-02222748', 'Pete': '010-2488634' }
>>> an_empty_dict = { }
>>> phone_nrs['Jack']
'070-02222748'
The first dictionary associates keys (names like Jack and Pete) with values (their phone numbers). The second dictionary is an empty one.
Now that you’ve seen how to initialize a dictionary, let’s see how we can add and remove entries to an already existing…