0% found this document useful (0 votes)
44 views8 pages

Python Lab Day 3 Codes

The document describes two Python projects - a secure password generator that allows a user to specify the number of letters, symbols and numbers to include in a randomly generated password, and a menu-driven number base converter that allows conversion between decimal, binary and hexadecimal numbering systems.

Uploaded by

santwanpathak1
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)
44 views8 pages

Python Lab Day 3 Codes

The document describes two Python projects - a secure password generator that allows a user to specify the number of letters, symbols and numbers to include in a randomly generated password, and a menu-driven number base converter that allows conversion between decimal, binary and hexadecimal numbering systems.

Uploaded by

santwanpathak1
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/ 8

Netaji Subhash Engineering College

Department of Computer Science & Engineering


B. Tech CSE 2nd Year 3rd Semester
2023-2024
___________________________________________________________________________

Name of the Course: IT Workshop (Python)


Course Code: PCC CS 393
Name of the Student: Santwan Pathak
Class Roll No: 158
University Roll No: 10900122161
Date of Experiment: 19th October 2023
Date of Submission: 2nd November 2023
__________________________________________________________________________

Python Project : Secure Password Generator

# pYTHON PASSWORD GENERATOR

import random

letters =
['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j',

'k','l','z','x','c','v','b','n','m','Q','W','E','R','T','Y','U','I',

'O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'
]

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"))

#Eazy Level - Order not randomised:

#e.g. 4 letter, 2 symbol, 2 number = JduE&!91

# password = ''
# for char in range(0 , nr_letters):
# password += random.choice(letters)

# for char in range( 0 , nr_symbols):


# password += random.choice(symbols)

# for char in range ( 0 , nr_numbers):


# password += random.choice(numbers)

# print(password)

#Hard Level - Order of characters randomised:

#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P

password_list = []

for char in range(0 , nr_letters):


password_list += random.choice(letters)

for char in range( 0 , nr_symbols):


password_list += random.choice(symbols)

for char in range ( 0 , nr_numbers):


password_list += random.choice(numbers)

random.shuffle(password_list)
password =''
for char in password_list :
password += char

print("Your Password is:" , password)

Output :

Welcome to the PyPassword Generator!


How many letters would you like in your password?
2
How many symbols would you like?
2
How many numbers would you like?
2
Your Password is: )q2a7$

Welcome to the PyPassword Generator!


How many letters would you like in your password?
2
How many symbols would you like?
2
How many numbers would you like?
2
Your Password is: W2#7+b

Welcome to the PyPassword Generator!


How many letters would you like in your password?
8
How many symbols would you like?
6
How many numbers would you like?
4
Your Password is: &1yG&zzx823%q#()Sd
Python Project : Menu Driven Number Base Converter

# Conversion function to convert decimal to binary


def decimal_to_binary(decimal):

# Use bin() to convert decimal to binary and [2:] to remove the "0b"
prefix
return bin(decimal)[2:]

# Conversion function to convert decimal to hexadecimal


def decimal_to_hexadecimal(decimal):

# Use hex() to convert decimal to hexadecimal and [2:] to remove the "0x"
prefix
return hex(decimal)[2:]

# Conversion function to convert binary to decimal


def binary_to_decimal(binary):

# Use int() to convert binary string to a decimal integer with base 2


return int(binary, 2)

# Conversion function to convert binary to hexadecimal


def binary_to_hexadecimal(binary):

# Convert binary to decimal using binary_to_decimal function, then to


hexadecimal
decimal_value = binary_to_decimal(binary)
return decimal_to_hexadecimal(decimal_value)

# Conversion function to convert hexadecimal to decimal


def hexadecimal_to_decimal(hexadecimal):

# Use int() to convert hexadecimal string to a decimal integer with base


16
return int(hexadecimal, 16)

# Conversion function to convert hexadecimal to binary


def hexadecimal_to_binary(hexadecimal):
# Convert hexadecimal to decimal using hexadecimal_to_decimal function,
then to binary
decimal_value = hexadecimal_to_decimal(hexadecimal)
return decimal_to_binary(decimal_value)
# Main program
while True:
# Display the menu

print("Number Base Converter Menu:")


print("1. Decimal to Binary")
print("2. Decimal to Hexadecimal")
print("3. Binary to Decimal")
print("4. Binary to Hexadecimal")
print("5. Hexadecimal to Decimal")
print("6. Hexadecimal to Binary")
print("7. Exit")

# Ask the user to choose an option


choice = input("Enter your choice (1-7): ")

# Check the user's choice


if choice == '1':
decimal_num = int(input("Enter a decimal number: "))
print("Binary:", decimal_to_binary(decimal_num))

elif choice == '2':


decimal_num = int(input("Enter a decimal number: "))
print("Hexadecimal:", decimal_to_hexadecimal(decimal_num))

elif choice == '3':


binary_num = input("Enter a binary number: ")
print("Decimal:", binary_to_decimal(binary_num))

elif choice == '4':


binary_num = input("Enter a binary number: ")
print("Hexadecimal:", binary_to_hexadecimal(binary_num))

elif choice == '5':


hex_num = input("Enter a hexadecimal number: ")
print("Decimal:", hexadecimal_to_decimal(hex_num))

elif choice == '6':


hex_num = input("Enter a hexadecimal number: ")
print("Binary:", hexadecimal_to_binary(hex_num))

elif choice == '7':


print("Exiting the Number Base Converter.")
break
else:
print("Invalid choice. Please select a valid option (1-7).")
Output :
Number Base Converter Menu:
1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Binary to Hexadecimal
5. Hexadecimal to Decimal
6. Hexadecimal to Binary
7. Exit
Enter your choice (1-7): 1
Enter a decimal number: 7
Binary: 111

Number Base Converter Menu:


1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Binary to Hexadecimal
5. Hexadecimal to Decimal
6. Hexadecimal to Binary
7. Exit
Enter your choice (1-7): 2
Enter a decimal number: 7
Hexadecimal: 7

Number Base Converter Menu:


1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Binary to Hexadecimal
5. Hexadecimal to Decimal
6. Hexadecimal to Binary
7. Exit
Enter your choice (1-7): 2
Enter a decimal number: 12
Hexadecimal: c

Number Base Converter Menu:


1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Binary to Hexadecimal
5. Hexadecimal to Decimal
6. Hexadecimal to Binary
7. Exit
Enter your choice (1-7): 2
Enter a decimal number: 98546
Hexadecimal: 180f2

Number Base Converter Menu:


1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Binary to Hexadecimal
5. Hexadecimal to Decimal
6. Hexadecimal to Binary
7. Exit
Enter your choice (1-7): 3
Enter a binary number: 01110101
Decimal: 117

Number Base Converter Menu:


1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Binary to Hexadecimal
5. Hexadecimal to Decimal
6. Hexadecimal to Binary
7. Exit
Enter your choice (1-7): 4
Enter a binary number: 111
Hexadecimal: 7

Number Base Converter Menu:


1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Binary to Hexadecimal
5. Hexadecimal to Decimal
6. Hexadecimal to Binary
7. Exit
Enter your choice (1-7): 5
Enter a hexadecimal number: a59
Decimal: 2649

Number Base Converter Menu:


1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Binary to Hexadecimal
5. Hexadecimal to Decimal
6. Hexadecimal to Binary
7. Exit
Enter your choice (1-7): 6
Enter a hexadecimal number: f
Binary: 1111

Number Base Converter Menu:


1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Binary to Hexadecimal
5. Hexadecimal to Decimal
6. Hexadecimal to Binary
7. Exit
Enter your choice (1-7): 7
Exiting the Number Base Converter.

You might also like