Jun-02-2018, 06:20 AM
You are trying two different things.
This guy, the instructor, is just lazy.
You have two lists. These lists are not variables but objects, instances of a class in Python.
The variables defined inside a class we call attributes of that class and the functions defined inside a class we call methods.
Each data type class in Python has its own methods. How definition of a class looks? Something simple.
So this instance of the class list has its own methods. Calling a method is different from calling a function. Let's define a simple function.
Calling a function is simple.
We doing it as putting the class/instance along with its method, separated by a period.
So it becomes:
However, there is a built-in function in Python called 'reversed'. Not 'reverse'.
Instead of the whole list or a sequence, they return a single element. It saves memory. For the examples we are using here it doesn't matter because the lists are small. But nowadays almost nothing is small when we are talking about data.
You have to pass the iterator to a for loop for instance or to a function which knows what to do with it.
This guy, the instructor, is just lazy.
You have two lists. These lists are not variables but objects, instances of a class in Python.
>>> first_list = [1,2,3,4,5,6] >>> second_list = [101,202,303,404] >>> type(first_list) <class 'list'> >>> type(second_list) <class 'list'>Well, classes are constructions which can contain variables ( instances of predefined classes in Python as integers, lists, strings, etc. ) and functions to operate with these variables.
The variables defined inside a class we call attributes of that class and the functions defined inside a class we call methods.
Each data type class in Python has its own methods. How definition of a class looks? Something simple.
>>> class Greeting: ... def say(self): # this is a function defined inside a class definition so it's called a method of that class ... print('Hello!') ... >>> greeting = Greeting() # here an instance of a class called 'greeting' is createdWhen you define a variable in Python as a list, for example, it happens basically the same but the syntax is a bit different and Python is doing its things behind so you don't see it.
first_list = [1,2,3,4,5,6] # 'first_list is an instance of a class listAs I showed you at the beginning the lists are just classes in Python. Like the integers or all the other data types in Python. Everything in Python.
So this instance of the class list has its own methods. Calling a method is different from calling a function. Let's define a simple function.
>>> def say(object): ... print(object) ...Now we have a method of the class Greetings called 'say' and a function 'say'.
Calling a function is simple.
>>> say(first_list) [1, 2, 3, 4, 5, 6]Calling a method of a class ( or instance of a class ) is simple too. But we need to know whose class/instance is this method.
We doing it as putting the class/instance along with its method, separated by a period.
>>> greeting.say() Hello!As you may already guess when you reverse a list the first way ( the working one ) you just call its method 'reverse'.
So it becomes:
>>> first_list.reverse() # calling the method 'reverse' of the instance of a class list, called 'first_list' >>> first_list [6, 5, 4, 3, 2, 1]In order to do the same using a function, you have to create such a function. But you didn't do that, so this function doesn't exist. And got an error.
However, there is a built-in function in Python called 'reversed'. Not 'reverse'.
>>> reversed(first_list) <list_reverseiterator object at 0x7f7184183dd8>It looks like it doesn't work too. We don't get the reversed list. But it works. This function returns an iterator. It says in the output what it is. An iterator from a class list called reversed and the memory address follows. The iterators are another concept in Python.
Instead of the whole list or a sequence, they return a single element. It saves memory. For the examples we are using here it doesn't matter because the lists are small. But nowadays almost nothing is small when we are talking about data.
You have to pass the iterator to a for loop for instance or to a function which knows what to do with it.
>>> list(reversed(first_list)) [6, 5, 4, 3, 2, 1]