update python notes
This commit is contained in:
parent
5d37a1efb2
commit
2a1762083b
2 changed files with 6 additions and 8 deletions
|
|
@ -2,10 +2,6 @@
|
||||||
tags: [python, data-structures]
|
tags: [python, data-structures]
|
||||||
---
|
---
|
||||||
|
|
||||||
# Tuples in Python
|
|
||||||
|
|
||||||
TODO: Exapand tuple notes - give more use cases
|
|
||||||
|
|
||||||
Tuples are one of the main data-structures or containers in Python. Tuples are
|
Tuples are one of the main data-structures or containers in Python. Tuples are
|
||||||
useful in cases where you want to group related data and ensure that it will not
|
useful in cases where you want to group related data and ensure that it will not
|
||||||
change.
|
change.
|
||||||
|
|
@ -31,9 +27,13 @@ tup1 = (1, 3, 5, 7)
|
||||||
print(tup1[2])
|
print(tup1[2])
|
||||||
|
|
||||||
# 5
|
# 5
|
||||||
"""
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> Really you should know in advance how long your tuple is going to be but if
|
||||||
|
> you just do `tup = ('shoe')` this will be processed by Python as a single
|
||||||
|
> string. Instead if you want to indicate that the tuple may be expanded later
|
||||||
|
> use `tup = ('shoe',)`. The comma converts it from a string to a tuple.
|
||||||
|
|
||||||
## Slicing
|
## Slicing
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ tags:
|
||||||
- data-types
|
- data-types
|
||||||
---
|
---
|
||||||
|
|
||||||
# Type hinting in Python
|
|
||||||
|
|
||||||
With type hinting we can add type information to variables, functions, and
|
With type hinting we can add type information to variables, functions, and
|
||||||
classes. This is not enforced by the Python interpreter but can be used by
|
classes. This is not enforced by the Python interpreter but can be used by
|
||||||
external tools like `mypy` to check the code.
|
external tools like `mypy` to check the code.
|
||||||
|
|
@ -109,7 +107,7 @@ def find_index(numbers: list[int], target: int) -> Optional[int]:
|
||||||
|
|
||||||
The function above returns an `int` or `None`.
|
The function above returns an `int` or `None`.
|
||||||
|
|
||||||
Post 3.10, we don't need to use `Optional`, we can use a union to cover the
|
From 3.11, we don't need to use `Optional`, we can use a union to cover the
|
||||||
`None` case. Refactoring the previous example:
|
`None` case. Refactoring the previous example:
|
||||||
|
|
||||||
```py
|
```py
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue