Learn.Dedaandsons
Learn.Dedaandsons Python for Beginners
💻 int function Python

Calculate Age in Python Using int() | Learn variables, input() and the int() function in this beginner-friendly Python lesson.

Course progress Lesson int function Python
Lesson 5 / 5

Calculate Age in Python Using int() | Python for Beginners

Calculate Your Age in Python Using int()

Learn how to take a user's birth year as input, convert it from text to an integer using int(), and calculate their approximate age.

What you'll learn

  • How input() receives user input
  • Why Python treats input as a string
  • How to convert a string to an integer using int()
  • How to use a variable in a calculation
  • How to combine text and variables in print()

Step 1: Ask for the user's name

Python
name = input("What is your name? ")

The input() function displays a question and waits for the user to enter something.

Step 2: Display a greeting

Python
print("Hello " + name)

Here, the + operator joins the greeting with the value stored in the name variable.

💡 What does " " mean?

The space between quotation marks is a string containing one blank space.

Step 3: Ask for the birth year

Python
birthyear = input("Enter your birth year: ")

The user's birth year is stored in the birthyear variable.

💡 Important

Values received from input() are strings by default, even when the user enters a number.

Step 4: Convert the birth year to an integer

Python
birthyear = int(birthyear)

The int() function converts the value into an integer so that Python can perform mathematical calculations with it.

Step 5: Calculate the age

Python
current_year = 2026
age = current_year - birthyear

The program subtracts the user's birth year from the current year and stores the result in the age variable.

💡 Beginner note

This example calculates an approximate age based only on the birth year. The exact age also depends on whether the user's birthday has occurred this year.

Step 6: Display the result

Python
print("Your age is:")
print(age)

Complete Python program

Python
name = input("What is your name? ")

print("Hello " + name)

birthyear = input("Enter your birth year: ")

birthyear = int(birthyear)

current_year = 2026
age = current_year - birthyear

print("Your age is:")
print(age)

Example output

What is your name? Ali
Hello Ali
Enter your birth year: 2005
Your age is:
21

How does it work?

1. input()

Gets information from the user.

2. int()

Converts the birth year from a string into an integer.

3. Subtraction

The program subtracts the birth year from the current year.

4. print()

Displays the result to the user.

Calculate Age in Python Using int() | Python for Beginners

Calculate Age in Python Using int() | Python for Beginners | Calculate Age in Python Using int() | Learn variables | input() Function | int() function, Python

Related