Software engineering and personal development

Tag: python (Page 4 of 10)

3 Ways How You Can Quickly Check Whether a Year is a Leap Year in Python

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 reading

How to Get the Number of Days in a Month in Python

If you need to get the number of days in a month in Python, you can do that quite quickly.

We are going to do that using the calendar module.

First, import the calendar module and then call the method monthrange() which returns the first_day and also the number_of_days in a month.

Let us see this in action:

 import calendar
 ​
 # Get current month
 _, number_of_days = calendar.monthrange(2023, 2)
 ​
 print(number_of_days)  # 28
Continue reading

How to Quickly Check if 2 Strings Are Anagrams using Counter

Anagrams 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.

How to Quickly Build Your Own Iterators in Python

You have probably had the chance to iterate through a list of elements in one way or another, or through elements of a set, or a dictionary. We can go through a list, a set, or a dictionary and access their elements because they are iterable objects.

An iterator is an object that contains a countable number of objects. This means that you can iterate through elements that an iterator contains.

Continue reading

How to Quickly Avoid Errors when Getting Nonexisting Dictionary Elements

Dictionaries also known as maps are data structures that are used a lot in different scenarios. The process of getting an element from a dictionary can be done using an element that is not part of the dictionary which results in an error.

For example, let us take this scenario where we have a dictionary that has an element with the key name and another one with the element surname. If we want to access it using another element, such as age, we are going to see an error like the following:

 my_dictonary = {"name": "Name", "surname": "Surname"}
 print(my_dictonary["age"])  
Continue reading
« Older posts Newer posts »

© 2024 Fatos Morina

Theme by Anders NorenUp ↑