liveBook · Manning

Password authentication

As explained in Learn AI-Assisted Python Programming, Second Edition

A password is a secret character sequence used to authenticate a user or protect access to an account or system.

A password is text used to decide whether an input meets a stated strength or validity rule. A Boolean result reports True or False. section 3.3.2

Build password checks by making the rule explicit:

  • Start with a simple rule: reject the exact strings "password" and "qwerty". section 3.3.2
  • Return a Boolean from a predicate such as is_strong_password(password). section 3.3.2
  • Strengthen the rule by requiring an uppercase character, a number, and a special symbol or punctuation mark. section 3.3.2
  • Test rejected, accepted, and boundary-like examples rather than trusting generated code automatically. section 3.3.2

Testing a stronger rule

A check rejects "Leo" because it lacks the required character categories, accepts "N3w Y0rk J375$", and rejects "N3w Y0rk J375" when punctuation is required. section 3.3.2

import string

def is_strong_password(password):
    """
    A strong password has at least one uppercase character,
    at least one number, and at least one punctuation.
 
    Return True if the password is a strong password, False if not.
    """
    return any(char.isupper() for char in password) and \
           any(char.isdigit() for char in password) and \
           any(char in string.punctuation for char in password)

Use the complete Python version to inspect uppercase, digit, and punctuation checks; importing string is necessary before using string.punctuation. section 3.3.2

def is_valid_password(password):
    if len(password) <= 8:
        if any(char.isupper() for char in password):
            if any(char.islower() for char in password):
                if any(char.isdigit() for char in password):
                    return True
            return False
        return False
    return False

Use the faulty exercise implementation to diagnose a length-condition error while practicing multi-criterion validation. section 8.8

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines progressively stricter checks and demonstrates implementations, tests, and an import failure, but stops short of real-world password security.
Examples
Tests password, qwerty, Leo, N3w Y0rk J375, and N3w Y0rk J375$ against changing validation rules.
What it assumes
Assumes no prior programming knowledge and teaches strings, Booleans, comparisons, and character checks from the beginning.
What you can do after
Build and debug Python password validators that return Boolean results and verify them with test calls.
How it connects
Connects strings, Boolean values, functions, conditional logic, iteration, imports, and debugging.

Also explained in

Compare how 9 books explain password authentication →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.