Using for loops when iterating through 2 lists that have different lengths can be error-prone, especially when the lengths of these lists can change with time.
Let us see an example to see when that happens:
Continue readingSoftware engineering and personal development
Using for loops when iterating through 2 lists that have different lengths can be error-prone, especially when the lengths of these lists can change with time.
Let us see an example to see when that happens:
Continue readingYou have probably seen cases when you can do splits of strings into multiple parts based on a pattern that is observed in the string. For example, let us say that we want to save all the words that are part of a sentence.
To do that, we usually try to do split the sentence into words based on the spaces that we find, such as:
Continue readingIf you need to find the longest string in a list in Python, you may be tempted to do that in the form of iterating through every element, finding their length, and checking whether this current length is larger than a temporary value that we have chosen in the beginning.
Continue readingIf 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.
I hope you find this useful.
© 2024 Fatos Morina
Theme by Anders Noren — Up ↑