-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch02.py
More file actions
125 lines (112 loc) · 3.42 KB
/
ch02.py
File metadata and controls
125 lines (112 loc) · 3.42 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 基本数据类型
u = None # None空值
w = True # bool布尔值
x = 123 # int整数 (50000的另一种表达方式:50_000)
y = 12.3 # float浮点数
s1 = 'string' # str字符串
s2 = b'binary' # str二进制字符串
s3 = r'saw string' # str原始字符串
# 进制
a = 0b1111 # 二进制
b = 0o17 # 八进制
c = 0xf # 十六进制
print(a, type(a)) # 结果:15, <class 'int'>
print(b, type(b)) # 结果:15, <class 'int'>
print(c, type(c)) # 结果:15, <class 'int'>
# 浮点数
d = 1.2e-3
e = .4
print(d, type(d)) # 结果:0.0012, <class 'float'>
print(e, type(e)) # 结果:0.4, <class 'float'>
# 基本数据类型的计算
f = 'abc中国'
print(1 / 3) # 结果:0.3333333333333333
print(1 // 3) # 结果:0
print(f * 5) # 结果:abc中国abc中国abc中国abc中国abc中国
# 字符串的index, iterable和转义
g = 'test"中文"'
h = "test\"中文\"" # 用\放在引号前面,可以把引号当成是字符串的一部分
print(g, type(g)) # 结果:test"中文" <class 'str'>
print(h, type(h)) # 结果:test"中文" <class 'str'>
print(g[-4]) # 某个index值是什么字符? "
print(h.index('e')) # 某个字符是什么index值? 1
print(g.count('t')) # 结果:2
s1 = 'hello\n'
s2 = b'world\n'
s3 = r'raw string\n'
print(s1, type(s1)) # 执行结果:hello 这儿有换行 <class 'str'>
print(s2, type(s2)) # 执行结果:b'world\n' <class 'bytes'>
print(s3, type(s3)) # 执行结果:raw string\n <class 'str'>
for item in s1:
print(item, type(item)) # 执行结果:h <class 'str'>
for item in s2:
print(item, type(item)) # 执行结果:119 <class 'int'>
for item in s3:
print(item, type(item)) # 执行结果:r <class 'str'>
# 基本数据类型转换
i = str(0b1111)
print(i, type(i)) # 执行结果:15 <class 'str'>
j = '732'
k = int(j)
v = str(k)
print(k, type(k)) # 732 <class 'int'>
print(v, type(v)) # 732 <class 'str'>
m = 'abc中国'
n = len(m) # 执行结果:5
print(m.upper(), n, type(n)) # ABC中国 5 <class 'int'>
# 是数值还是字符? None or bool?
o = 'abc{}xyz, efg{}abc{}efg'.format(24, 48, '25')
print(o.upper(), type(o)) # ABC24XYZ, EFG48ABC25EFG <class 'str'>
p = 23
q = '45'
r = f"abc{p}def,xyz{q}abc"
print(r.upper(), type(r)) # ABC23DEF,XYZ45ABC <class 'str'>
s = None
t = s is not None
print(s, type(s)) # None <class 'NoneType'>
print(t, type(t)) # False <class 'bool'>
# ***** 字符串特性测试 *****
# >>> print("c:\windows\name")
# c:\windows
# ame
# >>> print(r"c:\windows\name")
# c:\windows\name
# >>> print("""
# ... How are you?
# ... I'm fine.
# ... """)
#
# How are you?
# I'm fine.
#
# >>> print("""\
# ... How are you?
# ... I'm fine.
# ... """)
# How are you?
# I'm fine.
#
# >>> 'py''thon'
# 'python'
# >>> text = ('Are you ready? Yes,'
# ... ' I am ready.')
# >>> text
# 'Are you ready? Yes, I am ready.'
# >>> word = 'Python'
# >>> word[:2]
# 'Py'
# >>> word[2:]
# 'thon'
# >>> word[:]
# 'Python'
# >>> word[4:100]
# 'on'
# >>> word[100:]
# ''
# >>> step = 'abcdefghijk'
# >>> step[2 : 8 : 2]
# 'ceg'
# >>> word[0] = 'a'
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: 'str' object does not support item assignment