Legacy - Creating Lists
- 01:44
Learn how to create lists in Python.
Downloads
No associated resources to download.
Transcript
Let us now look at how to create lists in Python. The first thing to note is that lists are created in square brackets. Every object within a list is separated. Using a comma, we can create a variable name and save a list within the variable. In order to select separate items from the list. We will type the variable name and then select the desired term using index numbers in square brackets as shown on the slide. One thing to note is that the index in Python starts from zero rather than a one. For example, the list on the slide is a list of some commonly used terms and finance, the variable name is list and the variable consists of the terms. If we type list, square brackets 05 and returns us the first term which is NPV, it is similar for the rest of the terms. In order to add new terms into the list, we would use the append function and include the terms that need added into the brackets and in quotes. Similarly, to remove a term we use the remove function and include the desired term that needs removing inside the brackets and again, inside quotations. The DEL function deletes the whole list. The sort function sorts the list either in alphabetical order or ascending or descending order, depending on whether the list consists of screens, floats or integers and also depending on what our requirement is, and what is the required output that we are trying to achieve.