Login Authentication in Python

Simulate a simple login system by comparing username and password with stored values.

BeginnerConditional ProgramsExample 18 of 20
login-authentication.py
1# Simple login authentication
2
3USERNAME = "admin"
4PASSWORD = "secret123"
5
6username = input("Enter username: ")
7password = input("Enter password: ")
8
9if username == USERNAME and password == PASSWORD:
10 print("Login successful")
11else:
12 print("Invalid username or password")

Output

Enter username: admin
Enter password: secret123
Login successful

What's going on

USERNAME and PASSWORD are hardcoded. Real apps do not do this. Both have to match; a right password with the wrong user still fails. input() does not hide the password. Fine for a demo, useless for anything real.

After this program

If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.