top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

How to win against brilliant hackers - generating super password in Python


image url: https://unsplash.com/photos/iar-afB0QQw


How to generate a complex password using Python Lists and Loops?


We will discuss this by two methods. First one is easier, the other is a little bit harder. The harder one will generate more complex passwords.


Easy level:


To generate a random password by specifying the total number of characters (including alphabets, digits and special characters) in a random order from the combined list.

First, we will create a list of alphabets, numbers and special characters which are to be used in the password.



Now, we have to import random module in the Jupyter notebook to pick up random characters from the lists.


Import random


Ask the user how many alphabets, digits and special characters they would like to have in the password.


print("Ready to generate your super password? ")

no_of_alphabets= int(input("Please specify how many alphabets you would like in your password.\n"))

no_of_digits= int(input(f"Please specify how many digits you would like in your password.\n"))

no_of_special_characters= int(input(f"Please specify how many special_characters you would like in your password.\n"))


Start with creating an empty string:

p=” “


Let’s create a master list by adding all the 3 lists.


Master list (mlist)=alphabets+digits+special_characters


Now, calculate the total number of characters needed in the password by adding the number of alphabets, special characters and numbers.


Password_length=no_of_alphabets+no_of_digits+no_of_special_characters


And print it out to check,if you want.


print(Password_length)


Create a For loop to calculate the number of times the loop should travel in the list to pick up random characters. And, it should be equal to password length. In Python, character position starts with zero, so we have to take the limit as password length plus 1.


for m in range (1, Password_length+1):

Let's pick random values from the master list using random.choice function, and keep on storing the randomly generated characters in the password.


random_password=random.choice(mlist)

p+=random_password


Last step, print out our super password.


print(f"Your super password is {p}")


The whole above steps in Jupyter notebook are as follows:



The result in Jupyter notebook will be printed as:




Everytime we run the query, a different password will be generated by Python.



Hard level- For more complex passwords:


To generate a password which has specified number of alphabets, digits and special characters from different lists (shuffled in the end).


# Assuming you already have random module, else please import random module here.


First, we will ask the user how many alphabets, numbers and special characters the user wants in the password.


print("Ready to generate your super password? ")

no_of_alphabets= int(input("Please specify how many alphabets you would like in your password.\n"))

no_of_digits= int(input(f"Please specify how many digits you would like in your password.\n"))

no_of_special_characters= int(input(f"Please specify how many special_characters you would like in your password.\n"))


Because we have to shuffle the characters afterwards, we have to use the list in Python.

Let’s create an empty list to keep on adding the characters as we will loop through the list.


p_l=[ ]


Create a master list by adding all the 3 lists.


Master list (mlist)=alphabets+digits+special_characters

Now, calculate the total number of characters needed in the password by adding the number of alphabets, special characters and numbers.


Password_length=no_of_alphabets+no_of_digits+no_of_special_characters


And print it out to check,if you want.


Now, we will loop through each of the lists separately because we want the exact number of alphabets, digits and special characters as the user specified.


Create a For loop for each of the lists and pick up random alphabets, digits and special characters by random.choice function. The characters in the lists can be added with the append function. We will keep on adding the characters one by one in the empty list to get the final password. It will be written in python as:


for al in range(1, no_of_alphabets+1):

random_alphabet=random.choice(alphabets)

p_l.append(random_alphabet)

for dg in range(1, no_of_digits+1):

random_digit=random.choice(digits)

p_l.append(random_digit)

for sc in range(1, no_of_special_characters+1):

random_special_character=random.choice(special_characters)

p_l.append(random_special_character)


Lets see how our password looks at this stage, by printing it out.


print(p_l)


The output of this print statement will be:


['r', 'n', 'F', 'c', '0', '6', '2', '0', '#', '%', '{', '$', '$']


#Everytime it will be different as it is random.


As you must have noticed, it doesn’t look like a password at this stage, it is a list. Our next job is to shuffle this list and convert it into a string to create our complex password.


Shuffle the password by using random.shuffle function.


random.shuffle(p_l)


Lets create an empty string ‘pswd’.


pswd=" "


Again, we have to create a For loop to add on the randomly picked characters in our final password.


for character in p_l:

pswd+=character


Finally, print out our complex super password.


print (f"Your super password is {pswd}")


The whole above steps in Jupyter notebook are as follows:



The output in Jupyter notebook will be generated as:



Everytime we run the query, a different password will be generated by Python.


Thanks for reading!


95 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page