Skip to content

Commit e855fe0

Browse files
committed
update
1 parent 06870b4 commit e855fe0

File tree

7 files changed

+318
-0
lines changed

7 files changed

+318
-0
lines changed

bornforthis/.vuepress/sidebar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { sidebar } from "vuepress-theme-hope";
3333
export default sidebar({
3434
"/column/python60/": [
3535
"01",
36+
"02",
3637
],
3738
"/blog/crawler/": "structure",
3839
"/blog/2022/": "structure",
117 KB
Loading
32.7 KB
Loading
10.3 KB
Loading
10.8 KB
Loading
12.2 KB
Loading

bornforthis/column/python60/02.md

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
---
2+
title: Day 2:Python 四大数据类型总结
3+
icon: python
4+
time: 2022-08-11 21:19:35
5+
author: AI悦创
6+
isOriginal: true
7+
category:
8+
- Python全栈60天精通之路
9+
tag:
10+
- Python全栈60天精通之路
11+
sticky: false
12+
star: false
13+
password: false
14+
article: true
15+
timeline: true
16+
image: false
17+
navbar: true
18+
sidebarIcon: true
19+
headerDepth: 5
20+
comment: true
21+
lastUpdated: true
22+
editLink: false
23+
prev: 01.md
24+
next: 03.md
25+
backToTop: true
26+
toc: true
27+
---
28+
29+
## 基本数据类型
30+
31+
### 数值型
32+
33+
Python 中的数据皆是对象,比如被熟知的 int 整型对象、float 双精度浮点型、bool 逻辑对象,它们都是单个元素。举两个例子。
34+
35+
前缀加 `0x`,创建一个十六进制的整数:
36+
37+
```python
38+
0xa5 # 等于十进制的 165
39+
```
40+
41+
使用 `e` 创建科学计数法表示的浮点数:
42+
43+
```python
44+
1.05e3 # 1050.0
45+
```
46+
47+
### 容器型
48+
49+
可容纳多个元素的容器对象,常用的比如:list 列表对象、 tuple 元组对象、dict 字典对象、set 集合对象。Python 定义这些类型的变量,语法非常简洁。
50+
51+
举例如下。
52+
53+
使用一对中括号 `[]`,创建一个 list 型变量:
54+
55+
```python
56+
lst = [1, 3, 5] # list 变量
57+
```
58+
59+
示意图看出,右侧容器为开环的,意味着可以向容器中增加和删除元素:
60+
61+
![image-20200218111947234](./02.assets/7d3ecf00-51ff-11ea-85a8-e305846ee506.png)
62+
63+
64+
65+
使用一对括号 `()`,创建一个 tuple 型对象:
66+
67+
```python
68+
tup = (1, 3, 5) # tuple 变量
69+
```
70+
71+
示意图看出,右侧容器为闭合的,意味着一旦创建元组后,便不能再向容器中增删元素:
72+
73+
![image-20200218112031346](./02.assets/85164140-51ff-11ea-a73d-4901d8017902.png)
74+
75+
但需要注意,含单个元素的元组后面必须保留一个逗号,才被解释为元组。
76+
77+
```python
78+
tup = (1,) # 必须保留逗号
79+
```
80+
81+
否则会被认为元素本身:
82+
83+
```python
84+
In [14]: tup=(1)
85+
...: print(type(tup))
86+
<class 'int'>
87+
```
88+
89+
使用一对花括号 `{}` 另使用冒号 `:`,创建一个 dict 对象:
90+
91+
```python
92+
dic = {'a':1, 'b':3, 'c':5} # dict变量
93+
```
94+
95+
字典是一个哈希表,下面的示意图形象的表达出字典的 “形”。
96+
97+
![image-20200218112256968](./02.assets/8aae4ad0-51ff-11ea-8dce-59d407eb83a8.png)
98+
99+
100+
101+
仅使用一对花括号 `{}`,创建一个 set 对象:
102+
103+
```python
104+
s = {1, 3, 5} # 集合变量
105+
```
106+
107+
Python 的容器类型,list、dict、tuple、set 等能方便地实现强大的功能,下面给出几个案例。
108+
109+
**1. 去最求平均**
110+
111+
去掉列表中的一个最小值和一个最大值后,计算剩余元素的平均值。
112+
113+
```python
114+
def score_mean(lst):
115+
lst.sort()
116+
lst2 = lst[1:-1]
117+
return round((sum(lst2) / len(lst2)), 1)
118+
119+
120+
lst = [9.1, 9.0, 8.1, 9.7, 19, 8.2, 8.6, 9.8]
121+
score_mean(lst) # 9.1
122+
```
123+
124+
代码执行过程,动画演示:
125+
126+
![image-20200218112655335](./02.assets/36297b60-51ff-11ea-a847-a5aa0d0597f8.gif)
127+
128+
**2. 打印 99 乘法表**
129+
130+
打印出如下格式的乘法表:
131+
132+
```python
133+
1*1=1
134+
1*2=2 2*2=4
135+
1*3=3 2*3=6 3*3=9
136+
1*4=4 2*4=8 3*4=12 4*4=16
137+
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
138+
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
139+
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
140+
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
141+
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
142+
```
143+
144+
一共有 10 行,第 i 行的第 j 列等于:`j*i`,其中:
145+
146+
- i 取值范围:`1<=i<=9`
147+
- j 取值范围:`1<=j<=i`
148+
149+
根据“例子分析”的语言描述,转化为如下代码:
150+
151+
```python
152+
In [13]: for i in range(1, 10):
153+
...: for j in range(1, i+1):
154+
...: print('%d*%d=%d'%(j,i,j*i), end='\t')
155+
...: print()
156+
```
157+
158+
**3. 样本抽样**
159+
160+
使用 sample 抽样,如下例子从 100 个样本中随机抽样 10 个。
161+
162+
```python
163+
from random import randint, sample
164+
165+
lst = [randint(0, 50) for _ in range(100)]
166+
print(lst[:5]) # [38, 19, 11, 3, 6]
167+
lst_sample = sample(lst, 10)
168+
print(lst_sample) # [33, 40, 35, 49, 24, 15, 48, 29, 37, 24]
169+
```
170+
171+
### 字符串
172+
173+
注意 Python 中没有像 C++ 表示的字符类型(char),所有的字符或串都被统一为 str 对象。如单个字符 `c` 的类型也为 str。
174+
175+
str 类型会被经常使用,先列举 5 个被高频使用的方法。
176+
177+
strip 用于去除字符串前后的空格:
178+
179+
```python
180+
In [1]: ' I love python\t\n '.strip()
181+
Out[1]: 'I love python'
182+
```
183+
184+
replace 用于字符串的替换:
185+
186+
```python
187+
In [2]: 'i love python'.replace(' ', '_')
188+
Out[2]: 'i_love_python'
189+
```
190+
191+
join 用于合并字符串:
192+
193+
```python
194+
In [3]: '_'.join(['book', 'store','count'])
195+
Out[3]: 'book_store_count'
196+
```
197+
198+
title 用于单词的首字符大写:
199+
200+
```python
201+
In [4]: 'i love python'.title()
202+
Out[4]: 'I Love Python'
203+
```
204+
205+
find 用于返回匹配字符串的起始位置索引:
206+
207+
```python
208+
In [5]: 'i love python'.find('python')
209+
Out[5]: 7
210+
```
211+
212+
举个应用字符串的案例,判断 str1 是否由 str2 旋转而来。
213+
214+
字符串 stringbook 旋转后得到 bookstring,写一段代码验证 str1 是否为 str2 旋转得到。
215+
216+
转化为判断:str1 是否为 str2+str2 的子串。
217+
218+
下面函数原型中,注明了每个参数的类型、返回值的类型,增强代码的可读性和可维护性。
219+
220+
```python
221+
def is_rotation(s1: str, s2: str) -> bool:
222+
if s1 is None or s2 is None:
223+
return False
224+
if len(s1) != len(s2):
225+
return False
226+
227+
def is_substring(s1: str, s2: str) -> bool:
228+
return s1 in s2
229+
230+
return is_substring(s1, s2 + s2)
231+
```
232+
233+
测试函数 `is_rotation`
234+
235+
```python
236+
r = is_rotation('stringbook', 'bookstring')
237+
print(r) # True
238+
239+
r = is_rotation('greatman', 'maneatgr')
240+
print(r) # False
241+
```
242+
243+
代码执行过程,动画演示:
244+
245+
![img](./02.assets/33a28cf0-5200-11ea-8128-358088d96de7.gif)
246+
247+
字符串的匹配操作除了使用 str 封装的方法外,Python 的 re 正则模块功能更加强大,写法更为简便,广泛适用于爬虫、数据分析等。
248+
249+
下面这个案例实现:密码安全检查,使用正则表达式非常容易实现。
250+
251+
密码安全要求:
252+
253+
- 要求密码为 6 到 20 位;
254+
- 密码只包含英文字母和数字。
255+
256+
```python
257+
import re
258+
259+
pat = re.compile(r'\w{6,20}') # 这是错误的,因为 \w 通配符匹配的是字母,数字和下划线,题目要求不能含有下划线
260+
# 使用最稳的方法:\da-zA-Z 满足“密码只包含英文字母和数字”
261+
# \d匹配数字 0-9
262+
# a-z 匹配所有小写字符;A-Z 匹配所有大写字符
263+
pat = re.compile(r'[\da-zA-Z]{6,20}')
264+
```
265+
266+
选用最保险的 fullmatch 方法,查看是否整个字符串都匹配。
267+
268+
以下测试例子都返回 None,原因都在解释里。
269+
270+
```python
271+
pat.fullmatch('qaz12') # 返回 None,长度小于 6
272+
pat.fullmatch('qaz12wsxedcrfvtgb67890942234343434') # None 长度大于 22
273+
pat.fullmatch('qaz_231') # None 含有下划线
274+
```
275+
276+
下面这个字符串 `n0passw0Rd` 完全符合:
277+
278+
```python
279+
In [20]: pat.fullmatch('n0passw0Rd')
280+
Out[20]: <re.Match object; span=(0, 10), match='n0passw0Rd'>
281+
```
282+
283+
### 自定义类型
284+
285+
Python 使用关键字 class 定制自己的类,self 表示类实例对象本身。
286+
287+
一个自定义类内包括属性、方法,其中有些方法是自带的。
288+
289+
290+
291+
292+
293+
294+
295+
296+
297+
欢迎关注我公众号:AI悦创,有更多更好玩的等你发现!
298+
299+
::: details 公众号:AI悦创【二维码】
300+
301+
![](/gzh.jpg)
302+
303+
:::
304+
305+
::: info AI悦创·编程一对一
306+
307+
AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh
308+
309+
C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh
310+
311+
方法一:[QQ](http://wpa.qq.com/msgrd?v=3&uin=1432803776&site=qq&menu=yes)
312+
313+
方法二:微信:Jiabcdefh
314+
315+
:::
316+
317+
![](/zsxq.jpg)

0 commit comments

Comments
 (0)