I'm just looking for a cleaner way. Obviously I could store a variable for some of the int parts. Personally, I think the one-liner looks better and everyone's definition of what constitutes clean readable code differs.def gen_isbn(isbn):   for i, val in enumerate(isbn):       if len(isbn) == 9:           yield (i + 1) * int(val)       else:           yield int(val) * 3 if (i + 1) % 2 == 0 else int(val) * 1def isbn10(isbn):    mod = sum(gen_isbn(isbn[:-1])) % 11    return mod == int(isbn[-1]) if isbn[-1] != 'X' else mod == 10def isbn13(isbn):    mod = sum(gen_isbn(isbn[:-1])) % 10    return 10 - mod == int(isbn[-1]) if mod != 10 else int(isbn[-1]) == 0I'm really just wondering if there's a regex or cleaner one liners etc since this is already pretty short and works. It should be for both ISBN 10 and 13.Preferably, I would prefer pure Python. These are grabbed from web-scraping and hence I have enough external libraries floating around than throwing another one in just for ISBN validation.
Validating ISBNs
python;python 3.x;validation
Unnecessary check / bugmod can never be \$10\$ in isbn13 (it will always be smaller) because it is defined as x `mod` 10 and: $$ x\  `mod` \ n<  \ n$$So you can just write:return 10 - mod == int(isbn[-1])Remove repetitionI like the conditional expression, it is terse and clear, and it can reduce duplication by a lot, but in the below int(val) is repeated twice:int(val) * 3 if (i + 1) % 2 == 0 else int(val) * 1This is easy to fix, just make the ternary comprehend just the multiplier:int(val) * (3 if (i + 1) % 2 == 0 else 1)or, using a temporary var:multiplier = 3 if (i + 1) % 2 == 0 else 1int(val) * multiplierThis makes it extra-clear that int(val) remains the same and only the number it is multiplied with changes.Base elevenA special case for X is not needed if you think about it as base eleven, where X is the eleventh digit:def from_base_eleven(digit):    return 10 if digit == 'X' else int(digit)And then:return mod == from_base_eleven(isbn[-1])It makes the logic you're following easier to follow.