Python program to display Astrological sign or Zodiac sign for a given data of birth.

In this article, we'll learn how to determine the astrological sign or zodiac sign for a given date of birth. We'll compare the user's birthdate with predefined date ranges for each zodiac sign using Python programming logic.

The 12 astrological sun signs, also known as zodiac signs, are based on specific periods throughout the year. Each sign corresponds to a particular range of dates ?

  • Aries (March 21 − April 19)
  • Taurus (April 20 − May 20)
  • Gemini (May 21 − June 20)
  • Cancer (June 21 − July 22)
  • Leo (July 23 − August 22)
  • Virgo (August 23 − September 22)
  • Libra (September 23 − October 22)
  • Scorpio (October 23 − November 21)
  • Sagittarius (November 22 − December 21)
  • Capricorn (December 22 − January 19)
  • Aquarius (January 20 − February 18)
  • Pisces (February 19 − March 20)

Using If-Else Conditions

The most straightforward approach is using conditional statements to check date ranges for each zodiac sign ?

def get_zodiac_sign(day, month):  
    month = month.lower()
    
    if month == 'december':  
        sign = 'Sagittarius' if (day < 22) else 'Capricorn'  
    elif month == 'january':  
        sign = 'Capricorn' if (day < 20) else 'Aquarius'  
    elif month == 'february':  
        sign = 'Aquarius' if (day < 19) else 'Pisces'  
    elif month == 'march':  
        sign = 'Pisces' if (day < 21) else 'Aries'  
    elif month == 'april':  
        sign = 'Aries' if (day < 20) else 'Taurus'  
    elif month == 'may':  
        sign = 'Taurus' if (day < 21) else 'Gemini'  
    elif month == 'june':  
        sign = 'Gemini' if (day < 21) else 'Cancer'  
    elif month == 'july':  
        sign = 'Cancer' if (day < 23) else 'Leo'  
    elif month == 'august':  
        sign = 'Leo' if (day < 23) else 'Virgo'  
    elif month == 'september':  
        sign = 'Virgo' if (day < 23) else 'Libra'  
    elif month == 'october':  
        sign = 'Libra' if (day < 23) else 'Scorpio'  
    elif month == 'november':  
        sign = 'Scorpio' if (day < 22) else 'Sagittarius'
    else:
        return "Invalid month"
        
    return sign

# Test the function
day = 7  
month = "January"  
result = get_zodiac_sign(day, month)
print(f"Your zodiac sign is: {result}")
Your zodiac sign is: Capricorn

Using Date Objects

A more robust approach using Python's datetime module to handle dates properly ?

from datetime import datetime

def get_zodiac_by_date(birth_date):
    # Convert to datetime if string is provided
    if isinstance(birth_date, str):
        birth_date = datetime.strptime(birth_date, "%Y-%m-%d")
    
    month = birth_date.month
    day = birth_date.day
    
    zodiac_dates = [
        (1, 20, "Capricorn"), (2, 19, "Aquarius"), (3, 21, "Pisces"),
        (4, 20, "Aries"), (5, 21, "Taurus"), (6, 21, "Gemini"),
        (7, 23, "Cancer"), (8, 23, "Leo"), (9, 23, "Virgo"),
        (10, 23, "Libra"), (11, 22, "Scorpio"), (12, 22, "Sagittarius")
    ]
    
    for end_month, end_day, sign in zodiac_dates:
        if (month < end_month) or (month == end_month and day <= end_day):
            return sign
    
    return "Capricorn"  # Default for late December

# Test with different dates
dates = ["1990-01-07", "1995-07-15", "2000-12-25"]

for date_str in dates:
    sign = get_zodiac_by_date(date_str)
    print(f"Birth date: {date_str} ? Zodiac sign: {sign}")
Birth date: 1990-01-07 ? Zodiac sign: Capricorn
Birth date: 1995-07-15 ? Zodiac sign: Cancer
Birth date: 2000-12-25 ? Zodiac sign: Capricorn

Interactive Zodiac Sign Calculator

Here's a complete program that takes user input and displays the zodiac sign ?

def zodiac_calculator():
    try:
        print("=== Zodiac Sign Calculator ===")
        
        # Get user input (using fixed values for demo)
        day = 15
        month = "August"
        year = 1990
        
        print(f"Birth Date: {month} {day}, {year}")
        
        # Define zodiac signs with their date ranges
        zodiac_signs = {
            'aries': "March 21 - April 19",
            'taurus': "April 20 - May 20", 
            'gemini': "May 21 - June 20",
            'cancer': "June 21 - July 22",
            'leo': "July 23 - August 22",
            'virgo': "August 23 - September 22",
            'libra': "September 23 - October 22",
            'scorpio': "October 23 - November 21",
            'sagittarius': "November 22 - December 21",
            'capricorn': "December 22 - January 19",
            'aquarius': "January 20 - February 18",
            'pisces': "February 19 - March 20"
        }
        
        month_lower = month.lower()
        
        if month_lower == 'august':
            sign = 'Leo' if day < 23 else 'Virgo'
        elif month_lower == 'july':
            sign = 'Cancer' if day < 23 else 'Leo'
        # Add other months as needed for complete implementation
        else:
            sign = "Unknown"
            
        print(f"\nYour Zodiac Sign: {sign}")
        print(f"Date Range: {zodiac_signs.get(sign.lower(), 'N/A')}")
        
    except ValueError:
        print("Please enter valid date values.")

zodiac_calculator()
=== Zodiac Sign Calculator ===
Birth Date: August 15, 1990

Your Zodiac Sign: Leo
Date Range: July 23 - August 22

Comparison of Methods

Method Pros Cons
If-Else Conditions Simple, readable Long code, repetitive
Date Objects Robust, handles edge cases Slightly complex
Dictionary Lookup Compact, maintainable Requires careful setup

Conclusion

Python makes it easy to determine zodiac signs by comparing birth dates with predefined ranges. Use conditional statements for simple cases, or datetime objects for more robust date handling.

Updated on: 2026-03-24T21:01:05+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements