Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Calculate Age from Birth Year in Python

Estimate an age with Python date.today rather than a stale year constant. For beginners, the main lesson is why a birth year alone cannot establish exact age.

BeginnerBasic Python ProgramsExample 19 of 25
calculate-age-from-birth-year.py
1# Program to calculate age from birth year
2
3from datetime import date
4
5birth_year = int(input("Enter your birth year: "))
6current_year = date.today().year
7
8if birth_year > current_year:
9 print("Birth year cannot be in the future.")
10else:
11 age = current_year - birth_year
12 print("Your age is:", age)

Sample Output

Your age is: 25

This sample age of 25 assumes a 2025 run; a 2026 run gives a year difference of 26.

In this one

  • Import date from datetime
  • Read the current calendar year
  • Recognize a birth-year estimate

How it works

  1. 1

    Convert birth_year to an integer.

  2. 2

    Read date.today().year and reject a later birth year.

  3. 3

    Subtract the birth year from current_year and print the year difference.

Edge cases

Future birth year

Enter a year after the current one.

The program prints an error rather than returning a negative age.

Birthday still ahead

Supply only the year for a December birthday.

The estimate can be one higher than completed age because no month or day is known.

What's going on

Subtracting a birth year from the current year gives the age someone turns during this year. It does not necessarily give their age today. Born in 2000 during 2026 means 26 by year difference, but a December birthday may still be ahead.

from datetime import date imports the date class. date.today().year reads the current local calendar year rather than leaving a hardcoded year to rot in the file. The future-year check prevents a negative result.

The sample age depends on the year of execution. A displayed 25 for birth year 2000 corresponds to a run in 2025. To calculate completed age accurately, ask for month and day too and subtract one if this year's birthday has not happened yet. This program deliberately stops at the simpler estimate.

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.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Is this an exact age in completed years?

Pick an answer to check it.

Question 2

Why call date.today().year?

Pick an answer to check it.

Questions people ask

Is this an exact age in completed years?

No. It ignores the birthday month and day, so it can be one year high.

Why call date.today().year?

It reads the current local year so the calculation does not depend on a stale year constant.

Why can my output differ from the sample?

The current year changes, so the same birth year produces a different estimate in later years.