Software engineering and personal development

Tag: number

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 Whether a Variable is a Number in Python

photo of man walking near LED signage
Photo by Ryoji Iwata on Unsplash

Since you don’t have a predefined type for a variable in Python, you may notice a lot of bugs that can arise. As such, it is very important that you pay a lot of attention in cases when you have to make sure that you are using the correct type of variable.

This can also be the case when you expect a variable to be a number, but you get a string or any other type instead.

Continue reading

© 2024 Fatos Morina

Theme by Anders NorenUp ↑