Learning Python 10.10
英文版前面的部分实在没心情看,所以实际上是从127页开始的。
Python的核心数据类型:

Python是动态类型,强类型的。
Number:基本操作不提,有两个重要的相关模块:math, random。Number上不能直接用len()。
String:可以+、*,可以各种切片,可以len(),它属于一种Immutable(不能s[0]='c') Sequence Type(bytes, bytearray, list, tuple, range都是Sequence Type,但它们中只有tuple是Immutable,另外Number也是Immutable但不是Sequence Type)。Immutable Sequence Type包括以下通用方法:

String有许多特定方法。如下:
str方法
1 # -*- coding: utf-8 -*- 2 3 print("prestige".capitalize()) 4 #Prestige 5 print("prestige".center(2)) 6 #prestige 7 print("prestige".encode(encoding='utf_8', errors='strict')) 8 #b'prestige' 9 print("prestige".count("e")) 10 #2 11 print("prestige".endswith("ge")) 12 #True 13 print("pres\ttige".expandtabs(2)) 14 #pres tige 15 print("prestige".find("e")) 16 #2 17 print("The sum of 1 + 2 is {0}".format(1+2)) 18 #The sum of 1 + 2 is 3 19 print("The number of Ronaldo is {Ronaldo}".format_map({"Ronaldo":9})) 20 #The sum of 1 + 2 is 3 21 print("prestige".index("e")) 22 #2 #like find but raise ValueError when not found 23 print("you@there".isalnum()) 24 #False #True if all characters are alpha and numbers 25 print("3Q".isalpha()) 26 #False #True if all characters are alpha 27 print("3Q".isdecimal()) 28 #False #True if all characters are decimal 29 print("3Q".isdigit()) 30 #False #True if all characters are digits 31 print("3Q".isidentifier()) 32 #False #True if it's a legal identifier 33 print("Prestige".islower()) 34 #False #True if all alpha are lowercase 35 print("3Q".isnumeric()) 36 #False #True if all characters are numeric 37 print("3Q".isprintable()) 38 #True #True if all characters are printable 39 print("prestige".isspace()) 40 #False #True if all characters are spaces 41 print("prestige".istitle()) 42 #False #True if all characters 43 print("prestige".isupper()) 44 #False #True if all characters are uppercase 45 print("Batman".join("123456")) 46 #1Batman2Batman3Batman4Batman5Batman6 47 print("prestige".ljust(10)) 48 #prestige #two spaces added on the right 49 print("Prestige".lower()) 50 #prestige 51 print("prestige".lstrip("p")) 52 #restige 53 print("prestige".partition("e")) 54 #('pr', 'e', 'stige') 55 print("prestige".replace("e","i"))\ 56 #pristigi 57 print("prestige".rfind("e")) 58 #7 59 print("prestige".rindex("e")) 60 #7 61 print("prestige".rjust(10)) 62 # prestige 63 print("prestige".rpartition("e")) 64 #('prestig', 'e', '') 65 print("prestige".rsplit("e")) 66 #['pr', 'stig', ''] 67 print("prestige".rstrip("e")) 68 #prestig 69 print("prestige".split("e")) 70 #['pr', 'stig', ''] 71 print("prestige\nmemento".splitlines()) 72 #['prestige', 'memento'] 73 print("prestige".startswith("p")) 74 #True 75 print("Prestige".swapcase()) 76 #pRESTIGE 77 print("Prestige".title()) 78 #Prestige 79 table = str.maketrans("aeiou","12345") 80 print("Prestige".translate(table)) 81 #Pr2st3g2 82 print("Prestige".upper()) 83 #PRESTIGE 84 print("Prestige".zfill(10)) 85 #00Prestige
查看某类型上的方法:dir(var),查看某方法用法:help(var.method)
字符串可以使用正则匹配:import re,re.match(pattern,dest).groups()
List:它是一种Mutable Sequence Type(与它同样的还有bytes、bytearray),Mutable Sequence Type支持的操作有

未看到仅List支持而其他Mutable Sequence Type均不支持的方法(书中列出的List的Type-specfic operations在Python v3.2.3 documentation中均为Mutable Sequence Type上的方法
List上的边界检查、嵌套和基于迭代器的一些高级操作暂不详述。
至142页止

浙公网安备 33010602011771号