Validate IP Address

try:
            if len(IP.split('.')) == 4:
                print(IP)
                if all([bool(1) if(str(int(s)) == s and 0 <= int(s) <= 255) else bool(0) for s in IP.split('.')]):
                    return "IPv4"
            if len(IP.split(':')) == 8:
                if all([bool(1) if(len(s) <= 4 and int(s, 16) >= 0) else bool(0) for s in IP.split(':')]):
                    return "IPv6"
        except:
            return "Neither"
        return "Neither"

Here is what the above code is Doing:
1. First, we check if the IP address is IPv4 or IPv6.
2. If it is IPv4, we check if the IP address is valid or not.
3. If it is IPv6, we check if the IP address is valid or not.
4. If it is neither IPv4 nor IPv6, we return “Neither”.