Extra Credit
import random
# Characters that will be used to generate the password
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*'
# Ask user for the length of the password
length = int(input('Enter the length of the password: '))
# Generate and print the random password
rand_pass = ''.join(random.sample(chars, length))
#The random.sample() function is used to randomly select a specified number of characters (length) from the chars string.
#The resulting list of characters is then passed to the join() method of the empty string (''),
# which concatenates the characters into a single string. The resulting string is stored in the rand_pass variable.
print('Your randomly generated password is: ', rand_pass)
# Checks how easily guessed the password is
if len(rand_pass) > 15:
print("Very strong password")
elif 9 < len(rand_pass) < 15:
print("strong password")
elif 5 < len(rand_pass) < 9:
print("decently strong password")
else:
print("weak password")
This code generates a random password of a specified length and then checks how strong the password is based on its length.
The first line imports the random module, which provides functions for generating random numbers and selecting random elements from a list.
The chars string contains a list of all the characters that will be used to generate the password. This includes lowercase and uppercase letters, numbers, and special characters.
The code then asks the user to enter the desired length of the password using the input function. This value is converted to an integer using the int function and stored in the length variable.
The random.sample function is used to select length random characters from the chars string, and these characters are joined together using the join method of the string object to form the random password. This password is then printed to the screen.
Finally, the code uses an if statement to check the length of the password and print a message indicating how strong the password is. If the password is longer than 15 characters, it is considered a very strong password. If it is between 9 and 15 characters long, it is considered a strong password. If it is between 5 and 9 characters long, it is considered a decently strong password. Otherwise, it is considered a weak password.
def email_slicer(email):
# Split the email address at the '@' character
username, domain = email.split('@')
return username, domain
# Test the email slicer
print(email_slicer('john.doe@gmail.com'))
This email slicer uses the built-in split method to split the email address at the '@' character, and returns a tuple containing the username and domain name.
Note that this email slicer does not perform any validation on the input email address. It is up to you to ensure that the email address is in the correct format before calling the email slicer.
class AddressBook:
def __init__(self):
# Initialize an empty dictionary to store the contact details
self.contacts = {}
def create_contact(self, name, phone, email):
# Add the contact details to the dictionary
self.contacts[name] = {'phone': phone, 'email': email}
def edit_contact(self, name, phone=None, email=None):
# Check if the contact exists in the dictionary
if name in self.contacts:
# Update the phone and/or email if provided
if phone:
self.contacts[name]['phone'] = phone
if email:
self.contacts[name]['email'] = email
return True
else:
return False
def delete_contact(self, name):
# Remove the contact from the dictionary if it exists
if name in self.contacts:
del self.contacts[name]
return True
else:
return False
def get_contacts(self):
# Return a copy of the contacts dictionary
return self.contacts.copy()
# Create a new address book
address_book = AddressBook()
# Add some contacts
address_book.create_contact('Nikhil Chakravarthula', '415-937-3746', 'nikhilchakravarthula14@gmail.com')
address_book.create_contact('Anitha Ganjam', '510-717-7990', 'anitha.ganjam@gmail.com')
address_book.create_contact('Prashanth Chakravarthula', '415-341-3968', 'prashanth.chakravarthula@gmail.com')
# Edit a contact
address_book.edit_contact('Nikhil Chakravarthula', email='Nikhilc@yahoo.com')
# Delete a contact
address_book.delete_contact('Anitha Ganjam')
# Get the contacts
print(address_book.get_contacts())
The AddressBook class has four methods: create_contact, edit_contact, delete_contact, and get_contacts.
The init method initializes an empty dictionary called contacts to store the contact details.
The create_contact method takes three arguments: name, phone, and email, and adds a new contact to the contacts dictionary with these details. The contact's name is used as the key, and the phone and email are stored as a dictionary value.
The edit_contact method takes three optional arguments: name, phone, and email. It checks if the contact with the specified name exists in the contacts dictionary, and if it does, it updates the phone and/or email if provided. If the contact does not exist, it returns False.
The delete_contact method takes a single argument: name, and removes the contact with the specified name from the contacts dictionary if it exists. If the contact does not exist, it returns False.
The get_contacts method returns a copy of the contacts dictionary.
To use the AddressBook class, you can create a new instance of the class and call the various methods to create, edit, delete, and retrieve contacts.
pip install yfinance
import yfinance as yf
ticker = 'AAPL', 'AMZN', 'WMT', 'GOOGL', 'MSFT', 'MCD', 'NVDA', 'TSLA', 'COST', 'MA'
start_date = '2017-01-01'
end_date = '2023-03-01'
price = yf.download(ticker, start_date, end_date)['Adj Close']
print(price)
import matplotlib.pyplot as plt
price["AMZN"].plot(label="AMZNStock")
plt.title("AMZN")
plt.xlabel("year")
plt.ylabel("price")
This code is using the yfinance package to download daily stock price data for a list of ticker symbols from Yahoo Finance. The yfinance package is a wrapper for the Yahoo Finance API that makes it easy to retrieve stock data.
The ticker symbols for the stocks to be downloaded are specified as a tuple of strings: 'AAPL', 'AMZN', 'WMT', 'GOOGL', 'MSFT', 'MCD', 'NVDA', 'TSLA', 'COST', 'MA'.
The start and end dates for the data to be downloaded are specified as strings in the format 'YYYY-MM-DD': '2017-01-01' and '2022-01-01', respectively.
The yf.download() function is then called with the ticker symbols, start date, and end date as arguments, and the resulting data is stored in a variable called price. The ['Adj Close'] attribute is used to select only the "Adjusted Close" price column from the data.
Finally, the price data is printed to the console using the print() function.
Then the next line chunk of code just plots Amazons stock into a graph.