1 + (ternary if statement), which is what I did, but instead the entire line is the ternary if. This means it works out to be something like:
1 + something if something else something
The 1 + something is the first result, not just "something". Once again, I am somewhat disappointed in myself.
Anyway, onto recursion. Throughout most of the course, we've been slowly learning recursion step by step. The simplest way I could describe recursion to someone is that recursion is a type of function that calls itself somewhere in the body. It is used in order to reduce incredibly complex codes to the smallest "base case" in order to accomplish what you want the function to do. This is incredibly useful for things like nested lists when you're trying to find things like objects within nested lists or perhaps the maximum depth of nests within the argument. Of course, recursion is not simply limited to things like these but it is as good example to get a beginner started.
How the body of a recursive code works is that there is always what we call a "base case", as mentioned above. This base case is the simplest case, and is the only case that is evaluated instead of calling the recursive function again. Every other case will be a recursive call in order to try and simplify the code into the base case, and make things easier. An example given by our professors was the sum of the numbers inside a nested list, which can be found here.
http://www.cdf.toronto.edu/~heap/148/W14/Lectures/danny/W2/rec.py
def sum_list(L: list) -> float: """ Return sum of the numbers in L. L --- a possibly nested list of numbers. L, and all sublists, are non-empty >>> sum_list([1, 2, 3]) 6 >>> sum_list([1, [2, 3, [4]], 5]) 15 """ return sum( # sum of... # sum of sublists or else non-list elements themselves [sum_list(x) if isinstance(x, list) else x for x in L])
As you can see there is a base case, where if the object that the list comprehension comes across is not a list, then you simply add the numbers together. However, if the object IS a list, then you have to call the function again and go from there. Once again this is a very basic example.
There is still much to learn about recursion, but I think I'm really starting to get it.
Update:
the ternary if's are still affected by brackets so as you can see, the code will return different values depending on if and where you place the brackets.