Skip to content

Instantly share code, notes, and snippets.

@talbii
Last active October 2, 2023 21:35
Show Gist options
  • Select an option

  • Save talbii/83ac4aaa6d6ed975a368fd9282ddd307 to your computer and use it in GitHub Desktop.

Select an option

Save talbii/83ac4aaa6d6ed975a368fd9282ddd307 to your computer and use it in GitHub Desktop.
I keep seeing "password generator in Python" videos/comments which are written horribly, so I've decided to write a decent one instead. *Not cryptographically secure.*
from random import choices
#from secrets import choice # cryptographically secure version:
#choices = lambda N : [choice(letters) for _ in range(N)]
import string
letters = string.ascii_letters + string.digits + string.punctuation
password_gen = lambda N : ''.join(choices(letters, k=N))
if __name__ == '__main__':
n = int(input("Enter a positive integer: "))
print(password_gen(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment