python 知识点

  1. 幂运算 **pow(x,n)
  2. abs()取绝对值,round()把浮点数四舍五人最接近的整数值
  3. math.aqrt() 计算平方根
  4. x.reverse() 将list中元素方向存放
  5. list(reversed(x)):对一个序列进行反向迭代,那么可以使用reversed函数,这个函数并不返回一个列表,而是返回迭代器(iterator),使用list将返回对象转化list
  6. x.sort()原位置排序,sorted(x)返回新列表。另外两个可选参数key,reverse。关键字参数key提供的是排序过程中使用的函数。
  7. format 方法,{} 和:来代替以前的%
    {} 相当于占位符
"{} {}".format("hello", "world")    # 不设置指定位置,按默认顺序
'hello world'
 
"{0} {1}".format("hello", "world")  # 设置指定位置
'hello world'
 
"{1} {0} {1}".format("hello", "world")  # 设置指定位置
'world hello world'

数字格式化:

print('{:.2f}'.format(3.1415926))
  1. 转换为元祖 tuple(list)
  2. 合并元祖 +
  3. 列表合并 (+或extend)
  4. zip压缩 :将多个序列的对应位置的元素组成元祖
  5. zip(*zip_lst) 解压缩
  6. 合并字典 update()
dict1 = {'a': 1, 2: 'b', '3': [1, 2, 3]}
dict2 = {4: 'new1', 5: 'news'}
dict1.update(dict2)
print (dict1)
  1. set 并、交、差、异或,判断子集
a | b
a & b
a - b
a ^ b
a.issubset(b)
  1. 字符串格式化 %:
print ('%d %s cost me $%.2f.' %(2, 'books', 21.227))  #%d整型
  1. sorted(d.items(),key = lambda x:x[1],reverse = True)

你可能感兴趣的:(python)