-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic_datatype.py
67 lines (53 loc) · 1.23 KB
/
basic_datatype.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
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
__author__ = 'jszheng'
"""
class myint:
def __init__(self): pass
def each:
for i in data
yield i
this is a test file
on iterator
"""
# 产生器, yield! 保留当前状态, 下次call时候继续
# 节省空间,lazy eval
def gen_square(N):
for m in range(N):
yield m ** 2
if __name__ == '__main__':
# 构造一个list, 简单的for
alist = [x ** 2 for x in range(10)]
print(alist)
# 多个循环嵌套
b_list = [x + y
for x in [0, 1, 2]
for y in [100, 200, 300]]
print(b_list)
# 循环加上过滤器
c_list = [x for x in range(10) if x % 2 == 0]
print(c_list)
# 测试gen
for i in gen_square(5):
print(i, end=' : ')
print('')
# gen转为list
g = list(gen_square(6))
print(g)
# zip构造dict
keys = ['a', 'b', 'c']
vals = [3, 5, 8]
z = dict(zip(keys, vals))
print(z)
# 数字转整数字符串
print(bin(10))
print(type(bin(10)))
print(oct(10))
print(hex(255))
# 字串转整数
print(int('0b1010', 2))
print(int('0o12', 8))
print(int('0xff', 16))
print(float('1e-003'))
s = {'s', 'p', 'a', 'm'}
print(type(s))
s.add('slot')
print(s)