There can be many cases when you want to get elements from a list of named variables and not have to always use indexes.
For example, let us assume that we have a list that has five elements:
my_list = [1, 2, 3, 4]Continue reading
Software engineering and personal development
There can be many cases when you want to get elements from a list of named variables and not have to always use indexes.
For example, let us assume that we have a list that has five elements:
my_list = [1, 2, 3, 4]Continue reading
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.
Let us assume you are given a list and you need to remove all the duplicate elements from the list. One common way to remove duplicates from a list is to use the set() function.
We can simply convert the list to a set and then convert the set back to a list so that we are still working with a list.
Continue readingIf you are implementing a new class, but do not have time now, or simply want to postpone it for later, then you can use the pass statement.
I have seen it being used all over the place, and I think it is a good idea to use it when you are writing a class that does not have any bodies of any methods, or when you are implementing a class that is not yet complete.
You can use it to do test-driven development when you initially write a failing test that calls a method that is not implemented yet, which you can then implement and fix the failing test.
Another scenario when I saw it being used was in coding assignments that are part of homework.
Continue readingSometimes, you may need to check whether a list has any duplicate elements. This can be a common task when you are given a list of numbers or a list of strings. This can also be something that you may need to do both at work, or it can also be part of a coding challenge that you are implementing when being asked at a programming interview.
Continue readingWhen you are given a dictionary, you can do a lot of things with both its keys and its values. From time to time, you may need to do some sort of comparison based on the values of the dictionary. This can include finding the largest value, the smallest value, the sum of all the values, etc.
Continue readingMany programming interview questions have something to do with working with indexes. You may also be dealing with tasks at work that can have something to do with indexes.
One such task can be doing different splits and formattings of text files based on an index of a specific character.
Let us assume that you are asked to remove part of a string based on a certain index in a string.
Continue reading© 2024 Fatos Morina
Theme by Anders Noren — Up ↑