Age Calculator with Python
Python age calculator
This tutorial teaches you how to write and execute an age calculator in Python.
The code requires basic understanding of Python coding (including the built-in library datetime).
If you are not familiar with Python coding, please check these tutorials first.
Let’s start.
We use Python version 3.10+ and PyCharm (IDE).
Plan
This calculator contains 3 main parts:
- Calculation function
- User input request
- Result output
The calculator requires the user to input a birthday day, month, and year. As a result, it calculates how old a person is by taking the time of the calculation and the birthday date into account. The outcome shows in years.
Calculation function
First, we need to import the built-in datetime library.
from datetime import date
Then, we create a function which calculates the the difference between the time of calculation and the birthday date.
The function is very basic and straightforward. It takes into consideration today’s day, month, and year. And then, it calculates the difference in time, stores it in a variable (age) and returns the result.
def calculation(d, m, y): t = date.today() bd = date(y, m, d) age = t.year - bd.year - ((t.month, t.day) < (bd.month, bd.day)) return age
User input request
In the second part, the program asks for user input.
This section is also very easy. We only need three inputs (the day, month, and year when the person was born).
All requests are stored in variables, which are then passed into the function’s arguments/parameters.
d = input('Please enter the day: ') m = input('Please enter the month: ') y = input('Please enter the year: ')
Result output
The final part, outputs the outcome of the age calculation.
Here, we call the function and pass the user input into the function’s arguments. We also store the result inside a variable.
Finally, we print the result.
result = calculation(int(d), int(m), int(y)) print(f'\nThe age is {result}')
Example
Below is an example of the execution of the age calculator.

Full code of Age Calculator
Below is the full Python code of the age calculator.
from datetime import date def calculation(d, m, y): t = date.today() bd = date(y, m, d) age = t.year - bd.year - ((t.month, t.day) < (bd.month, bd.day)) return age d = input('Please enter the day: ') m = input('Please enter the month: ') y = input('Please enter the year: ') result = calculation(int(d), int(m), int(y)) print(f'\nThe age is {result}')