How to Create Password Generator Program in Python, Using Loop, and Random Module

The inspiration for this “Password generator program” comes from the fact that companies seem to be getting hacked these days. The main reason is by keeping the same password for different sites from Google, Facebook, and all other random services. So if any one of these sites got hacked, your email and password combination is exposed, So it is one step away from trying the same combination on all other sites.
So to keep things safe, It is always recommended to create a password with more complexity and use different passwords for different sites. So, here comes our password generator program, which helps to generate your complex password.
Below are concepts, We need to be aware of to succeed in this program creation.
— Loop
— Random Module
Loop
The loop is used to iterate a sequence like a list, range, etc
Example:
fruits = ["banana", "kiwi", "avocado"] for fruit in fruits: print(fruit)
— Above code, Will run 3 times (one for each element in a list) and will print all the elements in the list
banana kiwi avocado
Random Module
Random modules help generate unpredictable output (random numbers from a range). Check here to learn more about Random Module
random.choice (list)
— Above code, Will fetch a random element from the given list
There are multiple ways to implement this program, let’s see one the example
Code
import random letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+'] print("Welcome to the PyPassword Generator!") nr_letters= int(input("How many letters would you like in your password?\n")) nr_symbols = int(input(f"How many symbols would you like?\n")) nr_numbers = int(input(f"How many numbers would you like?\n")) pwd=[] for char in range(1, nr_letters +1): pwd.append(random.choice(letters)) for char in range(1,nr_symbols +1): pwd.append(random.choice(symbols)) for char in range(1,nr_numbers +1): pwd.append(random.choice(numbers)) random.shuffle(pwd) fpwd="" for i in pwd: fpwd+=i print(fpwd)
Code Explanation:
— In this code, We are getting user input and customizing the code based on the input
— Let’s say the user needs a password with 4 chars, 2 numbers, and 3 symbols, based on the input we need to generate the password
— We are using 3 for loops to generate char, number, and symbol based on the length.
— By using the list append method, We are appending the values to the list “pwd”
— Now, To increase the complexity, We are shuffling the list to make sure it is not readable and easy to crack.
Conclusion,
Congratulations on learning to code a password generator program, Hard work, and persistence beats raw talents. So keep going check here for more posts related to the python learning series