jydg 发表于 2018-8-16 06:42:08

python笔记二 基础

  10/24
  对于Python,一切事物都是对象,对象基于类创建,对象所有的功能都是去类里面找的
  变量名 = 对象 (值)                              重复的功能 创建一个类,然后对象去引用
http://blog.51cto.com/e/u261/themes/default/images/spacer.gifhttp://blog.51cto.com/e/u261/themes/default/images/spacer.gif
  
  整数
  age = 18
  print(type(age))
  <class 'int'>
  如果是其他的类型,会在下面显示类地址。
  age.__abs__()
  all_item = 95
  pager = 10
  result = all_item.__divmod__(10)
  print(result)
  (9, 5)
  5/65//6
  age.__add__(7)   age.__rdivmod__(7)         abs 绝对值
  浮点型float
  as_integer_ratio
  10/25
  字符串
  name = 'eric'
  print(dir(name))## 这个str所有的成员
  ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
  contains 包含   print(name.__contains__('er'))   True
  capitalize首字母大写   casefold小写首字母
  center    居中
  print(name.center(40,'*'))               count(self, sub, start=None, end=None)
  ******************eric******************
  count()   字数
  name = '何全'
  print(name.encode('gbk'))
  b'\xba\xce\xc8\xab'
  expantabs    #一个type 等于8个空格
  name = 'h\tlex'
  print(name.expandtabs())
  find和 index   find找不到返回-1, index找不到报错
  name = "he {0} is {1}"
  print(name.format('a','b'))
  he a is b
  join拼接
  h = ['h','e','q','u','a','n']
  print(''.join(h))
  hequan
  partition分割
  replace('a','b',1)替换1个
  split 指定字符,分割字符    rsplit从右开始
  splitlines()   换行符   =split('\n')
  swapcase大变小,小写变大写
  10/26
  list: appendclearcopy countextend 合并indexinsertpop(下标) remove(值) reverse反转sort
  元祖tuple
  字典dict   : keysvaluesitems
  for k,v in dic.items()
  print(k,v)
  10/31
  set   无需不重复的元素集合
  交集: update_set = old.intersection(new)
  差集: delete_set= old.symmetric_difference(update_set)
  add_set = new.symmetric_difference(update_set)
  s1 = set()
  s2 = set()
  ret0 = s1.intersection(s2)
  ret1 = s1.difference(s2)
  ret2 = s1.symmetric_difference(s2)
  print(ret0)
  print(ret1)
  print(ret2)
  {22}
  {33, 11}
  {33, 11, 44}
  collections系列
  import collections         计数器,多少次重复
  obj = collections.Counter('sdafsdafsdafsdafsdaf')
  print(obj)
  Counter({'f': 5, 'd': 5, 'a': 5, 's': 5})
  orderedDict有序字典
  dic = collections.OrderedDict()
  dic['k1'] = ['v1']
  dic['k2'] = ['v2']
  print(dic)
  OrderedDict([('k1', ['v1']), ('k2', ['v2'])])
  defaultDict默认字典
  dic = collections.defaultdict(list)
  dic['k1'].append('alex')
  namedtuple 可命名元祖
  mytuple = collections.namedtuple('mytuple',['x','y','z'])
  obj = mytuple(11,22,33)
  print(obj.x)
  11
  队列双向队列      单项队列
  双进双出      单进单出
  deque   queue
  d = collections.deque()
  importqueue
  d = queue.Queue()
  d.put('123')               d.get()
  浅拷贝深拷贝 赋值
  copy.copy
  copy.deepcopy
页: [1]
查看完整版本: python笔记二 基础