3.14 & 3.15 Hacks
This is regarding the 3.14 and 3.15 lesson. This includes the hacks for it.
import math as mt
mt.cos(0)
The piece of code above imports the library "math" and imports it as mt. The code then uses the math library to evaluate cos(0).
Hacks 2:
-
the import random function: This functions takes 2 arguments (parameters) and chooses a random output between those arguments.
-
There are many things we can import other than random
- import pandas: widely used for data science and machine learning tasks
- import NumPy: used for working with arrays
- import math: used for doing math operations
- here is a link to some of the most important python libraries : click here
-
here are some of my favorite python libraries Requests - for HTTP operation (Internet interaction library), substitute to URL lib
Json - json parsing library
Virtualenv - for creating virtual environments
Numpy - for advanced math operations
Flask - simple and quick web framework.
Scrappy - for web scraping
TensorFlow - open source AI module from Google.
Jupyter Notebook - advanced python shell.
-
Few lines of code the uses the import function. I decide to use getpass and sys to make a python quiz. getpass() gets the username of the user and sys.excutable shows the path to the python interpreter you are using. This can be useful at times because some pieces of code run better on different kernels.
import getpass, sys
print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked three questions.")
# quiz render function
def quiz():
guesses = []
correct_guesses = 0
question_num = 1
for key in questions:
print(" ")
print(key)
for i in options[question_num-1]:
print(i)
guess = input()
guess = guess.upper()
guesses.append(guess)
correct_guesses += check_answer(questions.get(key), guess)
question_num += 1
display_score(correct_guesses, guesses)
# checks if the answer is correct or not
def check_answer(answer, guess):
if answer == guess:
print("CORRECT!")
return 1
else:
print("WRONG!")
return 0
# takes the number of correct guesses and divides it by the questions to display score
def display_score(correct_guesses, guesses):
print(" ")
print("RESULTS")
print(" ")
score = int((correct_guesses/len(questions))*100)
print("Your score is: "+str(score)+"%")
# questions formatted into a dictionary for ease of access
questions = {
"What command is used to include other functions that were previously developed?" : "A",
"What command is used to evaluate correct or incorrect response in this example?" : "C",
"Each 'if' command contains an '_________' to determine a true or false condition?" : "B"
}
# answers formatted into options that are in a double list
options = [["A. import", "B. libraries", "C. do it yourself", "D. cheat"],
["A. else", "B. boolean", "C. if", "D. for"],
["A. answer", "B. expression", "C. diet coke", "D. function"],
]
quiz()
print("Byeeeeee!")
import random
spin = random.randint(1,8)
if spin in range(1,3):
print("green")
elif spin in range(4,5):
print("blue")
elif spin == 6:
print("red")
elif spin == 7:
print("purple")
elif spin == 8:
print("orange")