Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Code Python

some easy python code

Uploaded by

prernadjschool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Code Python

some easy python code

Uploaded by

prernadjschool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CODE FOR THE RANDOM PASSWORD GENERATOR :

import secrets
import string

def generate_password(length):
# Define the characters to use in the password
characters = string.ascii_letters + string.digits + string.punctuation

# Generate the password


password = ''.join(secrets.choice(characters) for _ in range(length))
return password

# Example usage
password_length = 12 # Set the desired length of the password
print("Generated Password:", generate_password(password_length))

Explanation of the Simplified Code:

1. **Imports**:
- `secrets`: For generating secure random numbers.
- `string`: For pre-defined character sets.

2. **Function `generate_password(length)`**:
- Takes a `length` argument to specify how long the password should be.
- `characters` combines letters, digits, and special characters into one string.
- `secrets.choice(characters)` picks a random character from the combined set.
- `''.join(...)` assembles the chosen characters into a single string.

3. **Main Block**:
- Sets `password_length` to 12 (you can change this to any length you want).
- Prints the generated password.
ANOTHER RANDOM CODE :

import secrets
import string

def generate_password(length=12):
# Define the character sets to choose from
alphabet = string.ascii_letters # a-z and A-Z
digits = string.digits # 0-9
special_characters = string.punctuation # Special characters like !@#$%^&*

# Combine all character sets


all_characters = alphabet + digits + special_characters

# Generate a secure random password


password = ''.join(secrets.choice(all_characters) for _ in range(length))
return password

# Example usage
if __name__ == "__main__":
password_length = 16 # Set the desired length of the password
print("Generated Password:", generate_password(password_length))

You might also like