Make a random password generator with Python
This is a simple Python project that's great for learning standard Python libraries, Python for loops, and function signatures.
In the video, I create a basic Python script that generates random passwords of different lengths.
The script starts by importing two modules,
string, that allow for generating random values and manipulating strings, respectively. Then, a function named
generate_password is defined that takes an integer argument,
password_length, which determines the length of the password to be generated.
generate_password function, a string named
characters is created by concatenating three strings,
string.punctuation, which contain the lowercase and uppercase letters of the alphabet, digits from 0 to 9, and various punctuation characters, respectively.
Next, an empty string named
password is created to store the generated password. A for loop is then used to iterate
password_length number of times. During each iteration, a random character is selected from the
characters string using the
random.choice method and added to the
password string is returned by the
generate_password function. The script then generates two passwords using the
generate_password function, one with a length of 12 characters and the other with a length of 20 characters, and prints them to the console using the
Python built-in libraries, for loops, and functions
Python built-in libraries
Python built-in libraries are pre-written pieces of code that provide a set of functionalities to developers without the need for them to write the code themselves. These libraries come with the standard Python installation and cover a wide range of areas, such as math, datetime, string manipulation, file I/O, networking, and more. Examples of commonly used built-in libraries in Python include random, math, datetime, os, and sys.
Python random module
Here's a link to the Python docs section.
random module is a built-in library that allows programmers to generate random numbers, sequences, and selections. It provides functions to generate random integers, random floats, random choices from a sequence, and more. It's commonly used in various applications such as games, simulations, and data analysis.
Python string module
Here's a link to the Python docs section.
In Python, the
string module provides a collection of constants (i.e., predefined values that cannot be changed) representing ASCII character sets such as lowercase and uppercase letters, digits, and punctuation. These constants can be used to manipulate strings in various ways, such as generating random passwords or filtering out unwanted characters.
Python for loops
for loop in Python is a way to iterate over a sequence of items, such as a list or a range of numbers. The basic syntax for a
for loop is:
for item in sequence:
# do something with item
In this loop,
item is a temporary variable that takes on the value of each item in
sequence, one at a time, and the indented block of code underneath the
for statement is executed once for each item in the sequence.
For example, here is a
for loop that iterates over a list of numbers and prints each one:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
This will output:
In addition to lists, you can also use
for loops to iterate over other data types such as strings, dictionaries, and sets.
Python function signatures
In Python, a function signature is the first line of the function definition, which includes the function name, any parameters or arguments that the function takes (enclosed in parentheses), and any return values (preceded by the " -> " symbol). For example, the function signature of a function named "add_numbers" that takes two integer arguments and returns an integer would be:
def add_numbers(num1: int, num2: int) -> int:
This tells us that the function is named "add_numbers", takes two integer arguments named "num1" and "num2", and returns an integer value.
In the video, we don't add in the
-> int component of the function signature. But, if you want to, you can add it like
-> str since our function outputs a string (a password as a string data type).
The full script from the video
Here's the full script we code together in the video.
characters = string.ascii_letters + string.digits + string.punctuation
password = ""
for step in range(password_length):
password += random.choice(characters)
my_password = generate_password(12)
my_other_password = generate_password(20)
Use cases for this video tutorial
As a developer, you might use the script to generate random passwords with varying levels of complexity for use in applications that require user authentication, such as login systems or password reset processes.
You could also use the script as a starting point for creating more complex password generation algorithms that incorporate additional security features, such as password strength rules and character sets.
Python projects 🐍
I enjoy making these little Python project videos very much. I'll be doing many more in the days and weeks to come.
What's a Python project and why are they awesome?
Python projects refer to coding exercises or tasks that developers work on using the Python programming language. They may involve creating software applications, building websites, automating tasks, and more.
Python projects are useful for a few reasons:
- They provide an opportunity for developers to practice and improve their coding skills.
- They can help developers learn new technologies, frameworks, and libraries.
- They allow developers to build something that solves a real-world problem or meets a specific need.
- They can be used to create portfolio pieces that demonstrate a developer's skills to potential employers or clients.
What are some Python project ideas?
There are many Python project ideas that you can work on as a beginner or an advanced programmer.
Here's a simple introduction to FastAPI video that's easy to follow and points at the FastAPI documentation throughout. It's perhaps a bit more challenging that this password generator project, but once you get the hang of building APIs with Python, you can get any data you want... and that is amazing.
Here are a few other ideas to get you started.
- Build a calculator that performs basic arithmetic operations.
- Develop a weather app that fetches data from an API and displays the current weather conditions for a given location.
- Create a password generator that randomly generates strong passwords for users.
- Build a simple game such as tic-tac-toe or hangman.
- Create a web scraper that collects data from a website and saves it to a file or a database.
- Develop a chatbot that responds to user queries and provides relevant information.
- Build a music player that plays songs from a playlist and allows users to skip tracks, pause, and resume playback.
- Create a web application that helps users manage their to-do lists.
- Develop a tool that analyzes text data and extracts insights such as sentiment, keywords, and themes.
- Build a machine learning model that predicts a certain outcome based on given input data, such as predicting stock prices or customer churn rates.