In the world of JavaScript, comparing values is a fundamental operation, and doing it right can save you from unexpected surprises and bugs. When it comes to equality comparisons, ===
is your trusty guardian, and in this brief guide, we’ll uncover why it should be your go-to choice.
Tag: check
One way to improve the concision of your Python code is by using the ternary operator, a handy tool that allows you to condense an if-else statement into a single line of code.
Let us jump straight into it.
Imagine that you have the following comparison:
x = 5
y = 10
if x > y:
result = x
else:
result = y
You can then write it in a single line:
result = x if x > y else y
Continue reading
A leap year is a year that has 366 days instead of the usual 365. It occurs every four years and is the year when an extra day, February 29th, is added to the calendar. This day, known as a leap day, helps to keep the calendar aligned with the Earth’s movements around the sun. Leap years occur every four years, with the next one occurring in 2024.
We can check whether a year is a leap year in Python in a few ways.
Continue readingAnagrams are strings that have the same letters but in a different order for example abc
, bca
, cab
, acb
, bac
are all anagrams, since they all contain the same letters.
We can check whether two strings are anagrams in Python in different ways. One way to do that would be to use Counter
from the collections
module.
From the documentation:
A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.
In plain English, with Counter
, we can get a dictionary that represents the frequency of elements in a list. Let us see this in an example:
from collections import Counter
print(Counter("Hello")) # Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1})
Now we can use Counter
to quickly check whether two strings are anagrams or not:
from collections import Counter
def check_if_anagram(first_string, second_string):
first_string = first_string.lower()
second_string = second_string.lower()
return Counter(first_string) == Counter(second_string)
print(check_if_anagram('testinG', 'Testing')) # True
print(check_if_anagram('Here', 'Rehe')) # True
print(check_if_anagram('Know', 'Now')) # False
We can also check whether 2 strings are anagrams using sorted():
def check_if_anagram(first_word, second_word):
first_word = first_word.lower()
second_word = second_word.lower()
return sorted(first_word) == sorted(second_word)
print(check_if_anagram("testinG", "Testing")) # True
print(check_if_anagram("Here", "Rehe")) # True
print(check_if_anagram("Know", "Now")) # False
That’s basically it.
I hope you find this useful.
One 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 readingThere 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 readingIn the last article, I shared a simple way that you can use to find the first element in a list without using the index 0.
In case you haven’t read it and you do not want to read it now, here is a short recap that is also going to be helpful in this article.
Continue reading