6 ways to increase performance, from using better algorithms to using C to multiprocessing

1. Optimizing your code and algorithms

Always take a good look at your code and algorithms first. You can resolve most speed issues by implementing a better algorithm or adding caching. Entire books are written about this subject, but some general guidelines to follow are:

  • Measure, don’t guess. Measure which parts of your code take the most time to run. Focus on those parts first.
  • Implement caching. This can be a big optimization if you perform many repeated lookups from disk, the network, and databases.
  • Reuse objects instead of creating a new one on each iteration. Python has to clean up every object you created to…


From slotted classes to replacing lists with tuples

Here are 10 advanced Python tricks to up your game and write faster, cleaner code. You may know some of these, but I’m sure there is something to learn here for everyone.

1. Using slotted classes

In a slotted class we explicitly define the fields that our class is allowed to have using the magic field name __slots__. This has some advantages:

  • Objects created from the class will take up slightly less memory
  • It’s faster to access class attributes
  • You can’t randomly add new attributes to objects of a slotted class

Here’s an example of how to define a slotted class:

To me…


Improve your Python knowledge and skills

Here are 30 Python best practices, tips, and tricks. I’m sure they’ll help you procrastinate your actual work, and still learn something useful in the process.

1. Use Python 3

In case you missed it: Python 2 is officially not supported as of January 1, 2020. This guide has a bunch of examples that only work in Python 3. If you’re still on Python 2.7, upgrade now.

If you’re on MacOS, you can use Homebrew to painlessly upgrade Python.

2. Check for a minimum required Python version

You can check for the Python version in your code, to make sure your users are not running your script with an incompatible version. …


From forcing keyword arguments to anonymous functions

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.

1. Forced Keyword Arguments

Keyword arguments have several advantages:

  • You’re not forced into a particular order in which you supply your arguments. The name matters — not the position.
  • Keyword arguments provide clarity. Without looking up the function itself, you can often guess what the argument is used for by looking at the names.

That’s nice, but you probably already knew these things. What you might not know is that you…


Using geological data to explain the basics of OOP

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects” (Wikipedia). A bunch of objects that are arranged into one group can be called a class. OOP can make the developer focus more on manipulating the object rather than manipulating the logic.

For example, in a showroom, there are various types of vehicles, as follows:

  • One jeep has 4 doors, 4 wheels, and green color
  • One motorcycle has no door, 2 wheels, and white color
  • One minibus has 6 doors, 4 wheels, and red color

If we apply OOP concepts in this example, we can say that…


With virtual environments, you can isolate your software and avoid version conflicts

Python virtual environments allow you to install Python packages in an isolated location for a particular application instead of installing them globally.

Let’s explore what the advantages are and how you can quickly get started.

Why You Need Virtual Environments

Preventing version conflicts

If you install third-party packages system-wide, you could argue that you’re very efficient. After all, you only need to install it once, saving you precious time and disk space.

There’s a problem with this approach that may start to unfold weeks or months later, however.

Suppose your project, Project A, is written against a specific version of its dependencies. …


There are better ways to manage your packages and virtual environments than using pip and virtualenv

Many developers still use the combination of virtualenv and pip, but most of them probably haven’t heard about pipenv yet. This article introduces pipenv, a better way to manage your Python virtual environments.

Why pipenv?

Using pipenv has several advantages compared to using pip and virtualenv. These are the main ones:

  • You no longer need to use pip and virtualenv separately. Instead, you have one tool that does it all — and more!
  • pipenv separates your top-level dependencies from the last tested combination (e.g., the output of pip freeze). This makes dependency management more user-friendly for you as a developer.
  • pipenv encourages…


Everything you ever need to know about Python dictionaries

Old books
Old books

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. This is because they allow you to associate keys with values.

Creating a Dictionary

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…


If you’re stuck at home, now is the time to polish your skills

1. Create a game

I bet you never considered creating a game with Python. But why not? There are awesome libraries that will help you develop games. So let’s help your kids, your partner, or your colleagues get through this crisis with a fun game!

Pygame

Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries. Here are a few tutorials you can try:


6 tips to keep your body and mind healthy during this pandemic

Extended periods of sitting increase your risk of chronic health problems, such as heart disease, diabetes, and cancer. It can also be bad for your mental health.

It’s essential to develop healthy habits as early as you can to counter these risks. I’ll share six crucial tips that helped me improve my health as a software developer.

1. Get Enough Quality Sleep

Sleep is underrated. It’s the foundation of your health. So why do we need it so badly? There are various reasons — these are a few found by scientists:

Python Land

Start learning Python — companion to https://python.land

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store