Check out this code in my interpreter:
$ python3
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
<<< first_list = [1,2,3,4,5,6]
<<< second_list = [101,202,303,404]
<<< first_list.reverse()
<<< print(first_list)
[6, 5, 4, 3, 2, 1]
<<< reverse(second_list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'reverse' is not defined
<<<
In the first line and second lines, I define two lists. Then I proceed to reverse the order of the first_list and second_list in two different ways. The first reversal succeeds whereas the second way is rejected.
Putting the variable inside the reverse function as a parameter is how I initially would naturally use it if I were to write a script (which evidently would be rejected by the Python interpreter).
The instructor in the Udemy course I am taking suggests putting the variable in front of the function separated by a dot. I understand that the computer dictates what works and what doesn’t. I just want to know why since my (faulty) approach comes so much more naturally.
How come variables sometimes have to go in front of the function when other times it can go inside a function as a parameter?
I suppose the much more important question I now have is this: When I am manipulating variables, how do I know when to put variables outside vs inside?
Thanks for your attention.