Hi guys,
I am having some trouble understanding why a variable needs to be declared to use a method that returns a value.
In the exercise I’m currently working on (https://www.codecademy.com/courses/learn-python-3/lessons/use-python-list/exercises/count), it says ‘Notice that since .count() returns a value, we must assign it to a variable to use it.’
The example it provides is;
letters = ["m", "i", "s", "s", "i", "s", "s", "i", "p", "p", "i"]
num_i = letters.count("i")
print(num_i)
# Output is 4
However, I seem to be able to achieve the same result without declaring a variable;
letters = ["m", "i", "s", "s", "i", "s", "s", "i", "p", "p", "i"]
print(letters.count("i"))
# Output is 4
Please could someone explain this for me? Is it best practice to declare any use of a method as a variable, for some reason that hasn’t been explained in the course yet?
Thanks for your help,
Tom