"The main goal of an object oriented language is to make code reusable – we do this through the use of classes and objects. If we want to design a new type of car, we can start with what they all have in common: wheels, seats, a frame. Now that we’ve determined what cars have in common, we can more easily implement any type of car we want by starting from that basic blueprint."
"SyntaxError means there is something wrong with the way your program is written — punctuation that does not belong, a command where it is not expected, or a missing parenthesis can all trigger a SyntaxError.
A NameError occurs when the Python interpreter sees a word it does not recognize. Code that contains something that looks like a variable but was never defined will throw a NameError."
One of the projects in my Python 3 course on Codecademy was to calculate the linear regression of any given line in a set. This is my journey of doing just that, following their instructions. The goal, is to calculate the bounciness of different balls with the least error possible.
Starting with `y = m*x + b`
We can determine the y of a point pretty easily if we have the slope of the line (m) and the intercept (b). Thus, we can write a basic function to calculate the y:
```
def get_y(m, b, x):
y = m*x + b
return y
print(get_y(1, 0, 7) == 7)
print(get_y(5, 10, 3) == 25)
```
We can then use that to calculate the linear regression of a line:
```
def calculate_error(m, b, point):
x_point = point[0]
y_point = point[1]
y2 = get_y(m, b, x_point)
y_diff = y_point - y2
y_diff = abs(y_diff)
return y_diff
```
To get a more accurate result, we need a function that will parse several points at a time:
```
def calculate_all_error(m, b, points):
totalerror = 0
for point in points:
totalerror += calculate_error(m, b, point)
return abs(totalerror)
```
We can test it using their examples:
```
#every point in this dataset lies upon y=x, so the total error should be zero:
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
print(calculate_all_error(1, 0, datapoints))
#every point in this dataset is 1 unit away from y = x + 1, so the total error should be 4:
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
print(calculate_all_error(1, 1, datapoints))
#every point in this dataset is 1 unit away from y = x - 1, so the total error should be 4:
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
print(calculate_all_error(1, -1, datapoints))
#the points in this dataset are 1, 5, 9, and 3 units away from y = -x + 1, respectively, so total error should be
# 1 + 5 + 9 + 3 = 18
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
print(calculate_all_error(-1, 1, datapoints))
```
You can save all possible m and b values between -10 and 10 (m values), as well as -20 and 20 (b values) using:
```
possible_ms = [mv * 0.1 for mv in range(-100, 100)] #your list comprehension here
possible_bs = [bv * 0.1 for bv in range(-200, 200)] #your list comprehension here
```
We can find the combination that produces the least error, which is:
```
m = 0.3
b = 1.7
x = 6
```
The goal was to calculate the bounciness of different balls with the least error possible. With this data, we can calculate how far a given ball would bounce. For example, a 6 cm ball would bounce 3.5 cm. We know this because we can plug in the numbers like this:
`"%s %s - 2020" % (month, day) # Replace %s with a variable. First the month, then the day.`
`Add %03d to specify a signed integer padded 2 places with zeros. For example, 2 becomes 02.`
This is super useful for displaying dates like this: `print("%02d-%02d-%02d") % (now.month, now.day, now.year)` or time like this: `print '%02d:%02d:%04d' % (now.hour, now.minute, now.second)` (Ch3, Ex. 4)
Say you only want to allow people over the height of 161 to ride your roller coaster for safety reasons. You could do this to create a list of only those heights about 161.
It will give you all numbers under the number you input. For example, `range(8)` would give you 0-7, `range(1, 8)` would give you 1-7, and `range(1, 8, 2)` would give you 1,3,5,7 (2 is the interval and needs no padding) Use `print(list(var))` when printing.
The following assigns an arbitrary name to a temporary variable (tempvar) to the number of elements in `sport_games` For every element in `sport_games`, it will print `print(tempvar)`
```
for tempvar in sport_games:
print(tempvar)
```
You can use `range()` to get lists of arbitrary lengths.