一、数字类型
num = -1000000000000000000000000000000000000000000000000
print(num, type(num))
num = 3.14
print(num, type(num))
res = True
print(res, type(res), isinstance(res, int))
print(3.14 + True)
num = complex(5, 4) # 5 + 4j
print(num + (4 + 5j))
a = 10
b = 3.74
c = True
print(int(a), int(b), int(c))
print(float(a), float(b), float(c))
print(bool(a), bool(b), bool(c))
二、字符串类型
# 需求:你是"好学生"
s1 = "你是\"好学生\""
print(s1)
# 可以通过引号的嵌套,使内部不同的引号在不转义的情况下直接输出
s2 = ‘你是"好学生"‘
print(s2)
# 需求:你是"好学生",是‘我的‘
s3 = """你是"好学生",是‘我的‘"""
print(s3)
```
(1)、字符串的常规操作
# 正向取值从0编号,反向取值从-1编号
s1 = ‘123abc呵呵‘
print(id(s1)) # 2217572683576
print(s1[0], id(s1[0])) # 2217543167200
t_s = ‘1‘
print(id(t_s)) # 2217543167200
# 取出c
print(s1[5], s1[-3])
s2 = ‘你好‘
s22 = ‘帅‘
ss2 = s2 + s22
print(ss2, id(s2), id(s22), id(ss2))
# 如果要拼接其他类型
a = 10
b = "20"
c = True
# res = "1020True"
res = "%s%s%s" % (a, b, c) # 方法1
print(res)
res = str(a) + b + str(c) # 方法2
print(res)
s3 = ‘12345‘
ln1 = s3.__len__()
print(ln1)
ln2 = len(s3)
print(ln2)
s4 = ‘123abc呵呵‘
sub_s = s4[0:6:]
print(sub_s) # 123abc
sub_s = s4[0:6:2]
print(sub_s) # 13b
sub_s = s4[::-1]
print(sub_s) # 呵呵cba321
sub_s = s4[-1:-6:-1]
print(sub_s) # 呵呵cba
s5 = ‘123abc呵呵‘
ss5 = ‘12a‘
print(ss5 in s5) # False
print(ss5 not in s5) # True
s6 = ‘123abc呵呵‘
for v in s5:
print(v)
(2)、python字符串重要方法
```python
s1 = ‘123abc呵呵‘
print(s1.index(‘b‘))
s2 = ‘***好 * 的 ***‘
print(s2.strip(‘*‘))
s3 = ‘12312312‘
print(s3.count(‘123‘))
s4 = ‘123‘
print(s4.isdigit())
s5 = "AbC def"
print(s5.upper()) # 全大写
print(s5.lower()) # 全小写
# 了解
print(s5.capitalize()) # 首字母大写
print(s5.title()) # 每个单词首字母大写
s6 = ‘https://www.baidu.com‘
r1 = s6.startswith(‘https:‘)
r2 = s6.startswith(‘http:‘)
r3 = s6.endswith(‘com‘)
r4 = s6.endswith(‘cn‘)
if (r1 or r2) and (r3 or r4):
print(‘合法的链接‘)
else:
print(‘非合法的链接‘)
s7 = ‘egon say: he is da shuai b,egon!egon!egon!‘
new_s7 = s7.replace(‘egon‘, ‘Liu某‘) # 默认替换所有
print(new_s7)
new_s7 = s7.replace(‘egon‘, ‘Liu某‘, 1) # 替换一次
print(new_s7)
s8 = ‘name:{},age:{}‘
print(s8.format(‘Owen‘, 18)) # 默认按位置
print(‘name:{1},age:{1}, height:{1}‘.format(‘Owen‘, 18)) # 标注位置,一个值可以多次利用
print(‘name:{n},age:{a}, height:{a}‘.format(a=18, n="Zero")) # 指名道姓
```
(3)字符串不常用操作
三、列表类型
```python
# 定义:
# 1.list中可以存放多个值,可以存放所有类型的数据
# 2.list中有序的,可以通过索引取值
(1)常规操作
```python
s1 = [1, 3, 2]
print(s1[0])
print(s1[-1])
s2 = [1, 2, 3]
print(s2 + s2)
print(s2 * 2)
print(s2)
s3 = [3, 4, 1, 2, 5]
print(len(s3))
s4 = [3, 4, 1, 2, 5]
new_s4 = s4[::-1]
print(new_s4)
new_s4 = s4[1:4:]
print(new_s4)
new_s4 = s4[-2:-5:-1]
print(new_s4)
s5 = [3, 4, ‘1‘, 2, 5]
print(‘1‘ in s5)
print(1 in s5)
print(5 not in s5)
for v in s5:
print(v, type(v))
# 只打印数字类型的数据
for v in s5:
if isinstance(v, int):
print(v, end=‘ ‘)
```
(2)增删改查
```python
ls = [1, 2, 3]
# 查
print(ls)
print(ls[1])
# 增
ls.append(0) # 末尾增
print(ls)
ls.insert(1, 666) # 任意index前增
print(ls)
ls.insert(len(ls), 888) # insert实行末尾增
print(ls)
# 改
ls[1] = 66666
print(ls)
# 删
ls.remove(888)
print(ls)
res = ls.pop() # 默认从末尾删,并返还删除的值
print(res)
res = ls.pop(1) # 从指定索引删除,并返还删除的值
print(res, ls)
# 了了解
del ls[2]
print(res, ls)
# 清空
ls.clear()
print(ls)
```
(2)了解的方法
```python
ls = [‘3‘, ‘1‘, ‘2‘]
ls.sort() # 默认正向排序
print(ls)
ls.sort(reverse=True) # 正向排序结果上翻转,形成倒序
print(ls)
ls = [‘3‘, ‘1‘, ‘2‘]
ls.reverse() # 按存储的顺序进行翻转
print(ls)
ls = [1, 2, 1, 2, 3, 1]
print(ls.count(1)) # 对象1存在的次数
```
#### 了了解
```python
# 1)整体增加,添加到末尾
ls = [1, 2, 3]
ls.extend(‘123‘)
print(ls)
ls.extend([0, 1, 2])
print(ls)
# 2) 目标的索引位置,可以规定查找区间
ls = [1, 2, 1, 2, 3, 1]
# 找对象1,在索引3开始往后找到索引6之前
ind = ls.index(1, 3, 6)
print(ind)
原文:https://www.cnblogs.com/chuwanliu/p/10726665.html