python27读书笔记0.2

# -*- coding:utf-8 -*-

##s.partition(d)
##Searches string s for the first occurrence of some delimiter string d.if s contains
##the delimiter,it returns a truple(pre,d,post),where pre is the part of s before
##the delimiter ,d is the delimiter itself,and post is the part of s after the delimiter.

##s=u'平林漠漠烟如织,寒山一带伤心碧。暝色入高楼,有人楼上愁。玉阶空伫立,宿鸟归飞急。何处是归程?长亭更短亭'
##
##print '------转换成GBK的显示方式---'
##print s.encode('GBK')
##print '-----原始编码----'
##print s
##
##li=s.partition(u',')
##
##print u'----显示元组----'
##
##print li
##
##print '----显示元组中的单个元素----'
##for i in li:
## print i.encode('GBK')
##
##s.replace(old,new[,max])
##Return a copy of s with all occurrences of string old replaced by string new.
##Normally,all occurrence are replaced;if you want to limit the number of replacements,
##pass that limit as the max argument.
##
##print 'banana'.replace('a','###')
##
##s.rfind(t[,start[,end]])
##like .find(),but if t occurs in s,this method returns the highest starting index.
##
##print 'banana'.find('a')
##print 'banana'.rfind('a')
##
##s.rindex(t[,start[,end]])
##Similar to s.index(),but it returns the last index in S where string t is found.
##It will raise a valueError exception if the string is not found.
##

##print 'banana'.index('a')
##print 'banana'.rindex('a')
##
##print 'banana'.index('c')
##
##print 'banana'.rindex('c')

##s.rjust(w[,fill])
##Return a copy of s right-justified in a field of width w,padded with spaces.
##if w<=len(s),the result is a copy of s.
##
##To pad values with some character than a space,pass that character as the optional second argument.

##print '123'.rjust(5)
##
##print '123'.rjust(5,'*')
##
##s.rpartition(d)
##Similar to s.partition(),except that it finds the last occurrence of the delimiter.
##s.rsplit(d[,max])
##Similar to s.split(d[,max]),except that if there are more fields than max,the split
##fields are taken from the end of the string instead of from the beginning.
##s.rstrip([c])
##Return s with all trailing characters from string c removed.The default value for c
##is a string containing all the whitespace characters.
##
##s.split(d[,max])
##Returns a list of strings [s1,s2,...] made by splitting s into pieces wherever the
##delimiter string d is found.The default is to split up s into pieces wherever clumps
##of one or more whitespace characters are found.
##The optional max argument limits the number of pieces removed from the front of s.
##The resulting list will have no more than max+1 elements.
##To use the max argument while splitting the string on clumps of whitespace,pass none
##as the first argumet.

##s =u'花非花,雾非雾。夜半来,天明去。来如春梦几多时。去似朝云无觅处。'
##
##for i in s.split(u'。'):
## print i.encode('GBK')
##print 'We are ready to use the max arguments'
##
##for i in s.split(u'。',2):
## print i.encode('GBK')

##s.splitlines([keepends])
##Splits s into lines and returns a list of the lines as strings.Discards the line
##sparators unless the optional keepends arguments is true.

##s=u'''江南好,
##风景旧曾谙。
##日出江花红胜火,
##春来江水绿如蓝。
##能不忆江南。'''
##
##print s.splitlines()
##for i in s.splitlines():
## print i.encode('GBK')

##s.startswith(t[,start[,end]])
##Predicate to test whether s starts with string t.Otherwise similar to .endswith().
##s.strip([c])
##Return s with all leading and trailing characters from string c removed.The default
##value for c is a string containing all the whitespace characters.

##s =u' 江南好, 风景旧曾谙。 日出江花红胜火, 春来江水绿如蓝。 '
##print s.strip()
##print s.strip(u',')

##s.swapcase()
##Return a copy of s with each lowercase character replaced by its uppercase equivalent,
##and vice versa.

##print 'abcDEV'.swapcase()

##s.upper()
##Return a copy of s with all lowercase characters replaced by their uppercase equivalents.

##s.zfill(w)
##Return a copy of s left-filled with '0' characters to width w.

## --- Type unicode: Strings of 32-bit character ---

##To get a Unicode string,prefix the string with u .
##All the operators and methods of str type are available with unicode values.
##Addititonally,for a Unicode value u,use this method to encode its value as a string of type str
##
##To encode a Unicode string U,use this method:
## U.encode('utf-8')
##
##To decode a regular str values S that contains a UTF-8 encoded value,use this method:
## S.decode('utf-8')

##>>>u16 = u'u0456'
##>>>u16
##>>>s = u16.encode('Utf-8')
##>>>s
##

## --- Type list:Mutable sequences ---
##There are a number of functions that can be used with lists as well:
## all():Are all the elements of an iterable true?
## any():Are any of the members of an iterable true?
## cmp():Compare two values
## enumerate():Step through indices and values of an iterable
## filter():Extract qualifying elements from an iterable
## iter():Produce an iterator over a sequence
## len():Number of elements
## list():Convert to a list
## map():Apply a function to each element of an iterable
## max():Largest element of an iterable
## min():Smallest element of an iterable
## range():Generate an arithmetic progression as a list
## reduce():Sequence reduction
## reversed():Produce a reverse iterator
## sorted():Sort a sequence
## sum():Total the elements of a sequence
## xrange():Arithmetic progression generator
## zip():Combine multiple sequences
##
##

##Methods on lists
##For any list value Li,these methods are available.
##Li.append(X):Append a new element x to the end of list Li.Does not return a value.
##Li.count(x):Return the number of elements of Li that compare equal to x.
##Li.extend(s):Append another sequence s to Li
##Li.index(x[,start[,end]]):
## If Li contains any elements that equal x,return the index of the first such element,otherwise
## raise a valueError exception.
## The option start and end arguments can be used to search only positions
## with the slice Li[start:end]
##
##Li.insert(i,x):Insert a new element x into list Li just before the ith element,
##shifting all higher-number elements to the right.No value is returned.

##Li.pop([i])
##Remove and return the element with index i from Li.The default value for i is -1,
##so if you pass no argument,the last element is removed.
##Li.remove(x)
##Remove the first element of Li that is equal to x.If there are not any such elements,
##raises ValueError.
##Li.reverse(): Reverses the elements of Li in place.Does not return a result.

##Li.sort(com[,key[,reverse]]])
##Sort list Li in place.Does not return a result.
##The reordering is guaranteed to be stable--that is,if tow elements are considered equl,
##their order after sorting will not change.

## --- Types set and frozenset:set types ---
##Mathematically speaking ,a set is an unordered collection of zero or more distinct elements.
##Most operations on sets work with both set and frozenset types.
##Values of type set are mutable:you can add or delete members.
##There are two ways to create a mutable set.
##In all python version of the 2.x series,the set(S) function operates on a sequence S and returns
##a mutable set containing the unique elements of S. The arguments is optional;if omitted,
##you get a new , empty set.

##Li = [1,3,5,3,3,5,5,9,8,8,0]
##s1 = set(Li)
##s2 = set()
##s3 = set('notlob bolton')
##print Li,len(Li)
##print s1,len(s1)
##print s2,len(s2)
##print s3,len(s3)

##A frozenset value is immutable:you can not change the membership,but you can use
##a frozenset value in contexts where set values are not allowed. For example,you can
##use a frozenset as a key in a dictionary ,but you cant not use a set values
##as a dictionary key.

--- Type dict:Dictinonaries ---
Python dictionaries are one of its more powerful built-in types.They are generally
used for look-up tables and many similare applications.

A Python dictionary represents a set of zero or more ordered pairs(ki,vi) such that:
Each k value is called a key;
Each key is unique and immutable;
and the associated value vi can be of any type.
Another term for this structure is mapping ,since it maps the set of keys onto the set
of values (in the algebraic sense).

Operations on dictionaries
These operations are available on any dictionary object D:
len(D):returns the number of key-value pairs in D.
D[k]: If diactionary D has a key whose value is equal to K,this operation
returns the corresponding value for that key.If there is no matching key,
it raises a KeyError exception.
D[k] = v : If dictionary D dose not have a key-value pair whose key equals k,
a new pair is added with key k and value v.
If D already has a key-value pair whose key equals k,the value of that pair is replaced by v.
k in D : A predicate that tests whether D has a key equal to k.
k not in D: A predicate that tests whether D does not have a key equal to k.
del D[k]: In Python ,del is a statement ,not a function;If dictionary D has a
key-value pair whose key equals k,that key-value pair is deleted from D. If there
is no matching key-value pair,the statement will raise a KeyError exception.
D.get(k,x):If dictionary D has a key equal to x,it returns the corresponding value,
that is ,it is the same as the expression "D[x]".

原文地址:https://www.cnblogs.com/Alex-Zeng/p/3270746.html