Software engineering and personal development

Tag: default

How to Set Default Values for Missing Keys in Python Dictionaries

Python is renowned for its elegant and versatile syntax that often leads to efficient and concise code. In this quick blog post, we’re going to explore a handy Python trick that utilizes dictionaries to effortlessly assign default values for missing keys. This simple technique can save time and streamline your code, making it an essential tool for any Python developer.

Continue reading

How to Handle Default Arguments Safely in Python Functions


Default arguments in Python functions provide convenient ways to assign values to parameters when no explicit arguments are provided. However, misusing mutable default arguments, such as lists or dictionaries, can introduce unexpected behavior and lead to bugs in your code. In this article, we’ll explore the potential pitfalls of misusing mutable default arguments and discuss best practices for handling defaults in a safer manner.

Continue reading

How to Quickly Avoid Errors when Getting Nonexisting Dictionary Elements

Dictionaries also known as maps are data structures that are used a lot in different scenarios. The process of getting an element from a dictionary can be done using an element that is not part of the dictionary which results in an error.

For example, let us take this scenario where we have a dictionary that has an element with the key name and another one with the element surname. If we want to access it using another element, such as age, we are going to see an error like the following:

 my_dictonary = {"name": "Name", "surname": "Surname"}
 print(my_dictonary["age"])  
Continue reading

How to Create a List with a Specific Length in Python

person writing bucket list on book
Photo by Glenn Carstens-Peters on Unsplash

When you create a new array in Java, you can set up the length of it. If you are programming mostly in Python these days, then you may not be aware that you can also do that in Python.

Let’s say that we have a fixed number of students and we want to save their heights in a list.

We can of course just declare an empty list and save their heights by appending new elements to the empty list. 

Continue reading

© 2024 Fatos Morina

Theme by Anders NorenUp ↑