Exception handling is a vital aspect of writing reliable and robust code. Neglecting to handle exceptions appropriately can result in unhandled errors, leading to program crashes and undesirable user experiences.
By anticipating potential errors and implementing proper exception-handling techniques, you can ensure graceful error recovery and maintain the stability of your code.
In this article, we’ll explore the significance of handling exceptions effectively and provide code examples that demonstrate the importance of incorporating try-except blocks.
If you are working with many numbers in Python, you probably need to do some rounding to cut off some digits that you may not need at all.
For example, let us say that you are calculating an average salary in a company and the average number that you have found is around 54,334.218
This may not be a number that can look good in a final report you want to submit to someone. You should instead round it.
Let us do it in Python:
As you can see, the number 2 after the comma represents the number of digits we want to keep. The thing is, we can also use negative numbers to specify the number of digits we want to keep.
If we use -1, we are doing the rounding to the nearest ten:
When we need to do the rounding to the nearest hundert, we use -2:
If we want to do the rounding to the nearest thousand, we use -3:
This may seem something quite trivial, but I also got to learn it just recently.
You have probably come across situations in which you needed to convert a string into a date in Python. There are a few ways to do that, but you may have not had the chance to use the following method.
It’s not that it is so special that you cannot learn, or that it is so difficult what’s going on. It’s basically a rare conversion that you have not had the chance to stumble upon before.
Finding the number of days is usually quite popular and well documentated. There can also be cases when we need to find a difference in terms of weeks or even months.
If you want to find the difference in months between 2 dates, then here is a quick Python script to help you do that.
For example, between October 2018 and November 2021, there isn’t just a month difference. There is also a difference in years as well.
Let us assume that we don’t have dates prepared yet, so we initialize 2 variables with 2 dates.
After that, we find the difference in terms of number of months in between.
We should keep in mind that we may have dates that are in different years, and it’s not enough to think that we can just subtract two dates. Because of that, we need to also find the difference in years.
Here is the entire script:
You may get the impression that we may have a negative number in the end, since we are subtracting the number representing a month from another month.
To illustrate, let us assume that we have the following:
Now, since we are subtracting the number of months from the first date and 3 < 12, we will indeed get -9. However, we also have in the end a difference in years that is equal 1, since 2021 – 2020 = 1.
Hence, we will add 12 to -9, and in the end we get the result equal to 3:
If you use Mac and tend to copy and paste things from one place to another, chances are, you may not be interested in preserving the original formatting of the text. Instead, you simply want the copied text to be pasted with the formatting of the destination of where it is posted.
I am an experienced and passionate Senior Software Engineer with a demonstrated history of working in the full life cycle of software development with enormous curiosity for data science, machine learning, algorithms, data structures, and solving challenging problems. I am an open-source enthusiast at https://github.com/fatosmorina and also a writer.
I am open for new opportunities.