天天看点

01月19日【Python3 基础知识】

2.1 数据类型

2.2 字符串

2.3 list操作

2.1 数据类型

# Ptyhon运算符
'''
数字运算符: + - * / %
关系运算符: a == b  a>b  a<b  a!=b  >=  <=
赋值运算符: a = b  +=  -=  *=  /=
逻辑运算符: and  or  not
'''
#
# 整型(int)
a = 10
print(a)
#
# 布尔型(bool)  True(真)  Falle(假)
#
# 浮点型(float): 小数类型
a = 1.2345
print(a)
#
# 字符串(str)
string = 'abc'
print(string)           

复制

2.2 字符串

string = 'abcdefg aa bbd  '
#
# 字符串操作
#
# find: 查找内容所在字符串位置,顺序从0开始;-1表示查不到
print(string.find('d'))
# 切片:[起始:结束:步长]
print(string[0:])
print(string[0:5])
print(string[0:5:2])
# replace (需要替换字符,替换后字符,替换次数)
print(string.replace('a', 'A'))
print(string.replace('a', 'A', 2))
# split 分隔符(分隔标志,分隔次数)
print(string.split())
print(string.split(' ', 1))
# strip 去字符串两端的空白字符
print(string.strip())
# title 首字母大写
print(string.title())           

复制

2.3 list操作

# append: 添加元素
lt = [1, 2, 3, 'a', 'b', 'c']
lt.append('d')
print(lt)
# insert: 指定位置插入指定元素
lt = [1, 2, 3, 'a', 'b', 'c']
lt.insert(2, '%')
print(lt)
# pop: 按下标弹出元素
lt = [1, 2, 3, 'a', 'b', 'c']
print(lt.pop())     # 默认从最后弹起
print(lt)
lt = [1, 2, 3, 'a', 'b', 'c']
print(lt.pop(2))
print(lt)
# remove: 删除元素
lt = [1, 2, 3, 'a', 'b', 'c']
lt.remove('a')
print(lt)
# index: 查询指定元素的下标
lt = [1, 2, 3, 'a', 'b', 'c']
print(lt.index(3))
# sort 顺序排序
lt = [1, 2, 3]
lt.sort()
print(lt)
# reverse   倒序排序
lt = [1, 2, 3]
lt.reverse()
print(lt)           

复制