Unit 3.14/3.15 Hacks
import math
def law_of_cosines(a, b, C):
C_radians = math.radians(C)
cos_C = math.cos(C_radians)
c = math.sqrt(a**2 + b**2 - 2 * a * b * cos_C)
return c
print(law_of_cosines(1, 1 / 2, 60))
print("sqrt(3)/2=" + str(math.sqrt(3) / 2))
The above code imports the python math module and uses the radians, cos, and sqrt functions to determine the side length of the third unknown side of a triangle given the two other sides and the angle between them. First, the angle is converted from degrees to radians using math.radians. Then, the cosine of C is calculated using math.cos. Finally, the third side is calculated using the law of cosines and the math.sqrt function.
import requests
# api endpoint
url = "https://pquotes.p.rapidapi.com/api/quote"
# specify topic
payload = {"topic": "motivation"}
# rapidapi headers (key, host, and body type)
headers = {
"content-type": "application/json",
"X-RapidAPI-Key": "c4fb753596mshe0fd8cf8607e256p1eb9ebjsn7c61a5142004",
"X-RapidAPI-Host": "pquotes.p.rapidapi.com"
}
# send post request to API with body and headers; convert response from json to obj
response = requests.post(url, json=payload, headers=headers).json()
# get text and author from response
text = response["quote"]
author = response["by"]
# print quote using python format string
print(f"Your inspirational quote of the day is:\n \"{text}\" -- {author}")
from flask import Flask, request
import random
# create flask app
app = Flask(__name__)
# initialize array with quotes
quotes = [
"GOD DID - DJ Khaled",
"What do I do now - Alex Kumar",
"You are your only limit - Kalani Cabral-Omana",
"You give a man food, he's given lunch for the day. If you teach a man, then you like, can eat... I forgot what it was - Navan Yatavelli"
]
# function for retrieving random quote
def random_quote():
return random.choice(quotes)
# GET endpoint on /get returns a quote
@app.route('/get', methods=['GET'])
def get():
return {
"status": "success",
"data": {
"quote": random_quote()
}
}
# POST endpoint on /post returns a message based on req body
@app.route('/post', methods=['POST'])
def post():
data = request.json
name = data["name"]
return {
"status": "success",
"data": {
"message": "Hello, " + name
}
}
import random: importing Python's builtin random library gives you access to a variety of functions that involve pseudorandom number generators with seeds such as the current precise time. Some of its most common functions include random(), random(a, b), uniform(a, b), choice(arr), shuffle(arr), sample(arr, a), and gauss(mu, sigma).
Popular Python libraries:
requestsFlasknumpypandasmatplotlibseabornscikit-learntensorflowkerasPyQt5opencv-pythonbeautifulsoup4pygamelxmlsympyPillowscipyPyYAMLnltknetworkxsqlalchemypygletparamikopytz
import random
isPlaying = True
while isPlaying:
n = random.randint(1, 8)
if n in range(1, 3):
print("green")
elif n in range(4, 5):
print("blue")
elif n == 6:
print("purple")
elif n == 7:
print("red")
elif n == 8:
print("orange")
if input("keep playing? [y/N]") != "y":
isPlaying = False
random.randrange(12, 20) can output any number from 12 through 20, inclusive. All calls to this function will output numbers between the first parameter and second parameter, inclusive.