对所学Python基础知识的一个小小总结
(1)字符类型的精确相加
from decimal import Decimal
print(Decimal('1.1')+Decimal('1.2'))#精确位数2.3
(2)对打印的内容的多种方式
name = '尘荒'
age = 18
res = '我的名字是%s,我的年龄是%s'%(name,age)
print(res)
res = '我的名字是%(name)s 我的年龄是 %(age)s'%{'name':name,'age':age}
print(res)
res= '我的名字是{0},我的年龄是{1}'.format(name,age)
print(res)
res ='我的名字是{name} 我的年龄是{age}'.format(name = name,age = age)
print(res)
print(f'我的名字是{name} 我的年龄是{age}')
print('成功的概率 %s%% ' % (97,))#97%
(3)编码格式
s='南风知我意 吹梦到西洲'
print(s.encode(encoding="GBK"))
print(s.encode(encoding="UTF-8"))
byte = s.encode(encoding="GBK")
print(byte.decode(encoding="GBK"))
byte = s.encode(encoding="UTF-8")
print(byte.decode(encoding="UTF-8"))
(4)小数位数的保留
print('%10.3f' % 3.1415926)#保留三位小数 用空格填充到10位
print('%10d' % 50)
print('{0:.3f}'.format(3.1415926))
print('{0:.3}'.format(3.1415926))#不带f的话 只保留三位有效数字
(5)进制的转换
#进制转换
print('{0:b}'.format(123))#转换成二进制
print('{0:o}'.format(123))#转换成八进制
print('{0:x}'.format(123))#转换成十六进制
print('{0:,}'.format(99812939393931))#转换成千分位格式
num = 18
print(bin(num))#二进制
print(oct(num))#八进制
print(hex(num))#十六进制
(6)对齐方式
print('{0:*<10}'.format('开始执行')) # 左对齐 开始执行******
print('{0:*>10}'.format('开始执行')) # 右对齐 ******开始执行
print('{0:*^10}'.format('开始执行')) # 居中对齐 ***开始执行***
print('{x:=<10}'.format(x='开始执行'))
print('{x:=>10}'.format(x='开始执行'))
print('{x:=^10}'.format(x='开始执行'))
(7)字符串相关操作
s='hello world python'
print(s[0::1])#开头到结尾 以1为跳跃格
print(s[::-1])#反转字符串
print(len(s))#字符串长度
msg = ' egon '
res= msg.strip()#去除字符两边的空格
msg = '*****egon*****'
res=msg.strip('*')#去掉指定的两边的值
msg='**/*=-**egon**-=()**'
print(msg.strip('*/-=()'))#去掉括号中包含的所有的字符
msg='***e***gon***'
print(msg.strip('**'))#只去除两边的满足条件的符号
msg = '****egon***'
res1=msg.strip('*')#去除两边*
res2=msg.lstrip('*')#仅去除左边
res2=msg.rstrip('*')#仅去除右边
info = 'hello|world|python'
res= info.split('|')#切片 按照|对字符串进行切分
print(res)
res1 = info.split(sep='|',maxsplit=1)#指定分割的次数
print(res1)
print(info.lower())
print(info.upper())
msg = 'hello world python'
print(msg.startswith('hello'))
print(msg.endswith('python'))
lst = ['hello','world','python']
print(':'.join(lst))#hello:world:python
print(','.join('hello'))#h,e,l,l,o
print(lst)
print(lst)
msg = 'you can do it you can do it'
print(msg.replace('you','YOU',))
#find 和 rfind 一个从左找 一个从右找返回索引
#找不到的话返回-1
print(msg.find('you'))
print(msg.rfind('you'))
#index 和 rindex 一个从左找 一个从右找 返回索引
#找不到的话会报错
print(msg.index('you'))
print(msg.rindex('you'))
#寻找字符串中 相应字符的个数
print(msg.count('you'))
print('python'.center(10,'*'))#居中填充
print('python'.ljust(10,'*'))#左填充
print('python'.rjust(10,'*'))#右填充
print('python'.zfill(10))#填充零
msg='hello\tworld'
print(msg.expandtabs(2))#设置制表符的空格数为2
msg = 'hello World PyThON'
print(msg.capitalize())#只有第一个字母变大写
print(msg.swapcase())#大小写互换
print(msg.title())#每个字符开头大写
print('abc'.islower())#是否小写
print('ABC'.isupper())#是否大写
print('Hello World'.istitle())#是否标题
print('151sdf'.isalnum())#是否数字字母组成
print('djnf'.isalpha())#是否全是字母
print('115'.isnumeric())#是否全是数字
print(' '.isspace())#是否全是空格
print('age'.isidentifier())#是否是有效标识符
(8)列表操作
lst =[i for i in range(1,10)]
print(lst)
lst = ['hello','python']
lst1 =lst[:]#切片相当于浅拷贝
print(lst1)
lst.append('python')#在末尾添加
print(lst)
lst.insert(1,'python')#指定位置插入相应数据
print(lst)
lst2 =['hello','world','python']
lst.extend(lst2)#在原有集合的基础上添加多个元素
print(lst)
del lst[0]#将集合中第一个元素进行删除
print(lst)
lst.pop()#不指定索引删除 删除最后一个
print(lst)
lst.remove('hello')#删除指定的元素
print(lst)
lst.reverse()#将列表进行反转
print(lst)
lst_num=[1,5141,5,115,1515,1551]
print(lst_num)
lst_num.sort()#默认为升序
print(lst_num)
lst_num.sort(reverse=True)#为降序
print(lst_num)
(9)元组
#元组是一个不可变的列表 每一个不可变元素不可变
#可变元素可以进行添加和改变
t=('hello',[1,2],'world')
t[1][0]=100
(10)字典
names = ['yangguo','令狐冲','石破天']
ages = [18,20,24]
dic = {names.upper():ages for names,ages in zip(names,ages)}
print(dic)
dict = {'杨过':18,'令狐冲':24,'石破天':25}
res = dict.pop('杨过')#删除指定的健 返回相应的值
print(res)
keys = dict.keys()#获得所有的健
values = dict.values()#获得所有的值
intem = dict.items()#获取所有的键值对
value = dict.get('令狐冲')#dict.get()容错性更好
print(dict)
dict.update({'任盈盈':19,'阿秀':20})
print(dict)
#如果key有则不添加 返回字典中key对应的值
#如果key有则添加 并将对应的key的值进行返回
res=dict.setdefault('令狐冲',20)
print(res)
(11)集合
s={1,2,3}
print(s)
s.discard(4)
print(s)
# s.remove(4) 删除不存在值 会报错
s.update({1,2,3})
print(s)
s.pop()
print(s)
s.add(4)
print(s)
(12)集合相关的操作
friends1 = {"zero","kevin","jason","egon"}
friends2 = {"Jy","ricky","jason","egon"}
# 取交集:两者共同的好友
res=friends1 & friends2
print(res)
print(friends1.intersection(friends2))
# 取并集/合集:两者所有的好友
print(friends1 | friends2)
print(friends1.union(friends2))
#4.3 取差集:取friends1独有的好友
print(friends1 - friends2)
print(friends1.difference(friends2))
# 取friends2独有的好友
print(friends2 - friends1)
print(friends2.difference(friends1))
# 对称差集: 求两个用户独有的好友们(即去掉共有的好友)
print(friends1 ^ friends2)
print(friends1.symmetric_difference(friends2))
# 父子集:包含的关系
s1={1,2,3}
s2={1,2,4}
# 不存在包含关系,下面比较均为False
print(s1 > s2)
print(s1 < s2)
s1={1,2,3}
s2={1,2}
print(s1 > s2) # 当s1大于或等于s2时,才能说是s1包含s2
print(s1.issuperset(s2))
print(s2.issubset(s1)) # s2 < s2 =>True
补充
list = ['hello','world','python']
list1=list#两者指向的是同一个地址 一荣俱荣 一损俱损
list2 = list.copy()#浅拷贝 把原列表第一层的内存地址不加区分完全copy一份给新列表
list3 = copy.deepcopy(list)#深拷贝
print('hello',end='*')#不换行直接输出 hello*
print(10/3)#保留小数位的除法
print(10//3)#取整
print(10**3)#10的三次方
# *会将没有对应关系的值存成列表然后赋值给紧跟其后的那个变量名,此处为_
x,y,z,*_=salaries=[111,222,333,444,555]
print(x,y,z)#111 222 333
print(_)#[444, 555]