Python Key Skills - Data Validation

1 Data Validation

Learn It

  • Data Validation means to check data used or entered in a program are valid.
  • valid can mean different things depending on the context of the program. For example, it could be that your program only accepts alphabetical letters or numbers, or a number that is only within a certain range or a legitimate email address.

Code It - example 1: only accepts numbers as input

  • The easiest way of doing this is to use the Python built-in function isnumeric

Code It - example 2: only accepts numbers within expected range

  • To accomplish this, we need to use if statements and additional flag (True or False Boolean value)

Code It - example 3: only accepts alphabetical letters

  • To accomplish this, we need to use another built-in function isalpha

Try It

  • Can you work out how to validate an input that is alphabetical letters and it can only be not more than 12 characters long?