- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
Recently, a Python instructor asked me whether a set is mutable or immutable. I can't clearly remember what I said, but I did get the last part of the words correct because I remember saying '-utable', which reminded me of the of the backbenchers in a class answering that the pressure 'creases' instead of a comprehensive 'increases' or 'decreases'. This led me to researching the built-in data structures in Python, and below is the first part of a summary of my findings.
Lists
fruits = ["banana", "mango", "watermelon"]
Lists are used to store multiple items in a single variable. They can be defined using square brackets [ ] or the list type function
Key Features of Lists
It is important to note that lists store references rather than values. Elements in a list are not stored directly in the list structure; rather, the list stores pointers to the actual objects in memory.
Common List Operations
Lists
fruits = ["banana", "mango", "watermelon"]
Lists are used to store multiple items in a single variable. They can be defined using square brackets [ ] or the list type function
Key Features of Lists
- Ordered - maintain the order of elements.
- Mutable - items can be modified after creation.
- Heterogeneous - elements can be of different data types.
- Allow duplicates - can contain duplicate values.
- Indexed - accessing items can be done directly using their index, starting from 0.
It is important to note that lists store references rather than values. Elements in a list are not stored directly in the list structure; rather, the list stores pointers to the actual objects in memory.
Common List Operations
| Method | Description |
|---|---|
| count(x) | Returns the number of times x appears in the list |
| append(x) | Adds x to the end of the list |
| insert(i,x) | Inserts x at index i |
| remove(x) | Remove the first item with the value x |
| pop() |