Python Program to Calculate Age in the Year

In our topic, we are using the Python program and language to find your age. The Python programming language can be used to determine the age of the person. It is a common question, how to prepare the age calculator in Python programming language and what is its code. It is quite simple to find out how old am I, especially in Python you can calculate age by various approaches. You may wonder how to calculate your age in the simplest manner. 

Python Program to Calculate Age in the Year

Python Age in Months Calculator:

For finding the age in Python, we are using the DateTime module as Python language offers a built-in module for the DateTime.When you are preparing the age-in-month calculator, you just have to recall the built-in function in your program. This would make your code simple and you haven’t been required to write the whole code of finding and comparing the data and time of a person. 

Check Also: How to Block Programs With Your Firewall [Full Guide]

The  DateTime Module Age Calculator Code:

Here we are presenting the simple code of the DateTime module of Python. 

The Python3 program finds the age in years. from datetime import date  def calculateAge(dob):    today = date.today()    age = today.year – dob.year -((today.month, today.day) <         (dob.month, dob.day))      return age   Print (calculateAge(date(2017, 12, 06)), ” years old”)
 Output age of the kid by the above code: 5  years old

When you implement the DateTime module the Python age calculator checks the current date and time and subtracts it from the date of birth of a person. This would extract the exact age of a person to the current date.

Explanation:

  • The code def calculateAge (dob) is used to extract the date of birth of the kid.
  • Then today = date.today () is used to extract the current date of the year and the month. 

The code is:     

           age = today.year – dob.year – ((today.month, today.day) <

              (dob.month, dob.day))

This code is used to subtract the date of the birth of the kids from the current month and the date of the year.

  • Then we used the code to print our result:

return age

          print(calculateAge(date(2017, 12, 06)), ” years old”)

  • Finally, the result extracted from the date of birth which is the (2017, 12, 06), and the age of the kid is now retired is “5” years.
  • You need to see the difference between the current dates 2022,12,06 from the date of birth is 2017, 12, 06 is exactly “5” years. This is our answer displayed.

Conclusion:

Python programming language can be used to make the age calculator as there are many built functions like the Date Time module. You can extract the age in Python programming language by the division method. It can be great to measure and compare the ages of various people.

Python provides the most interactive platform for measuring and calculating various measurements. The built-in functions and the scope of the language are essential to make our task easy. If you have knowledge of various built-in functions then you can recall them to measure various calculations.

Leave a Comment