天天看点

python笔记二 基础

10/24

对于Python,一切事物都是对象,对象基于类创建,对象所有的功能都是去类里面找的

变量名 = 对象 (值)                              重复的功能 创建一个类,然后对象去引用  

整数 

age = 18

print(type(age))

<class 'int'>

如果是其他的类型,会在下面显示类地址。

age.__abs__()

all_item = 95

pager = 10

result = all_item.__divmod__(10)

print(result)

(9, 5)

5/6  5//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: append  clear  copy count  extend 合并  index  insert  pop(下标) remove(值) reverse反转  sort 

元祖tuple   

字典dict   : keys  values  items

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([11,22,33])

s2 = set([22,44])

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()

import  queue

d = queue.Queue()

d.put('123')               d.get()

浅拷贝  深拷贝 赋值

copy.copy

copy.deepcopy

本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/1886269,如需转载请自行联系原作者