Calculate Age from Birth Year in Python
this year minus birth year. Wrong if your birthday hasn't happened yet. Close enough for a first program.
BeginnerBasic Python ProgramsExample 19 of 25
calculate-age-from-birth-year.py
1# Program to calculate age from birth year23from datetime import date45birth_year = int(input("Enter your birth year: "))6current_year = date.today().year78if birth_year > current_year:9 print("Birth year cannot be in the future.")10else:11 age = current_year - birth_year12 print("Your age is:", age)
Output
Enter your birth year: 2000 Your age is: 25
In this one
- date.today().year, not a hardcoded 2026
- this ignores the actual birthday
What's going on
date.today().year - birth_year. In September 2026, born 2000 → 26. If your birthday is in December you are still 25. This program does not care. Say that out loud if you use it for anything real.
from datetime import date so the year is not a hardcoded 2026 that rots.
Future birth years get a message instead of a negative age.
After this program
If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.