Python
Here are the Python projects that I completed in my free time, just for fun
Calculator: takes in two numbers and asks the user to choose an operation to perform
# Define the addition function
def add(a, b):
return a + b
# Define the subtraction function
def subtract(a, b):
return a - b
# Define the multiplication function
def multiply(a, b):
return a * b
# Define the division function
def divide(a, b):
return a / b
# Get input from user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Display the available operations
print("Select an operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
# Get the user's choice of operation
choice = int(input("Enter your choice (1/2/3/4): "))
# Perform the selected operation and display the result
if choice == 1:
print(num1, "+", num2, "=", add(num1, num2))
elif choice == 2:
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == 3:
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == 4:
print(num1, "/", num2, "=", divide(num1, num2))
else:
print("Invalid choice. Please try again.")
Converter: Does different types of mathematical conversions
def math_conversion():
print("Select a conversion type:")
print("1. Fahrenheit to Celsius")
print("2. Celsius to Fahrenheit")
print("3. Miles to kilometers")
print("4. Inches to centimeters")
print("5. Feet to meters")
print("6. Yards to meters")
print("7. Pounds to kilograms")
print("8. Ounces to grams")
print("9. Gallons to liters")
choice = input("Enter choice (1-9): ")
if choice == '1':
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5/9
print(f"{fahrenheit}°F is equal to {celsius:.2f}°C")
elif choice == '2':
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit:.2f}°F")
elif choice == '3':
miles = float(input("Enter distance in miles: "))
kilometers = miles * 1.60934
print(f"{miles} miles is equal to {kilometers:.2f} kilometers")
elif choice == '4':
inches = float(input("Enter length in inches: "))
centimeters = inches * 2.54
print(f"{inches} inches is equal to {centimeters:.2f} centimeters")
elif choice == '5':
feet = float(input("Enter length in feet: "))
meters = feet * 0.3048
print(f"{feet} feet is equal to {meters:.2f} meters")
elif choice == '6':
yards = float(input("Enter length in yards: "))
meters = yards * 0.9144
print(f"{yards} yards is equal to {meters:.2f} meters")
elif choice == '7':
pounds = float(input("Enter weight in pounds: "))
kilograms = pounds * 0.453592
print(f"{pounds} pounds is equal to {kilograms:.2f} kilograms")
elif choice == '8':
ounces = float(input("Enter weight in ounces: "))
grams = ounces * 28.3495
print(f"{ounces} ounces is equal to {grams:.2f} grams")
elif choice == '9':
gallons = float(input("Enter volume in gallons: "))
liters = gallons * 3.78541
print(f"{gallons} gallons is equal to {liters:.2f} liters")
else:
print("Invalid choice")
raspberry pi gpio...
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
while True:
GPIO.output(18, GPIO.HIGH)
time.sleep(1)
GPIO.output(18, GPIO.LOW)
time.sleep(1)
AI SPOTIFY RECOMMENDER
import csv
import random
from ColumnEnum import ColumnEnum
import HDBSCAN
import copy
artistEnum = {}
def main():
data = loadData()
cData = cleanData(copy.deepcopy(data))
#print(str(data))
modelTuple = HDBSCAN.TrainModel(cData)
while True:
index = int(len(data) * random.random())
print("input song: " + data[index][ColumnEnum.name.value] + " by " + data[index][ColumnEnum.artists.value])
ret = HDBSCAN.GetRecommendation(modelTuple, cData[index], data)
if ret != -1:
input("output song: " + ret[ColumnEnum.name.value] + " by " + ret[ColumnEnum.artists.value])
else:
input("press enter to continue")
# loads sample data from file
def loadData():
data = []
with open('Dataset/spotify.csv', newline='\r\n', encoding='utf8') as csvfile:
reader = csv.reader(csvfile)
skipFirst = True
for row in reader:
if skipFirst:
skipFirst = False
continue
data.append(row)
return data
# converts data to all numeric values for training and testing
# artist names will be added to a dictionary for consistency
# TODO: output this dictionary to a file
# TODO: load dictionary from file on startup
def cleanData(data):
for row in data:
# editing items in descending index order to avoid messing up indexing
# avoiding converting date to numeric plus year covers information
row.pop(ColumnEnum.release_date.value)
# name has no relevance on song similarity (in my opinion)
row.pop(ColumnEnum.name.value)
# ID is not relevany
row.pop(ColumnEnum.id.value)
# convert artist to enumeration
if row[ColumnEnum.artists.value] not in artistEnum:
val = len(artistEnum)
artistEnum[row[ColumnEnum.artists.value]] = val
row[ColumnEnum.artists.value] = val
else:
val = artistEnum[row[ColumnEnum.artists.value]]
row[ColumnEnum.artists.value] = val
# cast all data in list to floats
i = 0
while i < len(row):
row[i] = float(row[i])
i = i + 1
# data = data[0:1000]
return data
if __name__ == "__main__":
main()