Naming Slices

The built-in slice function creates a slice object tha can be used anywhere a slice is allowed. There are three arguments to to fill in, start, end, and step. The object returns the values at the indices specified in the arguments. If you only provide one argument, by default, slice will interpret it as the end. items = [0, 1, 2, 3, 4, 5, 6] section = slice(2, 4, 1) initial = slice(4) items[section] ## [2, 3] items[initial] ## [0, 1, 2, 3] More on slices can be found here. …

Posted on

Applications of Linear Functions

import matplotlib.pyplot as plt In this post, we’ll look at some textbook applications of linear functions. Although these are contrived problems taken from a book, they shed some light on how to think about using linear functions to solve real business problems. Highway tolls Since heavier vehicles are responsible for more of the wear and tear on highways, drivers should pay tolls in direct proportion to the weight of their vehicles. …

Posted on

Slope and Linear Functions

The graph of \(y = c\), or \[f(x) = c\] a horizontal line is the graph of a function. We call such a function a constant function. A constant function is one whose output value is the same for every input value. The graph of \(x = a\) is a vertical line, and \(x = a\) is not a function. import matplotlib.pyplot as plt def constant(x): return 4 y_values = [constant(x) for x in range(1,10)] plt. …

Posted on

Calculating with Dictionaries in Python

To perform useful calculations on the contents of a dictionary, it is often useful to invert the keys and values of the dictionary using zip(), which creates an iterable tuple of a dictionary’s keys and values. prices = { "ACME": 45.23, "AAPL": 612.78, "IBM": 205.55, "HPQ": 37.20 } prices ## {'ACME': 45.23, 'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.2} zipped = zip(prices.values(), prices.keys()) for v, k in zipped: print(v, k) ## 45. …

Posted on

Finding Commonalities in Dictionaries in Python

A Python dictionary is a mapping between a set of keys and values. The keys() method supports common set operations such as unions, intersections, and differences. Same goes for the items() method. However, that’s not the case with the values() method since the values of a dictionary are not guaranteed to be unique. Based on these notions, we can compare dictionaries and see what they have in common with basic set operations. …

Posted on