-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.py
38 lines (21 loc) · 840 Bytes
/
02.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from typing import Any, TypedDict, NotRequired, Literal, Optional
def longer_than(min_len: int, items: list[str]) -> list[str]:
return [i for i in items if len(i) > min_len]
longer_than(5, ['a', 'b', 'abcdefg', 'hello world'])
longer_than(5, ['a', 'b', 'abcdefg'])
items: list[str | int | bool] = [10, 20, 'abc', False, 30]
#######################
def sum_square_values(d: dict[Any, int]) -> int:
return sum([x * x for x in d.values()])
print(sum_square_values({'a': 10, 'b': 20}))
print(sum_square_values({'a': 10, False: 20}))
#################
class Point(TypedDict):
x: int
y: int
color: NotRequired[Literal['red', 'blue', 'green', 'yellow', 'white']]
p: Point = {'x': 10, 'y': 20}
q: Point = {'x': 10, 'y': 20}
def distance(p: Point, q: Point):
pass
z: Point = {'x': 1, 'y': 2, 'color': 'red'}