2020年暑假 (2)

元组

# 不能被修改
tuple1 = (1, 2, 3, 4)
tuple1[1]
tuple1[:5]

a = (1)  # int
b = (1,)  # tuple

# 更新和删除

temp = ('a', 'b', 'c')
temp = temp[:2] + ('d',) + temp[2:]
print(temp)
print(8*(8,))  # 8组成的
del temp
# 元组和列表不一样
# 元组可以变成列表
# 列表[]  元组()

# 字符串转
a = "a , c ,f"
# 一个一个字符
print(list(a))
b = (1, 2, 3, 4)
print(list(b))
#
print(str(b))
max(b)
len(b)
numbers = [1, 2, 3]
max(numbers)
min(numbers)
chars = '1234567890'
min(chars)
# 序列的数据类型要全是数字或者字符
# 数字求和
sum(numbers)
sorted(numbers)
# 返回迭代器对象
reversed(numbers)
list(reversed(numbers))
# 枚举  会有索引
enumerate(numbers)
list(enumerate(numbers))
#  相同的参数序列 组成 元组 会有省略
a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3]
list(zip(a, b))

字符串

str = "abcab"
# 第一个字母大写
str.capitalize()
# 所有字符改为小写
str.casefold()
# 填充 ,居中
str.center(40)
str.count("a", 0, 4)

str.endswith("s")
# 不存在返回-1
str.find("a")
# 不存在产生异常
str.index("a")
# 被调用的截开
print(str.join("12345"))  # 1abcab2abcab3abcab4abcab5

# 用字符串分成元组
str.partition("c")
str.replace("a", "aa")

str.rfind("a")  # 从右边开始查

str.split("c")  # 默认用空格切 变成列表 有参数那个数被消除

str.startswith("a")

# 删除空格
str.strip()
# 删除字符
str.strip("s")
# 变换大小写
str.swapcase()
# a变成b
str.translate("a", "b")
# 位置参数和关键字参数
# 位置参数在关键字参数前
"{0} love {1} ".format("I", "u")
print("{0} love {1} ".format("I", "u"))
print("{a} love {b} ".format(a="I", b="u"))

# 转义字符会被转义
print('	a')  # 输出 a
print("\")  # 输出 
# 打印花括号  {0}
print("{{0}}".format(""))

# 四舍五入保留一位小数
print('{0:.2f}'.format(72.364, 'GB'))

# %格式化操作符
'%c' % 97  # 字符或ASCII
'%c %c' % (97, 98)

'%s' % 'love'

'%d + %d = %d' % (4, 5, 4 + 5)

# 格式化定点数
'%f' % 520.1314
'%e' % 520.1314
'%E' % 520.1314
'%g' % 520.1314

#m.你代表位数
'%5.1f' % 520.1314   #总宽度,小数宽度
'%.2e' %520.1314
# 正负号
'%+d' % 5

'%-d' % 5
原文地址:https://www.cnblogs.com/ywqtro/p/13409396.html