Week 2.
[Audio] Lesson Objective The concept of lists and list processing, including the iteration provided by the for loop.
[Audio] Why do we need lists? It may happen that you have to read, store, process, and finally, print dozens, maybe hundreds, perhaps even thousands of numbers. What then? Do you need to create a separate variable for each value? Will you have to spend long hours writing statements like the one above? If you don't think that this is a complicated task, then take a piece of paper and write a program that: reads five numbers, prints them in order from the smallest to the largest ( NB, this kind of processing is called sorting)..
[Audio] Let's create a variable called numbers; it's assigned with not just one number, but is filled with a list consisting of five values ( note: the list starts with an open square bracket and ends with a closed square bracket; the space between the brackets is filled with five numbers separated by commas). The elements inside a list may have different types. Some of them may be integers, others floats, and yet others may be lists..
[Audio] Indexing lists How do you change the value of a chosen element in the list? Let's assign a new value of 111 to the first element in the list as given in the diagram above. The value inside the brackets which selects one element of the list is called an index, while the operation of selecting an element from the list is known as indexing..
[Audio] ACCESSING LIST CONTENT Each of the list's elements may be accessed separately. For example, it can be printed as given above in example 1. The snippet will send 111 to the console. As you can see in the editor, the list may also be printed as a whole - just like given in example 2. The output from the example snippet above looks like as given in example 3..
[Audio] The len() function The length of a list may vary during execution. New elements may be added to the list, while others may be removed from it. This means that the list is a very dynamic entity. If you want to check the list's current length, you can use a function named len() (its name comes from length)..
[Audio] REMOVING ELEMENTS FROM A LIST Any of the list's elements may be removed at any time - this is done with an instruction named del (delete). For Practive open the link given above..
[Audio] Negative indices are legal It may look strange, but negative indices are legal, and can be very useful. An element with an index equal to -1 is the last one in the list. The first example snippet will output 1. Similarly, the element with an index equal to -2 is the one before last in the list. The second example snippet will output 2..
[Audio] Open the Lab activity given above.. Estimated time – 10 Min.
[Audio] Functions vs. methods A method is a specific kind of function - it behaves like a function and looks like a function, but differs in the way in which it acts, and in its invocation style. A function doesn't belong to any data - it gets data, it may create new data and it (generally) produces a result. A method does all these things, but is also able to change the state of a selected entity..
[Audio] The function takes an argument, does something, and returns a result. The name of the method is preceded by the name of the data which owns the method. Next, you add a dot, followed by the method name, and a pair of parenthesis enclosing the arguments. The method will behave like a function, but can do something more - it can change the internal state of the data from which it has been invoked. You may ask: why are we talking about methods, not about lists? This is an essential issue right now, as we're going to show you how to add new elements to an existing list. This can be done with methods owned by all the lists, not by functions..
[Audio] Adding elements to a list: append and insert The insert() method is a bit smarter - it can add a new element at any place in the list, not only at the end. List append method - A new element may be glued to the end of the existing list..
[Audio] Click the activity given above to practice about the activity..
[Audio] CONGRATULATIONS! YOU HAVE COMPLETED MODULE 3 Well done! You've reached the end of Module 3 and completed a major milestone in your Python programming education. Here's a short summary of the objectives you've covered and got familiar with in Module 3: the concept of lists and list processing, including the iteration provided by the for loop You are now ready to take the module quiz and attempt the final challenge: Module 3 Test, which will help you gauge what you've learned so far..