When you have a list of elements, there can be cases when you need to check whether there an element is part of a list or not.
You can do this using in
checker.
Software engineering and personal development
When you have a list of elements, there can be cases when you need to check whether there an element is part of a list or not.
You can do this using in
checker.
One of the common things that you can notice in many tasks is the fact that you need to sort elements in a list. In fact, this can also be part of an interview that you may go through while applying for a job. Maybe not only sorting elements, but sorting elements fulfilling certain conditions.
Continue readingOne way of introducing potential bugs is the lack of information about the way scopes work inside functions in Python.
When we declare global variables, their value may not change as we may expect.
Let us see this in an example to understand how a simple example can be misleading and confusing for a lot of people.
Continue readingSimilar to the case of using any()
, we can also use a method that allows us to check whether all conditions are met. This can also greatly reduce the complexity of the code since you do not need to use multiple and checks.
Let us see this with an example.
Let us assume that we have the following conditions where we are checking whether we have more than 50 points in each school course:
math_points = 51
biology_points = 78
physics_points = 56
history_points = 72
my_conditions = [math_points > 50, biology_points > 50,
physics_points > 50, history_points > 50]
Now passing all of them means that each condition should be met. To help us with that, we can simply use all()
, as can be seen in the following snippet:
In many cases, we may have multiple conditions that we want to check, and going through each one of them can be a clutter.
First and foremost, we should keep in mind that we write code for other humans to read it. These are our colleagues that work with us, or people who use our open source projects.
As such, checking whether at least one condition is met can be done using a very quick way by using the method any().
Continue readingOne of the most common ways to debug is by using print() functions in Python.
They are so commonly used all over the place that you may even end up seeing them in code that is included in production.
One thing that not a lot of people know is that you can include conditions in print() functions.
Continue readingA lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one expression.
Let us see a quick example of a lambda expression being used to square a number:
sqr = lambda x: x * xContinue reading
sqr(10) # 100
There can be cases when we want to check whether a value is equal to one of the multiple other values.
One way that someone may start using is using or
and adding multiple checks. For example, let us say that we want to check whether a number is 11, 55, or 77. Your immediate response may be to do the following:
m = 1
if m == 11 or m == 55 or m == 77:
print("m is equal to 11, 55, or 77")
There is fortunately another quick way to do that.
Continue readingThere are two methods that you can use to copy objects in Python, namely, copy() and deepcopy().
They are similar and can be confusing for a lot of people. There is however a subtle difference.
Continue readingYou may need to read text files and do some formatting of those strings.
With formatting, it means that you may also need to replace a certain portion of the string with another string.
Let us assume that we have a sentence and want to replace all the occurrences of the word i with I.
Continue reading© 2024 Fatos Morina
Theme by Anders Noren — Up ↑