Question
I have a question about Python creating new variables derived from other variables. I am struggling to understand how Python automatically knows how to generate variables even when I do not explicitly tell it to.
Details
I am a new Python user, and am following along in the tutorials in: Joel Grus, "Data Science From Scratch".
In the tutorial, I create three list variables:
friends
contains the number of friends that someone has on a given social networking siteminutes
refers to the number of minutes that they spend on the sitelabels
is simply an alphabetic label for each user.
Part of the tutorial is graphically plotting labels next to the points when I create a scatterplot. In doing so, Python seems to automatically generate three new variables: label
, friend_count
, and minute_count
.
In short - how? How does Python know to create these variables? And what do they do? They do not correspond to the mean, median, or mode of any of the lists.
Code
import matplotlib.pyplot as plt
from collections import Counter
def make_chart_scatter_plot(plt):
friends = [ 70, 65, 72, 63, 71, 64, 60, 64, 67]
minutes = [175, 170, 205, 120, 220, 130, 105, 145, 190]
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
plt.scatter(friends, minutes)
# label each point
for label, friend_count, minute_count in zip(labels, friends, minutes):
plt.annotate(label,
xy=(friend_count, minute_count),
xytext=(5, -5), # but slightly offset
textcoords='offset points')
plt.title("Daily Minutes vs. Number of Friends")
plt.xlabel("# of friends")
plt.ylabel("daily minutes spent on the site")
plt.show()
Thank you!
friend
and the others are just names that we give python to refer to the object created on the right. Python doesn't make it up, we tell it to use those names.for
andzip
.