Python 3 latest code crumbs

Writing YAML to a file with Python

Writing YAML to a file with Python

Feb 26, 2022 by erik

Although most will only read YAML as a configuration file, it can be very handy to write YAML as well. In the following example, we'll: * Create a list with names as we did before * Save the names to...

yaml files
Python's Finally block In Try Except

Python's Finally block In Try Except

Feb 20, 2022 by erik

The finally block is executed regardless of whether an exception occurs or not. Finally blocks are useful, for example, when you want to close a file or a network connection regardless of what happens. After all, you want to clean...

files exceptions
Swap two variables in Python without using a third

Swap two variables in Python without using a third

Feb 15, 2022 by erik

With this neat little trick, you can swap two Python variables without using a third variable. Source: python.land blog...

variables trick
Python class example

Python class example

Feb 12, 2022 by erik

This is an example of a Python class. This class represents a car. It could be part of a game, for example, in which you have multiple cars running around. This example is part of the [Python objects and classes...

classes
Python Hello World

Python Hello World

Feb 11, 2022 by erik

This is a typical Hello World example in Python. It's part of the free and complete Python Tutorial for beginners at python.land....

hello-world
Runnable Python module

Runnable Python module

Feb 4, 2022 by erik

When we import a module, its name (stored in the variable __name__) is equal to the module name. When a script is executed, its name is always set to the__main__ string. Hence, if we check for this name with `if...

import modules