1. Collection Types?
It refers to a data structure that allows multiple data items to be managed as a unit. It allows multiple data items to be stored and managed in one variable. In Python, lists, tuples, sets, dictionaries, and more belong to the basic collection types.
2. List
A list is a sequential data structure that allows user to store and manage multiple values in one variable.
2-1. Creation
- Lists are created using square brackets[].
- Users can include multiple values separated by commas(,).
- Users can mix different data types in a list.
2-2. Indexing
- Each item in a list has a position (index) and can be accessed using its index.
- Indexing starts from 0.
2-3. Slicing
- Only a portion of the list can be extracted.
- Slicing is expressed using a colon :, specifying the start and end indexes.
- The end index can be omitted, in which case it extracts to the end of the list.
2-4. Mutability
- List items can be modified.
- Users can modify, add, or delete items in a list.
2-5. Concatenation
- Lists can be concatenated using the + operator.
- Lists can be repeated using the * operator.
2-6. Functions and Methods
- There are many functions and methods for working with lists.
- len(): Returns the number of items in a list.
- list.append(): Adds a new item to the end of a list.
- list.extend(): Adds all items of another list or iterable object to the end of a list.
- list.pop(): Removes and returns the item at the specified index. If no index is specified, the last item is removed.
- list.insert(): Inserts an item at the specified index.
- list.index(): Returns the first index of a specific item in a list.
- list.reverse(): Reverses the order of items in a list.
- list.sort(): Sorts the items in a list in ascending order.
- list.sort(reverse=True): Sorts the items in a list in descending order.
- list.sorted(): Returns a new list with the list sorted in ascending order. The original list remains unchanged.
- list.count(): Returns the number of times a specific item appears in a list.
'Python(Eng. ver)' 카테고리의 다른 글
6. Python collection type - Set (0) | 2024.03.14 |
---|---|
5. Python collection type - Tuple (0) | 2024.03.14 |
3. Python String & why 0.1+ 1.1 ≠ 1.2 (0) | 2024.03.13 |
2. Variables in Python (0) | 2024.03.13 |
1. Python Output (0) | 2024.03.12 |