 # Python program to calculate age in year

## Python program to calculate age in year

Given birth date in y/m/d format, write a Python program to find the present age in years.

Examples:

```Input : 1997/2/3
Output : 21 years (for present year i.e 2018)

Input : 2010/12/25
Output : 8 years (for present year i.e 2018)```

Approach #2: Using datetime module
Python provides datetime module to deal with all datetime related issues in python. Using datetime we can find the age by subtracting birth year from current year. Along with this, we need to focus on the birth month and birthday. For this, we check if current month and date are less than birth month and date. If yes subtract 1 from age, otherwise 0.

 `# Python3 code to  calculate age in years` `from` `datetime ``import` `date` `def` `calculateAge(birthDate):` `    ``today ``=` `date.today()` `    ``age ``=` `today.year ``-` `birthDate.year ``-` `         ``((today.month, today.day) <` `         ``(birthDate.month, birthDate.day))` `    ``return` `age` `    ` `# Driver code` `print``(calculateAge(date(``1997``, ``2``, ``3``)), ``"years"``)`

Output:

`21 years`

Approach #3 : Efficient datetime approach
The above approaches do not deal with a special case i.e. when birth date is February 29 and the current year is not a leap year. This case has to be raised as an exception because the calculation of birthdate may be inaccurate. This method includes try and catch for this exception.

 `# Python3 code to  calculate age in years` `from` `datetime ``import` `date` `def` `calculateAge(born):` `    ``today ``=` `date.today()` `    ``try``:` `        ``birthday ``=` `born.replace(year ``=` `today.year)` `    ``# raised when birth date is February 29` `    ``# and the current year is not a leap year` `    ``except` `ValueError:` `        ``birthday ``=` `born.replace(year ``=` `today.year,` `                  ``month ``=` `born.month ``+` `1``, day ``=` `1``)` `    ``if` `birthday > today:` `        ``return` `today.year ``-` `born.year ``-` `1` `    ``else``:` `        ``return` `today.year ``-` `born.year` `        ` `# Driver code` `print``(calculateAge(date(``1997``, ``2``, ``3``)), ``"years"``)`

Output:

`21 years`

Approach #4: Using division
In this approach, we calculate the number of date from the birth date till current date. Divide the number of date by the days in a year i.e 365.2425.

 `# Python3 code to  calculate age in years` `from` `datetime ``import` `date` `def` `calculateAge(birthDate):` `    ``days_in_year ``=` `365.2425` `    ``age ``=` `int``((date.today() ``-` `birthDate).days ``/` `days_in_year)` `    ``return` `age` `        ` `# Driver code` `print``(calculateAge(date(``1997``, ``2``, ``3``)), ``"years"``)`

Output:

`21 years`

Last Updated on November 11, 2021 by admin

## Python PIL – Image.crop() methodPython PIL – Image.crop() method

Python PIL | Image.crop() method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.crop() method is used to crop a rectangular portion of any image. Syntax: PIL.Image.crop(box = None) Parameters:  box – a 4-tuple defining

## Python – Pandas dataframe.interpolate()Python – Pandas dataframe.interpolate()

Pandas dataframe.interpolate()   Pandas dataframe.interpolate() function is basically used to fill NA values in the dataframe or series. But, this is a very powerful function to fill the missing values. It uses various interpolation technique to fill the missing values

## Python exit commands: quit(), exit(), sys.exit() and os._exit()Python exit commands: quit(), exit(), sys.exit() and os._exit()

Python exit commands: quit(), exit(), sys.exit() and os._exit()   The functions quit(), exit(), sys.exit() and os._exit() have almost same functionality as they raise the SystemExit exception by which the Python interpreter exits and no stack traceback is printed. We can

## Python – Pandas Series.str.replace() to replace text in a seriesPython – Pandas Series.str.replace() to replace text in a series

Pandas Series.str.replace() to replace text in a series   Pandas Series.str.replace() method works like Python .replace() method only, but it works on Series too. Before calling .replace() on a Pandas series, .str has to be prefixed in order to differentiate

## Check if a value exists in a DataFrame using in & not in operator in Python-PandasCheck if a value exists in a DataFrame using in & not in operator in Python-Pandas

In this article, Let’s discuss how to check if a given value exists in the dataframe or not. Method 1 : Use in operator to check if an element exists in dataframe. # import pandas library import pandas as pd

## Python – os.path.exists() methodPython – os.path.exists() method

Python | os.path.exists() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in python

## Python – Which is faster to initialize lists?Python – Which is faster to initialize lists?

Python | Which is faster to initialize lists? Python is a very flexible language where a single task can be performed in a number of ways, for example initializing lists can be performed in many ways. However there are subtle

## How to install Jupyter Notebook on Windows?How to install Jupyter Notebook on Windows?

How to install Jupyter Notebook on Windows? Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. Uses include data cleaning and transformation, numerical simulation, statistical