Hi chase, you can ask on this community as and when you meet some difficulties. I find that easier than holding a list of questions in the backlog.
Could you try markdown when writing code? For single line code, you can surround them backticks (top left of keyboard)
like this. For blocks of code, surround them by triple backticks (both on their own new lines), ```. This makes the indentation of
for blocks correct and thus more readable.
For question 1, the
rating variable is repeatedly assigned to during iteration, with each iteration assigning a different key of the
content_ratings dictionary to
rating. It does not have to be defined before starting the iteration. I haven’t looked at the mission, but If the instruction was worded as "search for
rating", that would be strange because usually in computer science, search is a specific term implying looking for a specific item in a collection(ordered/unordered) of items. In this case i wouldn’t call it search but just iteration since there isn’t a particular item to search for. You can still argue this is search if you add a conditional in the body of the for loop, eg.
if rating == 4+: do_something(). In this case this is the linear search technique.
For question 2, what is the difference between
key? This understanding will solve your question.
For question 3,
size_freq[size] = anything is the syntax for assigning a value to an existing/new key to the dictionary. One practical example is emulating
sklearn.impute.SimpleImputer by looping through columns of a dataframe, and saving the mean value of each column into a dictionary. This dictionary would begin empty and in each iteration be populated with the key being column name and value being the mean of the column.
if size in size_freq comes before the assignment, it is logically more convenient for us to think in positives than negatives. Imagine if you wanted to reverse it, it becomes
if size not in size_freq:
size_freq[size] = 1
size_freq[size] += 1
in is now
On the grounds for typing 1 word less and positive being easier to think about, the former style is preferred.
However, there are times when the
not in pattern is preferred. Sidetrack to probability, where there are concepts of
A_complement. It may be more convenient to specify the outcomes/events you don’t want than the outcomes/events you want because there is less to write. Eg. writing
not in [8,9], when
in [0,1,2,3,4,5,6,7] also works for the event
single digits less than 8.
So above explains why order that way.
Why it is ok to order that way? Because the code will not try to access the value of the key in the dictionary unless that key (and its value which is implied) is already in the dictionary. It avoids this access (which will generate
KeyError if key wasn’t in) with the
if size in size_freq conditional key check. If key in, increment, if key not in, move to
else path of code to assign that key in with a value.
As you get familiar with this this if-else, you may move to the
get property of python dictionaries to make use of pythonic features. https://www.geeksforgeeks.org/get-method-dictionaries-python/ . Even more advanced is
That is not to say if-else is useless. It is much more explicit with what’s going on and helps readability. Also makes
git diff which operates line by line clearer.