-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics-0-data-structures.py
More file actions
executable file
·88 lines (69 loc) · 2.02 KB
/
basics-0-data-structures.py
File metadata and controls
executable file
·88 lines (69 loc) · 2.02 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
## int, float, string, list, dict
a,b,c,d,e = 17,17.5,"doesn't",[1,2,3],{"key":'value'}
print(a,b,c,d,e)
print("is 'a' an int?", isinstance(a,int))
print("is 'a' a float?", isinstance(a,float))
print("is 'a' a str?", isinstance(a,str))
print("is 'c' a str?", isinstance(c,str))
print("is 'd' a list?", isinstance(d,list))
print("is 'd' a dict?", isinstance(d,dict))
## class
class Person(object):
NAME = 'Person'
ATTRIBUTES = ['gender', 'age', 'weight', 'height']
def __init__(self, name):
self.name = name
def __call__(self):
return self.name
def __lt__(self, other):
return self.name < other.name
def __le__(self, other):
return self.name <= other.name
def __eq__(self, other):
return self.name == other.name
def __ne__(self, other):
return self.name != other.name
def __gt__(self, other):
return self.name > other.name
def __ge__(self, other):
return self.name >= other.name
def __str__(self):
return '<Person(name={})>'.format(self.name)
def __enter__(self):
print('enter object {}'.format(Person.NAME))
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print('exit object {} {}'.format(Person.NAME, self.name))
del self.name
# you may define some behavior when quitting
def __del__(self):
print('delete object {}'.format(Person.NAME))
# more content in this function can easily raise exceptions
p1 = Person('Doris')
p2 = Person('Mike')
print(p1,p2, p1>p2)
print(p1.NAME, p1.NAME, Person.NAME)
with Person("David") as p:
print(p, p.name)
## list and iterables:
l = list(range(1,11))
print(l)
ll = []
for i,v in enumerate(l):
ll.append((i,v))
print(l[3:-1])
print(ll)
l.sort(reverse=True)
print(l)
ll.sort(key=lambda x:x[1], reverse=True)
print(ll)
llr = sorted(ll, key=lambda x: x[1], reverse=True)
print(llr)
llrc = llr.copy()
e = (5,6)
print(llr.index(e))
llr.remove(e)
print(llr)
print(llrc)