Chapter 11 and more Flashcards
What’s the only thing that will spit out a float from two intergers despite there being no remainder
Division
How do you perform an exponent equation (n to the 3rd power)
3 ** 3
What are the formatting codes for
integers
floating point numbers
To use comma separators
Percentage format
Exponent(scientific) notion
Exponent (scientific) notion with capital E
integers - :n
floating point numbers :f
To use comma separators :,
Percentage format :%
Exponent(scientific) notion :e
Exponent (scientific) notion with capital E :E
Find the square root of a number using the math import.
Remember to use formatting
import math
number = input(“Please enter a number: “)
number = int(number)
result = math.sqrt(number)
print(“The square root of {:n} is {:6f}”.format(number, result))
To find a full list of math uses, see pg 208
round numbers to the nearest whole number
a = 4.2
print(round(a))
The problem with float is that python stores floats as fractions, but converts it to decimal upon print, this doesn’t always work well. When we need a precise version of a float, what would we do?
from decimal import decimal
a = decimal(“2.3”)
b = decimal(“1.9”)
c = decimal(“8.4”)
result = a + b + c
print(str(result))
Python doesn’t have a dedicated function or module for percentages, what should you do?
multiply by 100 and add a percent sign at the end
What is mean, median, mode, and standard deviation?
Mean is the average of a data set, found by adding all numbers in the data set and then dividing by the number of values in the set123.
Median is the middle value when a data set is ordered from least to greatest. If there are 2 numbers in the middle, the median is the average of those 2 numbers134.
Mode is the number that occurs most often in a data set. Count how many times each number occurs in the data set. The mode is the number with the highest tally. It’s ok if there is more than one mode14.
Standard Deviation - how much does data deviate from the average.
Measures how spread out data is.
Low standard deviation - date closely clustered around mean
High - data dispersed over wider range
Is the data point standard and expected, or unusual and unexpected?
Is the extra ounce of soda in this bottle to be expected or is this something we need to look more into?
Calculate mean, median, mode, and standard deviation
print these out as floats using formatting
import statistics
numbers = [ 1, 4, 17, 62, 12, 84, 5, 8, 21 ]
mean = statistics.mean(numbers)
median = statistics.median(numbers)
mode = statistics.median(numbers)
stdev = statistics.stdev(numbers)
print(“mean: {:f}”.format(mean))
print(“median: {:f}”.format(median))
print(“mode: {:f}.format(mode))
print(“stdev: {:f}”.format(stdev))
Get the current date and time
import datetime
now = datetime.datetime.now()
print(now)
Print the date and the month
aside from this, what other options do you have?
import datetime
now = datetime.datetime.now()
print(now.year)
print(now.month)
day, hour, minute, second, microsecond
Let’s say we want to format datetime in different ways user our “now” variable. How would we do this?
print(now.strftime(“%A, %B, %d, %Y at %I:%M %p”))
This looks a bit more complicated than it is, but you can find what the codes represent on page 215
Finding differences in time can be a bit overwhelming when you try to subtract days and hours and years or whatever you want while taking into account leap years and things of that sort.
Let’s say we want to subtract 18 hours and 49 minutes from the current time. How would we accomplish this?
import datetime
now = datetime.datetime.now()
delta = datetime.timedelta(hours, minutes = 49)
previous_time = now - delta
print(now)
print(previous_time)
Show how many days until your birthday, tee-hee
import timedate
now = timedate.timedate.now()
birthday = timedate.timedate(2024, 7, 16, 0, 0)
print(birthday - now)
Remember to do coffee game for chapters 11, 12, 13 when you’re ready
ok