Python 3 latest code crumbs

NumPy float types: a demonstration of precision

NumPy float types: a demonstration of precision

Oct 11, 2022 by erik

The different NumPy float types allow us to store floats in different precision, dependent on the number of bits we allow the float to use. The larger the number of allowed bits, the more precision our array’s elements will have....

Your for-loop might be missing an else block

Your for-loop might be missing an else block

Aug 30, 2022 by erik

There's a little-known construct in Python that can be quite useful: using else in combination with loops. That's right, for-loops and while-loops can be followed by an else block! Let's look at how you can use the else block in...

Create an iterable with a constructor

Create an iterable with a constructor

May 24, 2022 by erik

Add a constructor to the EvenNumbers class that accepts a parameter called max. This max comes instead of the hard-coded value of 8. So the iterator must return values until it reaches this max value, but it must include the...

Python pass statement

Python pass statement

May 10, 2022 by erik

Python has a special keyword called pass. The Python pass keyword tells Python to do nothing at all. In other words: just pass this line of code and continue. If you are used to programming in C-like languages, you probably...

Convert JSON to YAML using Python

Convert JSON to YAML using Python

May 6, 2022 by erik

If you need to convert JSON to YAML, you can simply parse the JSON with the built-in JSON library. In the next step, you can use the PyYAML module to convert the object to YAML. In this example, we open...

Quickly print your Python variables with this f-string trick

Quickly print your Python variables with this f-string trick

Apr 20, 2022 by erik

There's an easy way to print the contents of variables by using f-strings. Using this trick, Python prints both the variable name and its value. This is useful for debugging purposes!...