Autosave: 2023-02-15 07:36:48
This commit is contained in:
parent
08764b0c3a
commit
4b34232081
5 changed files with 241 additions and 29 deletions
|
@ -15,3 +15,145 @@ Lists have the following properties:
|
||||||
- They **allow duplicate** members
|
- They **allow duplicate** members
|
||||||
- They are **indexed**
|
- They are **indexed**
|
||||||
- You can increase/decrease their length by adding/removing new members
|
- You can increase/decrease their length by adding/removing new members
|
||||||
|
|
||||||
|
> Lists are denoted with `[...]`
|
||||||
|
|
||||||
|
## Basic usage
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Defining a list
|
||||||
|
list1 = ['John', 'Paul', 'George', 'Ringo']
|
||||||
|
list2 = [4]
|
||||||
|
|
||||||
|
# Empty list
|
||||||
|
list3 = [] # empty list
|
||||||
|
list3 = list() # Also empty list
|
||||||
|
|
||||||
|
# Nested list
|
||||||
|
list5 = [[2, 3], [6, 8]]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Slicing
|
||||||
|
|
||||||
|
```python
|
||||||
|
list1 = ['John', 'Paul', 'George', 'Ringo']
|
||||||
|
|
||||||
|
print(list1[1])
|
||||||
|
print(list1[-1])
|
||||||
|
print(list1[1:3])
|
||||||
|
print(list1[:3])
|
||||||
|
print(list1[1:])
|
||||||
|
|
||||||
|
"""
|
||||||
|
Ringo
|
||||||
|
['Paul', 'George']
|
||||||
|
['John', 'Paul', 'George']
|
||||||
|
['Paul', 'George', 'Ringo']
|
||||||
|
"""
|
||||||
|
```
|
||||||
|
|
||||||
|
## Adding additional values to existing list
|
||||||
|
|
||||||
|
```python
|
||||||
|
list1 = ['John', 'Paul', 'George', 'Ringo']
|
||||||
|
|
||||||
|
# Add single element to the end of a list
|
||||||
|
list1.append('Pete')
|
||||||
|
# ['John', 'Paul', 'George', 'Ringo', 'Pete']
|
||||||
|
|
||||||
|
# Add multiple elements to end of a list
|
||||||
|
list1.extend(['Albert', 'Bob'])
|
||||||
|
list1 += ['Ginger', 'Sporty']
|
||||||
|
# ['John', 'Paul', 'George', 'Ringo', 'Pete', 'Albert', 'Bob', 'Ginger', 'Sporty']
|
||||||
|
|
||||||
|
## Insert at specific index
|
||||||
|
list1.insert(2, 7)
|
||||||
|
['John', 'Paul', 7, 'George', 'Ringo', 'Pete', 'Albert', 'Bob', 'Ginger', 'Sporty']
|
||||||
|
|
||||||
|
a_list = ['Adele', 'Madonna', 'Cher']
|
||||||
|
print(a_list)
|
||||||
|
a_list.insert(1, 'Paloma')
|
||||||
|
print(a_list)
|
||||||
|
# ['Adele', 'Paloma', 'Madonna', 'Cher']
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Removing elements
|
||||||
|
|
||||||
|
We distinguish `del` from `remove` when removing elements from lists:
|
||||||
|
|
||||||
|
- `del` requires an index value
|
||||||
|
- `remove` requires a value reference (i.e. the mame of the element rather than its index)
|
||||||
|
|
||||||
|
`del` is simple deletion whereas `remove` searches the list. Therefore `del` is more efficient.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Remove and return element removed
|
||||||
|
list6 = ['Once', 'Upon', 'a', 'Time']
|
||||||
|
print(list6.pop(2))
|
||||||
|
# a
|
||||||
|
|
||||||
|
# Remove and return last element
|
||||||
|
list6 = ['Once', 'Upon', 'a', 'Time']
|
||||||
|
print(list6.pop())
|
||||||
|
list6.pop()
|
||||||
|
print(list6)
|
||||||
|
# Time
|
||||||
|
|
||||||
|
list6.remove('Upon')
|
||||||
|
print(list6)
|
||||||
|
# ['Once', 'a']
|
||||||
|
|
||||||
|
my_list = ['A', 'B', 'C', 'D', 'E']
|
||||||
|
print(my_list)
|
||||||
|
# ['A', 'B', 'C', 'D', 'E']
|
||||||
|
del my_list[2]
|
||||||
|
print(my_list)
|
||||||
|
# ['A', 'B', 'D', 'E']
|
||||||
|
|
||||||
|
|
||||||
|
print(my_list)
|
||||||
|
# ['A', 'B', 'C', 'D', 'E']
|
||||||
|
del my_list[1:3]
|
||||||
|
print(my_list)
|
||||||
|
# ['A', 'D', 'E']
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Retrieve elements by index
|
||||||
|
|
||||||
|
```python
|
||||||
|
list7 = [2, 3, 6, 8]
|
||||||
|
print(list7.index(8))
|
||||||
|
# 3
|
||||||
|
|
||||||
|
list6 = ['Once', 'Upon', 'a', 'Time']
|
||||||
|
print(list6.index('a'))
|
||||||
|
# 2
|
||||||
|
```
|
||||||
|
|
||||||
|
## Nesting lists
|
||||||
|
|
||||||
|
```python
|
||||||
|
l1 = [1, 43.5, 'Phoebe', True]
|
||||||
|
l2 = ['apple', 'orange', 31]
|
||||||
|
root_list = ['John', l1, l2, 'Denise']
|
||||||
|
print(root_list)
|
||||||
|
# ['John', [1, 43.5, 'Phoebe', True], ['apple', 'orange', 31], 'Denise']
|
||||||
|
```
|
||||||
|
|
||||||
|
## List comprehension
|
||||||
|
|
||||||
|
> List comprehension is an older feature of Python. Now the same functionality can be achieved with greater concision using functional methods like `map` and `filter`. But you may see it used in older code.
|
||||||
|
|
||||||
|
```python
|
||||||
|
values = [1, 2, 4, 6, 8, 9]
|
||||||
|
|
||||||
|
new_values = [i + 1 for i in values]
|
||||||
|
print('new_values', new_values)
|
||||||
|
# new_values [2, 3, 5, 7, 9, 10]
|
||||||
|
new_list = [item + 1 for item in values if item % 2 == 0]
|
||||||
|
|
||||||
|
print('new_list:', new_list)
|
||||||
|
# new_list: [3, 5, 7, 9]
|
||||||
|
```
|
||||||
|
|
|
@ -10,3 +10,50 @@ tags: [python, data-structures]
|
||||||
- You can increase/decrease their length by adding/removing new members
|
- You can increase/decrease their length by adding/removing new members
|
||||||
- They **do not allow duplicate members**
|
- They **do not allow duplicate members**
|
||||||
- **Can only hold immutable objects**
|
- **Can only hold immutable objects**
|
||||||
|
|
||||||
|
> Sets are denoted with `{...}`
|
||||||
|
|
||||||
|
## Basic usage
|
||||||
|
|
||||||
|
```python
|
||||||
|
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
|
||||||
|
print(basket) # show that duplicates have been removed
|
||||||
|
print(len(basket))
|
||||||
|
# {'apple', 'pear', 'banana', 'orange'}
|
||||||
|
# 4
|
||||||
|
```
|
||||||
|
|
||||||
|
## Looping through sets
|
||||||
|
|
||||||
|
```python
|
||||||
|
for item in basket:
|
||||||
|
print(item)
|
||||||
|
|
||||||
|
"""
|
||||||
|
apple
|
||||||
|
pear
|
||||||
|
banana
|
||||||
|
orange
|
||||||
|
"""
|
||||||
|
```
|
||||||
|
|
||||||
|
## Check for membership
|
||||||
|
|
||||||
|
```python
|
||||||
|
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
|
||||||
|
print('apple' in basket)
|
||||||
|
# True
|
||||||
|
```
|
||||||
|
|
||||||
|
## Remove items from set
|
||||||
|
|
||||||
|
> `remove` will raise an error if the specified item does not exist, `discard` will not
|
||||||
|
|
||||||
|
```python
|
||||||
|
basket.remove('apple')
|
||||||
|
basket.discard('apricot')
|
||||||
|
print(basket)
|
||||||
|
# {'pear', 'banana', 'orange'}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Add items to a set
|
||||||
|
|
|
@ -16,43 +16,48 @@ Tuples have the following properties:
|
||||||
- **Allow duplicate** members
|
- **Allow duplicate** members
|
||||||
- They are **indexed**
|
- They are **indexed**
|
||||||
|
|
||||||
|
As with all containers in Python they permit any data type.
|
||||||
|
|
||||||
|
> Tuples are denoted with `(...)`
|
||||||
|
|
||||||
|
## Basic usage
|
||||||
|
|
||||||
```python
|
```python
|
||||||
tup1 = (1, 3, 5, 7)
|
tup1 = (1, 3, 5, 7)
|
||||||
print('tup1[0]:\t', tup1[0])
|
print(tup1[0])
|
||||||
print('tup1[1]:\t', tup1[1])
|
print(tup1[1])
|
||||||
print('tup1[2]:\t', tup1[2])
|
print(tup1[2])
|
||||||
print('tup1[3]:\t', tup1[3])
|
print(tup1[3])
|
||||||
|
|
||||||
"""
|
"""
|
||||||
tup1[0]: 1
|
1
|
||||||
tup1[1]: 3
|
3
|
||||||
tup1[2]: 5
|
5
|
||||||
tup1[3]: 7
|
7
|
||||||
"""
|
"""
|
||||||
|
```
|
||||||
|
|
||||||
# Slicing
|
## Slicing
|
||||||
|
|
||||||
print('tup1[1:3]:\t', tup1[1:3])
|
```python
|
||||||
print('tup1[:3]:\t', tup1[:3])
|
tup1 = (1, 3, 5, 7)
|
||||||
print('tup1[1:]:\t', tup1[1:])
|
|
||||||
print('tup1[::-1]:\t', tup1[::-1])
|
print(tup1[1:3])
|
||||||
|
print(tup1[:3])
|
||||||
|
print(tup1[1:])
|
||||||
|
print(tup1[::-1])
|
||||||
|
|
||||||
"""
|
"""
|
||||||
tup1[1:3]: (3, 5)
|
(3, 5)
|
||||||
tup1[:3]: (1, 3, 5)
|
(1, 3, 5)
|
||||||
tup1[1:]: (3, 5, 7)
|
(3, 5, 7)
|
||||||
tup1[::-1]: (7, 5, 3, 1)
|
(7, 5, 3, 1)
|
||||||
"""
|
"""
|
||||||
|
```
|
||||||
|
|
||||||
|
## Looping
|
||||||
|
|
||||||
print('len(tup1):\t', len(tup1))
|
```python
|
||||||
# len(tup1): 4
|
|
||||||
|
|
||||||
tup2 = (1, 'John', True, -23.45)
|
|
||||||
print(tup2)
|
|
||||||
# (1, 'John', True, -23.45)
|
|
||||||
|
|
||||||
|
|
||||||
tup3 = ('apple', 'pear', 'orange', 'plum', 'apple')
|
tup3 = ('apple', 'pear', 'orange', 'plum', 'apple')
|
||||||
for x in tup3:
|
for x in tup3:
|
||||||
print(x)
|
print(x)
|
||||||
|
@ -64,20 +69,32 @@ orange
|
||||||
plum
|
plum
|
||||||
apple
|
apple
|
||||||
"""
|
"""
|
||||||
|
```
|
||||||
|
|
||||||
|
## Useful methods and predicates
|
||||||
|
|
||||||
|
```python
|
||||||
|
tup3 = ('apple', 'pear', 'orange', 'plum', 'apple')
|
||||||
|
|
||||||
|
# Count instances of a member
|
||||||
print(tup3.count('apple'))
|
print(tup3.count('apple'))
|
||||||
print(tup3.index('pear'))
|
|
||||||
|
|
||||||
# 2
|
# 2
|
||||||
|
|
||||||
|
# Get index of a member
|
||||||
|
print(tup3.index('pear'))
|
||||||
# 1
|
# 1
|
||||||
|
|
||||||
|
# Check for membership
|
||||||
if 'orange' in tup3:
|
if 'orange' in tup3:
|
||||||
print('orange is in the Tuple')
|
print('orange is in the Tuple')
|
||||||
|
|
||||||
# orange is in the Tuple
|
# orange is in the Tuple
|
||||||
|
```
|
||||||
|
|
||||||
|
## Nest tuples
|
||||||
|
|
||||||
|
```python
|
||||||
|
|
||||||
tuple1 = (1, 3, 5, 7)
|
|
||||||
tuple2 = ('John', 'Denise', 'Phoebe', 'Adam')
|
tuple2 = ('John', 'Denise', 'Phoebe', 'Adam')
|
||||||
tuple3 = (42, tuple1, tuple2, 5.5)
|
tuple3 = (42, tuple1, tuple2, 5.5)
|
||||||
print(tuple3)
|
print(tuple3)
|
||||||
|
@ -85,3 +102,5 @@ print(tuple3)
|
||||||
# (42, (1, 3, 5, 7), ('John', 'Denise', 'Phoebe', 'Adam'), 5.5)
|
# (42, (1, 3, 5, 7), ('John', 'Denise', 'Phoebe', 'Adam'), 5.5)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
// TODO: How to flatten a tuple?
|
||||||
|
|
|
@ -96,6 +96,10 @@ A. Sweighart. 2020. **Beyond the Basic Stuff with Python**
|
||||||
|
|
||||||
A. Sweighart. 2015. **Automate the Boring Stuff with Python**
|
A. Sweighart. 2015. **Automate the Boring Stuff with Python**
|
||||||
|
|
||||||
|
J. Hunt. 2019. **A Beginner's Guide to Python Programming**
|
||||||
|
|
||||||
|
J. Hunt. 2019. **An Advanced Guide to Python Programming**
|
||||||
|
|
||||||
[Tiny Python Projects (O'Reilly)](https://learning.oreilly.com/library/view/tiny-python-projects/9781617297519/)
|
[Tiny Python Projects (O'Reilly)](https://learning.oreilly.com/library/view/tiny-python-projects/9781617297519/)
|
||||||
|
|
||||||
[Learning Arduino with Python](https://realpython.com/arduino-python/)
|
[Learning Arduino with Python](https://realpython.com/arduino-python/)
|
||||||
|
|
Loading…
Add table
Reference in a new issue