python基础1

注:部分内容摘自:http://www.cnblogs.com/wupeiqi/articles/4911365.html。

一、整数
如:12, 45, 23
1.def bit_length(self):
    """ 返回表示该数字的时占用的最少位数 """
    >>>(12).bit-length()
    4
    >>>bin(12)
    '0b1100'

2.def __abs__(self):
        """ 返回绝对值 """
        """ x.__abs__() <==> abs(x) """
        pass
3.def __add__(self, y):
        """ x.__add__(y) <==> x+y """
        pass
4.def __and__(self, y):
        """ x.__and__(y) <==> x&y """
        pass   #在二进制位取与运算
5.def __coerce__(self, y):
        """ 强制生成一个元组 """
        """ x.__coerce__(y) <==> coerce(x, y) """
        pass
6.def __divmod__(self, y):
        """ 相除,得到商和余数组成的元组 """
        """ x.__divmod__(y) <==> divmod(x, y) """
        pass
7.def __div__(self, y):
        """ x.__div__(y) <==> x/y """
        pass
8.def __float__(self):
        """ 转换为浮点类型 """
        """ x.__float__() <==> float(x) """
        pass
9. def __int__(self):
        """ 转换为整数 """
        """ x.__int__() <==> int(x) """
        pass
10. def __long__(self):
        """ 转换为长整数 """
        """ x.__long__() <==> long(x) """
        pass
二:长整型
1.def bit_length(self): # real signature unknown; restored from __doc__
        """
        long.bit_length() -> int or long
        
        Number of bits necessary to represent self in binary.
        >>> bin(37L)
        '0b100101'
        >>> (37L).bit_length()
        6
三:浮点型
四:字符串
s = 'spam'
1.find   #查到的话,返回字符的偏移量,没有查到的话,返回-1
  >>>s.find(p)
  1  
2.replace
  >>>s.replace('pa','XXX')
  'sXXXm'
3.分割
  >>>line = 'aa,bb,cc,dd'
     lin.split(,)  #括号内指定分隔符,为空则为空格。
  ['aa','bb','cc','dd']    
4. def translate(self, table, deletechars=None):  
        """
        转换,需要先做一个对应表,最后一个表示删除字符集合
        intab = "aeiou"
        outtab = "12345"
        trantab = maketrans(intab, outtab)
        str = "this is string example....wow!!!"
        print str.translate(trantab, 'xm')
        """
5.def center(self, width, fillchar=None):  
        """ 内容居中,width:总长度;fillchar:空白处填充内容,默认无 """
        """
        S.center(width[, fillchar]) -> string
        
        Return S centered in a string of length width. Padding is
        done using the specified fill character (default is a space)
        """
        return ""
6.def index(self, sub, start=None, end=None):  
        """ 子序列位置,如果没找到,报错 """
        S.index(sub [,start [,end]]) -> int
        
        Like S.find() but raise ValueError when the substring is not found.
        """
        return 0
7.def isalnum(self):  
        """ 是否是字母和数字 """
        """
        S.isalnum() -> bool
        
        Return True if all characters in S are alphanumeric
        and there is at least one character in S, False otherwise.
        """
        return False
8.def isalpha(self):  
        """ 是否是字母 """
9.def isdigit(self):  
        """ 是否是数字 """
10.def islower(self):  
        """ 是否小写 """
11. def lstrip(self, chars=None):  
        """ 移除左侧空白 """
        """
12.def swapcase(self):  
        """ 大写变小写,小写变大写 """
        """
    
需求帮助:
>>>dir(line)
>>>help
五:列表
1. 追加
def append(self, p_object):
        """ L.append(object) -- append object to end """
        pass
2.计数
 def count(self, value):
        """ L.count(value) -> integer -- return number of occurrences of value """
        return 0
3.扩展
def extend(self, iterable):
        """ L.extend(iterable) -- extend list by appending elements from the iterable """
        pass
4.删除 pop()中间没有值的话删除最后一个并返回值
def pop(self, index=None):
        """
        L.pop([index]) -> item -- remove and return item at index (default last).
        Raises IndexError if list is empty or index is out of range.
        """
        pass
5.删除指定元素
def remove(self, value):
        """
        L.remove(value) -- remove first occurrence of value.
        Raises ValueError if the value is not present.
        """
        pass        
6.反转
def reverse(self):
        """ L.reverse() -- reverse *IN PLACE* """
        pass
7.排序
def sort(self, cmp=None, key=None, reverse=False):
        """
        L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
        cmp(x, y) -> -1, 0, 1
        """
        pass
8.插入
 def insert(self, index, p_object):
        """ L.insert(index, object) -- insert object before index """
        pass        
9.def __add__(self, y):
        """ x.__add__(y) <==> x+y """
        pass
10.包含
def __contains__(self, y):
        """ x.__contains__(y) <==> y in x """
        pass
11.判断长度
def __len__(self):
        """ x.__len__() <==> len(x) """
        pass
六:字典
1.def clear(self):
        """ 清除内容 """
        """ D.clear() -> None.  Remove all items from D. """
        pass
2.def copy(self):
        """ 浅拷贝 """
        """ D.copy() -> a shallow copy of D """
        pass
3.def get(self, k, d=None):
        """ 根据key获取值,d是默认值 """
        """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """
        pass        
4.def has_key(self, k):
        """ 是否有key """
        """ D.has_key(k) -> True if D has a key k, else False """
        return False        
5.def keys(self):
        """ 所有的key列表 """
        """ D.keys() -> list of D's keys """
        return []        
6.def pop(self, k, d=None):
        """ 获取并在字典中移除 """
        """
        D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
        If key is not found, d is returned if given, otherwise KeyError is raised
        """
7.def popitem(self):
        """ 获取并在字典中移除 """
        """
        D.popitem() -> (k, v), remove and return some (key, value) pair as a
        2-tuple; but raise KeyError if D is empty.
        """
8.def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
        """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """
        """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
        pass
9.def update(self, E=None, **F): # known special case of dict.update
        """ 更新
            {'name':'alex', 'age': 18000}
            [('name','sbsbsb'),]
        """
        """
        D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
        If E present and has a .keys() method, does:     for k in E: D[k] = E[k]
        If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v
        In either case, this is followed by: for k in F: D[k] = F[k]
        """
        pass        
       

posted @ 2016-03-23 17:46  张瑞东  阅读(244)  评论(0编辑  收藏  举报