Fatos Morina

Software engineering and personal development

Page 5 of 55

How to Quickly Remove All Elements from a Data Structure in Python

Working with lists, sets, and dictionaries is a common task in Python programming. Often, we may need to remove all the elements from a list, set, or dictionary for various reasons. Python provides a simple and efficient way to clear all the elements from these data structures using the clear() method. In this article, we will discuss how to use the clear() method to remove all elements from a list, set, or dictionary.

Continue reading

How to Quickly Capitalize First Letters in Python

One of the essential features of any programming language is the ability to manipulate strings. In this article, we’ll explore a simple yet powerful Python string method called title(). We will see how to use this method to capitalize the first letter of each word in a given string.

The title() method is a built-in Python string method that capitalizes the first letter of each word in a given string. It returns a new string where the first letter of each word is capitalized, and all other letters are in lowercase. Here is the syntax of the title() method:

string.title()
Continue reading

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

How to Get Default Values for Missing Keys in Python

Working with dictionaries in Python is a common task for many programmers, as it allows them to store and manipulate data in a key-value format. However, sometimes we may need to access a key in a dictionary that does not exist, and we want to provide a default value in such cases. The get() method in Python provides a simple and elegant solution to this problem.

The get() method is used to retrieve the value of a specified key in a dictionary. It takes two parameters: the key to look for and a default value to return if the key is not found in the dictionary. If the key is present in the dictionary, get() returns the corresponding value. Otherwise, it returns the default value specified.

Let’s look at the following code snippet:

dictionary = {'first_element': 1, 'second_element': 2}

print(dictionary.get('third_element', 3))  # 3

In this code, we have a dictionary with two key-value pairs. We then use the get() method to retrieve the value associated with the key 'third_element'. Since this key is not present in the dictionary, the method returns the default value of 3.

Continue reading

How to Perform Math Operations Inside Strings in Python

In Python, it’s possible to perform mathematical operations inside of string literals, using a feature called “f-strings”. F-strings, or “formatted string literals”, allow you to embed expressions inside string literals, which are evaluated at runtime. This allows you to mix variables, arithmetic expressions, and other computations into strings in a very readable and convenient way.

num_val = 42

print(f'{num_val % 2 = }') # num_val % 2 = 0

Here, the f-string '{num_val % 2 = }' is a string literal that contains an expression num_val % 2. The % operator is used to perform the modulo operation, which returns the remainder of the division of one number by another. In this case, num_val % 2 returns the remainder of the division of 42 by 2, which is 0.

Continue reading
« Older posts Newer posts »

© 2024 Fatos Morina

Theme by Anders NorenUp ↑