Posts by Editor
Reorder items in a list
- 13 December 2024
Lists in Python are ordered collections of items. You can reorder the items in a list using the sort method or by manually moving items around in the list. So let’s start with an example of how you can print an unsorted list as items at index 2 and 3 are not in order.
Speeding up functions in Python
- 20 January 2024
Python’s versatility and ease of use make it a popular choice for a wide range of programming tasks. However, performance can sometimes be a concern, especially when working with large data sets or computationally intensive tasks. Repetitive calculations can be a particular problem, but Python provides a number of tools to help speed up these functions. One way is to use the functools.cache
decorator to cache the results of a function which was introduced in Python 3.9. This can be used to avoid repeating calculations, and can significantly speed up the execution of a function.
Format datetime easily
- 21 December 2023
Formatting datetime is a common task in Python and could be a daunting task for those new to Python. Especially when you need to format datetime in a specific way and looking at the documentation can be daunting for some. The exampple below shows how to format datetime in a specific way with the time.strftime()
method what can be complicated with due to all the extra lines.
Use dotenv to load environment variables
- 14 November 2023
Developing and deploying applications often requires the use of configuration variables such as passwords, API keys, and secret keys. These variables should not be hardcoded in the source code, as they can be accessed by unauthorized users. The dotenv library can be used to load environment variables from a file when it is started. This allows you to store sensitive information in a file that is not uploaded to a repository and to override the environment variables for development, testing, and debugging. This allows an application to be 12-factor compliant and to be deployed to different environments without changing the source code.
How to use post-init with dataclasses
- 12 October 2023
By default a class in Python has an __init__
method that is called when the class is instantiated. This method is used to initialize the class attributes. The example below shows this in action and how a variable is set by combining two other variables.
Using magic method __contains__
- 07 September 2023
Python is a high level language and allows you to do a lot of things with ease. One of the things that Python allows you to do is to check if an item is present in a list or tuple for example. This is done using the in
operator as shown below.
The NAND, NOR, and XOR operators in Python
- 07 April 2022
The NAND, NOR, and XOR operators are logical operators that are not built into Python, but can be implemented using the built-in not, and, and or operators. The NAND operator returns True if and only if both of its operands are False, and the NOR operator returns True if and only if both of its operands are False. The XOR operator returns True if and only if exactly one of its operands is True.
Convert numbers between numerical systems
- 11 December 2021
Learning to convert numbers between numerical systems is a fundamental skill in every programming language. Python provides built-in functions to convert numbers between binary, octal, decimal, and hexadecimal systems. While it seems like a simple task, it is essential to understand how to convert numbers between different numerical systems and how Python handles them.