Python basic skills: Lets create an 8 characters random password generator.
Tags: python, datascience, programming, codenewbie
Published: 2021-07-24 09:09:22

## What is python?

According to the official [`python`](https://www.python.org/) website, __Python is a programming language that lets you work quickly and integrate systems more effectively__. 

A much more specific meaning would be: _its an `interpreted`, `object-oriented`, `high-level` programming language with dynamic semantics_. 

In this article we will code a python program that generates a random 8 characters long password.

A password is a secret phrase that must be used to allow access to a computer system or service.

To ensure each password generated is strong, it must contain each of the following:

- 2 random lower case letters
- 2 random upper case letters
- 2 random numeric characters
- 2 random symbols

### Breaking down the process ?

The process involves generating two characters of each type (letters, numbers and symbols), combine them in a `list`, `shuffle` them randomly and `join` them to a string.

__List__: A list is a python data type used to store multiple items in a single variable.

To begin writing the program, its important we understand the requirements.

We need to make the password as unique and random as possible. For this we need to import `random`.

__Module__: A module is a Python file that's intended to be imported into scripts 

 __library__ : Python Libraries are a set of useful functions that eliminate the need for writing codes from scratch.

### Let's code, shall we?

__Importing the required modules.__

```python
import random
import string
```
String module has a pre-initialized string used as string constant, `string.printable`.

__Create a helper function for splitting a string.__
```python
def split(word):
  return [char for char in word]
```

The `split` helper function will split `string.printable` into a list of all the individual charters of ascii.

```python
all_strings = split(string.printable)
```

Split returns a list containing all of the ascii characters, which we store in a variable `all_strings`

__Extract new lists for each type of ascii characters__

```python
digits = all_strings[0:10]
lowerCase = all_strings[10:36]
upperCase = all_strings[36:62]
syms = all_strings[62:94]
```
We use the indexing of each of the characters in the `all_strings` list to extract from and to the required index of each type of character.
> _Indexing in python starts from 0._

__Create helpers functions to generate random characters of each type__

```python
# create helper functions to generate two random characters of each
def randomLower(lowerCase):
  return random.choices(lowerCase, k=2)

def randomUpper(upperCase):
  return random.choices(upperCase, k=2)

def randomDigits(digits):
  return random.choices(digits, k=2)

def randomSyms(syms):
  return random.choices(syms, k=2)
```
__random.choices__: The `choices()` method returns a list with the randomly selected element from the specified sequence

To refactor the code further: 
```python
def twoRandom(coll):
  return random.choices(coll, k=2)
```
The function will return two random items from a list passed to in.

__Concatenate the random strings in one list__
```python
temp = twoRandom(syms)+twoRandom(digits)+twoRandom(lowerCase) +twoRandom(upperCase)

random.shuffle(temp)
passwd = "".join(temp)
```

We `concatenate` the generated string in one list and use the `shuffle()` method to shuffle the list.

 The `join()` method is used to join items in a list by a delimiter.


__Now we can print out the result__

```python
print(passwd)
```