Software engineering and personal development

Month: March 2023

How to Quickly Remove Falsy Values from Lists in Python

One of the most attractive features of Python is its readability and conciseness, allowing developers to write elegant and efficient code. In this article, we will explore a Python method called “Compact” that helps remove falsy values from a list using filter().

The Compact method is a simple yet powerful Python function that removes falsy values (False, None, 0, and “”) from a list. It does this by utilizing the filter() function, which returns an iterator that includes only the elements of the list that satisfy a specific condition.

Continue reading

How Non-Empty Lists, Tuples, and Dictionaries are Evaluated to True

When working with conditional statements in Python, it’s important to understand how different data types are evaluated to True or False. In addition to the previously discussed false values such as None, "False", and 0, there are also some specific rules for evaluating lists, tuples, and dictionaries.

In Python, any non-empty list, tuple, or dictionary is evaluated to True, while an empty one is evaluated to False. This means that if you have a list or dictionary that contains at least one element, it will evaluate to True in a conditional statement.

Continue reading

Understanding False Values in Python: None, “False”, and 0

When working with conditional statements in Python, it’s important to understand what values evaluate to True and what values evaluate to False. While some values are obviously True, such as any non-zero number or a non-empty string, there are a few other values that can sometimes trip up programmers. In particular, the values None, "False", and the number 0 are all examples of values that evaluate to False in Python.

Continue reading

How to Calculate the Time Spent in Python

In programming, it’s important to optimize code for performance. One way to do this is by measuring how long it takes for code to execute. In this blog post, we will explore a Python snippet that can be used to calculate the time it takes to execute a particular piece of code.

Python’s time module provides a simple way to measure the execution time of a program. The time.time() function returns the current time in seconds since the epoch (January 1, 1970, 00:00:00 UTC) as a floating-point number. We can use this function to measure the time before and after a particular piece of code and calculate the difference to get the total time it took to execute the code.

Continue reading

© 2024 Fatos Morina

Theme by Anders NorenUp ↑