Fork me on GitHub

Python之路,Day2 - Python基础2

运算符

1、算数运算:

2、比较运算:

3、赋值运算:

4、逻辑运算:

5、成员运算:

基本数据类型

1、数字

int(整型)

  在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
  在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
2、布尔值
  真或假
  1 或 0
3、字符串
"hello world"
字符串常用功能:
  • 移除空白
  • 分割
  • 长度
  • 索引
  • 切片
  1. class str(basestring):
  2. """
  3. str(object='') -> string
  4. Return a nice string representation of the object.
  5. If the argument is a string, the return value is the same object.
  6. """
  7. def capitalize(self):
  8. """ 首字母变大写 """
  9. """
  10. S.capitalize() -> string
  11. Return a copy of the string S with only its first character
  12. capitalized.
  13. """
  14. return ""
  15. def center(self, width, fillchar=None):
  16. """ 内容居中,width:总长度;fillchar:空白处填充内容,默认无 """
  17. """
  18. S.center(width[, fillchar]) -> string
  19. Return S centered in a string of length width. Padding is
  20. done using the specified fill character (default is a space)
  21. """
  22. return ""
  23. def count(self, sub, start=None, end=None):
  24. """ 子序列个数 """
  25. """
  26. S.count(sub[, start[, end]]) -> int
  27. Return the number of non-overlapping occurrences of substring sub in
  28. string S[start:end]. Optional arguments start and end are interpreted
  29. as in slice notation.
  30. """
  31. return 0
  32. def decode(self, encoding=None, errors=None):
  33. """ 解码 """
  34. """
  35. S.decode([encoding[,errors]]) -> object
  36. Decodes S using the codec registered for encoding. encoding defaults
  37. to the default encoding. errors may be given to set a different error
  38. handling scheme. Default is 'strict' meaning that encoding errors raise
  39. a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
  40. as well as any other name registered with codecs.register_error that is
  41. able to handle UnicodeDecodeErrors.
  42. """
  43. return object()
  44. def encode(self, encoding=None, errors=None):
  45. """ 编码,针对unicode """
  46. """
  47. S.encode([encoding[,errors]]) -> object
  48. Encodes S using the codec registered for encoding. encoding defaults
  49. to the default encoding. errors may be given to set a different error
  50. handling scheme. Default is 'strict' meaning that encoding errors raise
  51. a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
  52. 'xmlcharrefreplace' as well as any other name registered with
  53. codecs.register_error that is able to handle UnicodeEncodeErrors.
  54. """
  55. return object()
  56. def endswith(self, suffix, start=None, end=None):
  57. """ 是否以 xxx 结束 """
  58. """
  59. S.endswith(suffix[, start[, end]]) -> bool
  60. Return True if S ends with the specified suffix, False otherwise.
  61. With optional start, test S beginning at that position.
  62. With optional end, stop comparing S at that position.
  63. suffix can also be a tuple of strings to try.
  64. """
  65. return False
  66. def expandtabs(self, tabsize=None):
  67. """ 将tab转换成空格,默认一个tab转换成8个空格 """
  68. """
  69. S.expandtabs([tabsize]) -> string
  70. Return a copy of S where all tab characters are expanded using spaces.
  71. If tabsize is not given, a tab size of 8 characters is assumed.
  72. """
  73. return ""
  74. def find(self, sub, start=None, end=None):
  75. """ 寻找子序列位置,如果没找到,返回 -1 """
  76. """
  77. S.find(sub [,start [,end]]) -> int
  78. Return the lowest index in S where substring sub is found,
  79. such that sub is contained within S[start:end]. Optional
  80. arguments start and end are interpreted as in slice notation.
  81. Return -1 on failure.
  82. """
  83. return 0
  84. def format(*args, **kwargs): # known special case of str.format
  85. """ 字符串格式化,动态参数,将函数式编程时细说 """
  86. """
  87. S.format(*args, **kwargs) -> string
  88. Return a formatted version of S, using substitutions from args and kwargs.
  89. The substitutions are identified by braces ('{' and '}').
  90. """
  91. pass
  92. def index(self, sub, start=None, end=None):
  93. """ 子序列位置,如果没找到,报错 """
  94. S.index(sub [,start [,end]]) -> int
  95. Like S.find() but raise ValueError when the substring is not found.
  96. """
  97. return 0
  98. def isalnum(self):
  99. """ 是否是字母和数字 """
  100. """
  101. S.isalnum() -> bool
  102. Return True if all characters in S are alphanumeric
  103. and there is at least one character in S, False otherwise.
  104. """
  105. return False
  106. def isalpha(self):
  107. """ 是否是字母 """
  108. """
  109. S.isalpha() -> bool
  110. Return True if all characters in S are alphabetic
  111. and there is at least one character in S, False otherwise.
  112. """
  113. return False
  114. def isdigit(self):
  115. """ 是否是数字 """
  116. """
  117. S.isdigit() -> bool
  118. Return True if all characters in S are digits
  119. and there is at least one character in S, False otherwise.
  120. """
  121. return False
  122. def islower(self):
  123. """ 是否小写 """
  124. """
  125. S.islower() -> bool
  126. Return True if all cased characters in S are lowercase and there is
  127. at least one cased character in S, False otherwise.
  128. """
  129. return False
  130. def isspace(self):
  131. """
  132. S.isspace() -> bool
  133. Return True if all characters in S are whitespace
  134. and there is at least one character in S, False otherwise.
  135. """
  136. return False
  137. def istitle(self):
  138. """
  139. S.istitle() -> bool
  140. Return True if S is a titlecased string and there is at least one
  141. character in S, i.e. uppercase characters may only follow uncased
  142. characters and lowercase characters only cased ones. Return False
  143. otherwise.
  144. """
  145. return False
  146. def isupper(self):
  147. """
  148. S.isupper() -> bool
  149. Return True if all cased characters in S are uppercase and there is
  150. at least one cased character in S, False otherwise.
  151. """
  152. return False
  153. def join(self, iterable):
  154. """ 连接 """
  155. """
  156. S.join(iterable) -> string
  157. Return a string which is the concatenation of the strings in the
  158. iterable. The separator between elements is S.
  159. """
  160. return ""
  161. def ljust(self, width, fillchar=None):
  162. """ 内容左对齐,右侧填充 """
  163. """
  164. S.ljust(width[, fillchar]) -> string
  165. Return S left-justified in a string of length width. Padding is
  166. done using the specified fill character (default is a space).
  167. """
  168. return ""
  169. def lower(self):
  170. """ 变小写 """
  171. """
  172. S.lower() -> string
  173. Return a copy of the string S converted to lowercase.
  174. """
  175. return ""
  176. def lstrip(self, chars=None):
  177. """ 移除左侧空白 """
  178. """
  179. S.lstrip([chars]) -> string or unicode
  180. Return a copy of the string S with leading whitespace removed.
  181. If chars is given and not None, remove characters in chars instead.
  182. If chars is unicode, S will be converted to unicode before stripping
  183. """
  184. return ""
  185. def partition(self, sep):
  186. """ 分割,前,中,后三部分 """
  187. """
  188. S.partition(sep) -> (head, sep, tail)
  189. Search for the separator sep in S, and return the part before it,
  190. the separator itself, and the part after it. If the separator is not
  191. found, return S and two empty strings.
  192. """
  193. pass
  194. def replace(self, old, new, count=None):
  195. """ 替换 """
  196. """
  197. S.replace(old, new[, count]) -> string
  198. Return a copy of string S with all occurrences of substring
  199. old replaced by new. If the optional argument count is
  200. given, only the first count occurrences are replaced.
  201. """
  202. return ""
  203. def rfind(self, sub, start=None, end=None):
  204. """
  205. S.rfind(sub [,start [,end]]) -> int
  206. Return the highest index in S where substring sub is found,
  207. such that sub is contained within S[start:end]. Optional
  208. arguments start and end are interpreted as in slice notation.
  209. Return -1 on failure.
  210. """
  211. return 0
  212. def rindex(self, sub, start=None, end=None):
  213. """
  214. S.rindex(sub [,start [,end]]) -> int
  215. Like S.rfind() but raise ValueError when the substring is not found.
  216. """
  217. return 0
  218. def rjust(self, width, fillchar=None):
  219. """
  220. S.rjust(width[, fillchar]) -> string
  221. Return S right-justified in a string of length width. Padding is
  222. done using the specified fill character (default is a space)
  223. """
  224. return ""
  225. def rpartition(self, sep):
  226. """
  227. S.rpartition(sep) -> (head, sep, tail)
  228. Search for the separator sep in S, starting at the end of S, and return
  229. the part before it, the separator itself, and the part after it. If the
  230. separator is not found, return two empty strings and S.
  231. """
  232. pass
  233. def rsplit(self, sep=None, maxsplit=None):
  234. """
  235. S.rsplit([sep [,maxsplit]]) -> list of strings
  236. Return a list of the words in the string S, using sep as the
  237. delimiter string, starting at the end of the string and working
  238. to the front. If maxsplit is given, at most maxsplit splits are
  239. done. If sep is not specified or is None, any whitespace string
  240. is a separator.
  241. """
  242. return []
  243. def rstrip(self, chars=None):
  244. """
  245. S.rstrip([chars]) -> string or unicode
  246. Return a copy of the string S with trailing whitespace removed.
  247. If chars is given and not None, remove characters in chars instead.
  248. If chars is unicode, S will be converted to unicode before stripping
  249. """
  250. return ""
  251. def split(self, sep=None, maxsplit=None):
  252. """ 分割, maxsplit最多分割几次 """
  253. """
  254. S.split([sep [,maxsplit]]) -> list of strings
  255. Return a list of the words in the string S, using sep as the
  256. delimiter string. If maxsplit is given, at most maxsplit
  257. splits are done. If sep is not specified or is None, any
  258. whitespace string is a separator and empty strings are removed
  259. from the result.
  260. """
  261. return []
  262. def splitlines(self, keepends=False):
  263. """ 根据换行分割 """
  264. """
  265. S.splitlines(keepends=False) -> list of strings
  266. Return a list of the lines in S, breaking at line boundaries.
  267. Line breaks are not included in the resulting list unless keepends
  268. is given and true.
  269. """
  270. return []
  271. def startswith(self, prefix, start=None, end=None):
  272. """ 是否起始 """
  273. """
  274. S.startswith(prefix[, start[, end]]) -> bool
  275. Return True if S starts with the specified prefix, False otherwise.
  276. With optional start, test S beginning at that position.
  277. With optional end, stop comparing S at that position.
  278. prefix can also be a tuple of strings to try.
  279. """
  280. return False
  281. def strip(self, chars=None):
  282. """ 移除两段空白 """
  283. """
  284. S.strip([chars]) -> string or unicode
  285. Return a copy of the string S with leading and trailing
  286. whitespace removed.
  287. If chars is given and not None, remove characters in chars instead.
  288. If chars is unicode, S will be converted to unicode before stripping
  289. """
  290. return ""
  291. def swapcase(self):
  292. """ 大写变小写,小写变大写 """
  293. """
  294. S.swapcase() -> string
  295. Return a copy of the string S with uppercase characters
  296. converted to lowercase and vice versa.
  297. """
  298. return ""
  299. def title(self):
  300. """
  301. S.title() -> string
  302. Return a titlecased version of S, i.e. words start with uppercase
  303. characters, all remaining cased characters have lowercase.
  304. """
  305. return ""
  306. def translate(self, table, deletechars=None):
  307. """
  308. 转换,需要先做一个对应表,最后一个表示删除字符集合
  309. intab = "aeiou"
  310. outtab = "12345"
  311. trantab = maketrans(intab, outtab)
  312. str = "this is string example....wow!!!"
  313. print str.translate(trantab, 'xm')
  314. """
  315. """
  316. S.translate(table [,deletechars]) -> string
  317. Return a copy of the string S, where all characters occurring
  318. in the optional argument deletechars are removed, and the
  319. remaining characters have been mapped through the given
  320. translation table, which must be a string of length 256 or None.
  321. If the table argument is None, no translation is applied and
  322. the operation simply removes the characters in deletechars.
  323. """
  324. return ""
  325. def upper(self):
  326. """
  327. S.upper() -> string
  328. Return a copy of the string S converted to uppercase.
  329. """
  330. return ""
  331. def zfill(self, width):
  332. """方法返回指定长度的字符串,原字符串右对齐,前面填充0"""
  333. """
  334. S.zfill(width) -> string
  335. Pad a numeric string S with zeros on the left, to fill a field
  336. of the specified width. The string S is never truncated.
  337. """
  338. return ""
  339. def _formatter_field_name_split(self, *args, **kwargs): # real signature unknown
  340. pass
  341. def _formatter_parser(self, *args, **kwargs): # real signature unknown
  342. pass
  343. def __add__(self, y):
  344. """ x.__add__(y) <==> x+y """
  345. pass
  346. def __contains__(self, y):
  347. """ x.__contains__(y) <==> y in x """
  348. pass
  349. def __eq__(self, y):
  350. """ x.__eq__(y) <==> x==y """
  351. pass
  352. def __format__(self, format_spec):
  353. """
  354. S.__format__(format_spec) -> string
  355. Return a formatted version of S as described by format_spec.
  356. """
  357. return ""
  358. def __getattribute__(self, name):
  359. """ x.__getattribute__('name') <==> x.name """
  360. pass
  361. def __getitem__(self, y):
  362. """ x.__getitem__(y) <==> x[y] """
  363. pass
  364. def __getnewargs__(self, *args, **kwargs): # real signature unknown
  365. pass
  366. def __getslice__(self, i, j):
  367. """
  368. x.__getslice__(i, j) <==> x[i:j]
  369. Use of negative indices is not supported.
  370. """
  371. pass
  372. def __ge__(self, y):
  373. """ x.__ge__(y) <==> x>=y """
  374. pass
  375. def __gt__(self, y):
  376. """ x.__gt__(y) <==> x>y """
  377. pass
  378. def __hash__(self):
  379. """ x.__hash__() <==> hash(x) """
  380. pass
  381. def __init__(self, string=''): # known special case of str.__init__
  382. """
  383. str(object='') -> string
  384. Return a nice string representation of the object.
  385. If the argument is a string, the return value is the same object.
  386. # (copied from class doc)
  387. """
  388. pass
  389. def __len__(self):
  390. """ x.__len__() <==> len(x) """
  391. pass
  392. def __le__(self, y):
  393. """ x.__le__(y) <==> x<=y """
  394. pass
  395. def __lt__(self, y):
  396. """ x.__lt__(y) <==> x<y """
  397. pass
  398. def __mod__(self, y):
  399. """ x.__mod__(y) <==> x%y """
  400. pass
  401. def __mul__(self, n):
  402. """ x.__mul__(n) <==> x*n """
  403. pass
  404. @staticmethod # known case of __new__
  405. def __new__(S, *more):
  406. """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
  407. pass
  408. def __ne__(self, y):
  409. """ x.__ne__(y) <==> x!=y """
  410. pass
  411. def __repr__(self):
  412. """ x.__repr__() <==> repr(x) """
  413. pass
  414. def __rmod__(self, y):
  415. """ x.__rmod__(y) <==> y%x """
  416. pass
  417. def __rmul__(self, n):
  418. """ x.__rmul__(n) <==> n*x """
  419. pass
  420. def __sizeof__(self):
  421. """ S.__sizeof__() -> size of S in memory, in bytes """
  422. pass
  423. def __str__(self):
  424. """ x.__str__() <==> str(x) """
  425. pass
  426. str

4、列表
创建列表:
1
2
3
name_list = ['alex''seven''eric']
name_list = list(['alex''seven''eric'])

基本操作:

  • 索引
  • 切片
  • 追加
  • 删除
  • 长度
  • 切片
  • 循环
  • 包含

  1. class list(object):
  2. """
  3. list() -> new empty list
  4. list(iterable) -> new list initialized from iterable's items
  5. """
  6. def append(self, p_object): # real signature unknown; restored from __doc__
  7. """ L.append(object) -- append object to end """
  8. pass
  9. def count(self, value): # real signature unknown; restored from __doc__
  10. """ L.count(value) -> integer -- return number of occurrences of value """
  11. return 0
  12. def extend(self, iterable): # real signature unknown; restored from __doc__
  13. """ L.extend(iterable) -- extend list by appending elements from the iterable """
  14. pass
  15. def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
  16. """
  17. L.index(value, [start, [stop]]) -> integer -- return first index of value.
  18. Raises ValueError if the value is not present.
  19. """
  20. return 0
  21. def insert(self, index, p_object): # real signature unknown; restored from __doc__
  22. """ L.insert(index, object) -- insert object before index """
  23. pass
  24. def pop(self, index=None): # real signature unknown; restored from __doc__
  25. """
  26. L.pop([index]) -> item -- remove and return item at index (default last).
  27. Raises IndexError if list is empty or index is out of range.
  28. """
  29. pass
  30. def remove(self, value): # real signature unknown; restored from __doc__
  31. """
  32. L.remove(value) -- remove first occurrence of value.
  33. Raises ValueError if the value is not present.
  34. """
  35. pass
  36. def reverse(self): # real signature unknown; restored from __doc__
  37. """ L.reverse() -- reverse *IN PLACE* """
  38. pass
  39. def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
  40. """
  41. L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
  42. cmp(x, y) -> -1, 0, 1
  43. """
  44. pass
  45. def __add__(self, y): # real signature unknown; restored from __doc__
  46. """ x.__add__(y) <==> x+y """
  47. pass
  48. def __contains__(self, y): # real signature unknown; restored from __doc__
  49. """ x.__contains__(y) <==> y in x """
  50. pass
  51. def __delitem__(self, y): # real signature unknown; restored from __doc__
  52. """ x.__delitem__(y) <==> del x[y] """
  53. pass
  54. def __delslice__(self, i, j): # real signature unknown; restored from __doc__
  55. """
  56. x.__delslice__(i, j) <==> del x[i:j]
  57. Use of negative indices is not supported.
  58. """
  59. pass
  60. def __eq__(self, y): # real signature unknown; restored from __doc__
  61. """ x.__eq__(y) <==> x==y """
  62. pass
  63. def __getattribute__(self, name): # real signature unknown; restored from __doc__
  64. """ x.__getattribute__('name') <==> x.name """
  65. pass
  66. def __getitem__(self, y): # real signature unknown; restored from __doc__
  67. """ x.__getitem__(y) <==> x[y] """
  68. pass
  69. def __getslice__(self, i, j): # real signature unknown; restored from __doc__
  70. """
  71. x.__getslice__(i, j) <==> x[i:j]
  72. Use of negative indices is not supported.
  73. """
  74. pass
  75. def __ge__(self, y): # real signature unknown; restored from __doc__
  76. """ x.__ge__(y) <==> x>=y """
  77. pass
  78. def __gt__(self, y): # real signature unknown; restored from __doc__
  79. """ x.__gt__(y) <==> x>y """
  80. pass
  81. def __iadd__(self, y): # real signature unknown; restored from __doc__
  82. """ x.__iadd__(y) <==> x+=y """
  83. pass
  84. def __imul__(self, y): # real signature unknown; restored from __doc__
  85. """ x.__imul__(y) <==> x*=y """
  86. pass
  87. def __init__(self, seq=()): # known special case of list.__init__
  88. """
  89. list() -> new empty list
  90. list(iterable) -> new list initialized from iterable's items
  91. # (copied from class doc)
  92. """
  93. pass
  94. def __iter__(self): # real signature unknown; restored from __doc__
  95. """ x.__iter__() <==> iter(x) """
  96. pass
  97. def __len__(self): # real signature unknown; restored from __doc__
  98. """ x.__len__() <==> len(x) """
  99. pass
  100. def __le__(self, y): # real signature unknown; restored from __doc__
  101. """ x.__le__(y) <==> x<=y """
  102. pass
  103. def __lt__(self, y): # real signature unknown; restored from __doc__
  104. """ x.__lt__(y) <==> x<y """
  105. pass
  106. def __mul__(self, n): # real signature unknown; restored from __doc__
  107. """ x.__mul__(n) <==> x*n """
  108. pass
  109. @staticmethod # known case of __new__
  110. def __new__(S, *more): # real signature unknown; restored from __doc__
  111. """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
  112. pass
  113. def __ne__(self, y): # real signature unknown; restored from __doc__
  114. """ x.__ne__(y) <==> x!=y """
  115. pass
  116. def __repr__(self): # real signature unknown; restored from __doc__
  117. """ x.__repr__() <==> repr(x) """
  118. pass
  119. def __reversed__(self): # real signature unknown; restored from __doc__
  120. """ L.__reversed__() -- return a reverse iterator over the list """
  121. pass
  122. def __rmul__(self, n): # real signature unknown; restored from __doc__
  123. """ x.__rmul__(n) <==> n*x """
  124. pass
  125. def __setitem__(self, i, y): # real signature unknown; restored from __doc__
  126. """ x.__setitem__(i, y) <==> x[i]=y """
  127. pass
  128. def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__
  129. """
  130. x.__setslice__(i, j, y) <==> x[i:j]=y
  131. Use of negative indices is not supported.
  132. """
  133. pass
  134. def __sizeof__(self): # real signature unknown; restored from __doc__
  135. """ L.__sizeof__() -- size of L in memory, in bytes """
  136. pass
  137. __hash__ = None
  138. list
5、元祖
创建元祖:
1
2
3
ages = (1122334455)
ages = tuple((1122334455))
基本操作:
  • 索引
  • 切片
  • 循环
  • 长度
  • 包含
  1. lass tuple(object):
  2. """
  3. tuple() -> empty tuple
  4. tuple(iterable) -> tuple initialized from iterable's items
  5. If the argument is a tuple, the return value is the same object.
  6. """
  7. def count(self, value): # real signature unknown; restored from __doc__
  8. """ T.count(value) -> integer -- return number of occurrences of value """
  9. return 0
  10. def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
  11. """
  12. T.index(value, [start, [stop]]) -> integer -- return first index of value.
  13. Raises ValueError if the value is not present.
  14. """
  15. return 0
  16. def __add__(self, y): # real signature unknown; restored from __doc__
  17. """ x.__add__(y) <==> x+y """
  18. pass
  19. def __contains__(self, y): # real signature unknown; restored from __doc__
  20. """ x.__contains__(y) <==> y in x """
  21. pass
  22. def __eq__(self, y): # real signature unknown; restored from __doc__
  23. """ x.__eq__(y) <==> x==y """
  24. pass
  25. def __getattribute__(self, name): # real signature unknown; restored from __doc__
  26. """ x.__getattribute__('name') <==> x.name """
  27. pass
  28. def __getitem__(self, y): # real signature unknown; restored from __doc__
  29. """ x.__getitem__(y) <==> x[y] """
  30. pass
  31. def __getnewargs__(self, *args, **kwargs): # real signature unknown
  32. pass
  33. def __getslice__(self, i, j): # real signature unknown; restored from __doc__
  34. """
  35. x.__getslice__(i, j) <==> x[i:j]
  36. Use of negative indices is not supported.
  37. """
  38. pass
  39. def __ge__(self, y): # real signature unknown; restored from __doc__
  40. """ x.__ge__(y) <==> x>=y """
  41. pass
  42. def __gt__(self, y): # real signature unknown; restored from __doc__
  43. """ x.__gt__(y) <==> x>y """
  44. pass
  45. def __hash__(self): # real signature unknown; restored from __doc__
  46. """ x.__hash__() <==> hash(x) """
  47. pass
  48. def __init__(self, seq=()): # known special case of tuple.__init__
  49. """
  50. tuple() -> empty tuple
  51. tuple(iterable) -> tuple initialized from iterable's items
  52. If the argument is a tuple, the return value is the same object.
  53. # (copied from class doc)
  54. """
  55. pass
  56. def __iter__(self): # real signature unknown; restored from __doc__
  57. """ x.__iter__() <==> iter(x) """
  58. pass
  59. def __len__(self): # real signature unknown; restored from __doc__
  60. """ x.__len__() <==> len(x) """
  61. pass
  62. def __le__(self, y): # real signature unknown; restored from __doc__
  63. """ x.__le__(y) <==> x<=y """
  64. pass
  65. def __lt__(self, y): # real signature unknown; restored from __doc__
  66. """ x.__lt__(y) <==> x<y """
  67. pass
  68. def __mul__(self, n): # real signature unknown; restored from __doc__
  69. """ x.__mul__(n) <==> x*n """
  70. pass
  71. @staticmethod # known case of __new__
  72. def __new__(S, *more): # real signature unknown; restored from __doc__
  73. """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
  74. pass
  75. def __ne__(self, y): # real signature unknown; restored from __doc__
  76. """ x.__ne__(y) <==> x!=y """
  77. pass
  78. def __repr__(self): # real signature unknown; restored from __doc__
  79. """ x.__repr__() <==> repr(x) """
  80. pass
  81. def __rmul__(self, n): # real signature unknown; restored from __doc__
  82. """ x.__rmul__(n) <==> n*x """
  83. pass
  84. def __sizeof__(self): # real signature unknown; restored from __doc__
  85. """ T.__sizeof__() -- size of T in memory, in bytes """
  86. pass
  87. tuple
6、字典(无序)
创建字典:
1
2
3
person = {"name""mr.wu"'age'18}
person = dict({"name""mr.wu"'age'18})

常用操作:

  • 索引
  • 新增
  • 删除
  • 键、值、键值对
  • 循环
  • 长度

  1. class dict(object):
  2. """
  3. dict() -> new empty dictionary
  4. dict(mapping) -> new dictionary initialized from a mapping object's
  5. (key, value) pairs
  6. dict(iterable) -> new dictionary initialized as if via:
  7. d = {}
  8. for k, v in iterable:
  9. d[k] = v
  10. dict(**kwargs) -> new dictionary initialized with the name=value pairs
  11. in the keyword argument list. For example: dict(one=1, two=2)
  12. """
  13. def clear(self): # real signature unknown; restored from __doc__
  14. """ 清除内容 """
  15. """ D.clear() -> None. Remove all items from D. """
  16. pass
  17. def copy(self): # real signature unknown; restored from __doc__
  18. """ 浅拷贝 """
  19. """ D.copy() -> a shallow copy of D """
  20. pass
  21. @staticmethod # known case
  22. def fromkeys(S, v=None): # real signature unknown; restored from __doc__
  23. """
  24. dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
  25. v defaults to None.
  26. """
  27. pass
  28. def get(self, k, d=None): # real signature unknown; restored from __doc__
  29. """ 根据key获取值,d是默认值 """
  30. """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
  31. pass
  32. def has_key(self, k): # real signature unknown; restored from __doc__
  33. """ 是否有key """
  34. """ D.has_key(k) -> True if D has a key k, else False """
  35. return False
  36. def items(self): # real signature unknown; restored from __doc__
  37. """ 所有项的列表形式 """
  38. """ D.items() -> list of D's (key, value) pairs, as 2-tuples """
  39. return []
  40. def iteritems(self): # real signature unknown; restored from __doc__
  41. """ 项可迭代 """
  42. """ D.iteritems() -> an iterator over the (key, value) items of D """
  43. pass
  44. def iterkeys(self): # real signature unknown; restored from __doc__
  45. """ key可迭代 """
  46. """ D.iterkeys() -> an iterator over the keys of D """
  47. pass
  48. def itervalues(self): # real signature unknown; restored from __doc__
  49. """ value可迭代 """
  50. """ D.itervalues() -> an iterator over the values of D """
  51. pass
  52. def keys(self): # real signature unknown; restored from __doc__
  53. """ 所有的key列表 """
  54. """ D.keys() -> list of D's keys """
  55. return []
  56. def pop(self, k, d=None): # real signature unknown; restored from __doc__
  57. """ 获取并在字典中移除 """
  58. """
  59. D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
  60. If key is not found, d is returned if given, otherwise KeyError is raised
  61. """
  62. pass
  63. def popitem(self): # real signature unknown; restored from __doc__
  64. """ 获取并在字典中移除 """
  65. """
  66. D.popitem() -> (k, v), remove and return some (key, value) pair as a
  67. 2-tuple; but raise KeyError if D is empty.
  68. """
  69. pass
  70. def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
  71. """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """
  72. """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
  73. pass
  74. def update(self, E=None, **F): # known special case of dict.update
  75. """ 更新
  76. {'name':'alex', 'age': 18000}
  77. [('name','sbsbsb'),]
  78. """
  79. """
  80. D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
  81. If E present and has a .keys() method, does: for k in E: D[k] = E[k]
  82. If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
  83. In either case, this is followed by: for k in F: D[k] = F[k]
  84. """
  85. pass
  86. def values(self): # real signature unknown; restored from __doc__
  87. """ 所有的值 """
  88. """ D.values() -> list of D's values """
  89. return []
  90. def viewitems(self): # real signature unknown; restored from __doc__
  91. """ 所有项,只是将内容保存至view对象中 """
  92. """ D.viewitems() -> a set-like object providing a view on D's items """
  93. pass
  94. def viewkeys(self): # real signature unknown; restored from __doc__
  95. """ D.viewkeys() -> a set-like object providing a view on D's keys """
  96. pass
  97. def viewvalues(self): # real signature unknown; restored from __doc__
  98. """ D.viewvalues() -> an object providing a view on D's values """
  99. pass
  100. def __cmp__(self, y): # real signature unknown; restored from __doc__
  101. """ x.__cmp__(y) <==> cmp(x,y) """
  102. pass
  103. def __contains__(self, k): # real signature unknown; restored from __doc__
  104. """ D.__contains__(k) -> True if D has a key k, else False """
  105. return False
  106. def __delitem__(self, y): # real signature unknown; restored from __doc__
  107. """ x.__delitem__(y) <==> del x[y] """
  108. pass
  109. def __eq__(self, y): # real signature unknown; restored from __doc__
  110. """ x.__eq__(y) <==> x==y """
  111. pass
  112. def __getattribute__(self, name): # real signature unknown; restored from __doc__
  113. """ x.__getattribute__('name') <==> x.name """
  114. pass
  115. def __getitem__(self, y): # real signature unknown; restored from __doc__
  116. """ x.__getitem__(y) <==> x[y] """
  117. pass
  118. def __ge__(self, y): # real signature unknown; restored from __doc__
  119. """ x.__ge__(y) <==> x>=y """
  120. pass
  121. def __gt__(self, y): # real signature unknown; restored from __doc__
  122. """ x.__gt__(y) <==> x>y """
  123. pass
  124. def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
  125. """
  126. dict() -> new empty dictionary
  127. dict(mapping) -> new dictionary initialized from a mapping object's
  128. (key, value) pairs
  129. dict(iterable) -> new dictionary initialized as if via:
  130. d = {}
  131. for k, v in iterable:
  132. d[k] = v
  133. dict(**kwargs) -> new dictionary initialized with the name=value pairs
  134. in the keyword argument list. For example: dict(one=1, two=2)
  135. # (copied from class doc)
  136. """
  137. pass
  138. def __iter__(self): # real signature unknown; restored from __doc__
  139. """ x.__iter__() <==> iter(x) """
  140. pass
  141. def __len__(self): # real signature unknown; restored from __doc__
  142. """ x.__len__() <==> len(x) """
  143. pass
  144. def __le__(self, y): # real signature unknown; restored from __doc__
  145. """ x.__le__(y) <==> x<=y """
  146. pass
  147. def __lt__(self, y): # real signature unknown; restored from __doc__
  148. """ x.__lt__(y) <==> x<y """
  149. pass
  150. @staticmethod # known case of __new__
  151. def __new__(S, *more): # real signature unknown; restored from __doc__
  152. """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
  153. pass
  154. def __ne__(self, y): # real signature unknown; restored from __doc__
  155. """ x.__ne__(y) <==> x!=y """
  156. pass
  157. def __repr__(self): # real signature unknown; restored from __doc__
  158. """ x.__repr__() <==> repr(x) """
  159. pass
  160. def __setitem__(self, i, y): # real signature unknown; restored from __doc__
  161. """ x.__setitem__(i, y) <==> x[i]=y """
  162. pass
  163. def __sizeof__(self): # real signature unknown; restored from __doc__
  164. """ D.__sizeof__() -> size of D in memory, in bytes """
  165. pass
  166. __hash__ = None
  167. dict
PS:循环,range,continue 和 break
1、for循环
用户按照顺序循环可迭代对象中的内容,
PS:break、continue
  1. li = [11,22,33,44]
  2. for item in li:
  3. print item
2、enumrate
  1. li = [11,22,33]
  2. for k,v in enumerate(li, 1):
  3. print(k,v)

3、range和xrange
指定范围,生成指定的数字
  1. print range(1, 10)
  2. # 结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
  3. print range(1, 10, 2)
  4. # 结果:[1, 3, 5, 7, 9]
  5. print range(30, 0, -2)
  6. # 结果:[30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2] 

PS:字符串的处理

  1. str='python String function'
  2. 生成字符串变量str='python String function'
  3. 字符串长度获取:len(str)
  4. 例:print '%s length=%d' % (str,len(str))
  5. 字母处理
  6. 全部大写:str.upper()
  7. 全部小写:str.lower()
  8. 大小写互换:str.swapcase()
  9. 首字母大写,其余小写:str.capitalize()
  10. 首字母大写:str.title()
  11. print '%s lower=%s' % (str,str.lower())
  12. print '%s upper=%s' % (str,str.upper())
  13. print '%s swapcase=%s' % (str,str.swapcase())
  14. print '%s capitalize=%s' % (str,str.capitalize())
  15. print '%s title=%s' % (str,str.title())
  16. 格式化相关
  17. 获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
  18. 获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
  19. 获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
  20. 获取固定长度,右对齐,左边不足用0补齐
  21. print '%s ljust=%s' % (str,str.ljust(20))
  22. print '%s rjust=%s' % (str,str.rjust(20))
  23. print '%s center=%s' % (str,str.center(20))
  24. print '%s zfill=%s' % (str,str.zfill(20))
  25. 字符串搜索相关
  26. 搜索指定字符串,没有返回-1str.find('t')
  27. 指定起始位置搜索:str.find('t',start)
  28. 指定起始及结束位置搜索:str.find('t',start,end)
  29. 从右边开始查找:str.rfind('t')
  30. 搜索到多少个指定字符串:str.count('t')
  31. 上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
  32. print '%s find nono=%d' % (str,str.find('nono'))
  33. print '%s find t=%d' % (str,str.find('t'))
  34. print '%s find t from %d=%d' % (str,1,str.find('t',1))
  35. print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
  36. #print '%s index nono ' % (str,str.index('nono',1,2))
  37. print '%s rfind t=%d' % (str,str.rfind('t'))
  38. print '%s count t=%d' % (str,str.count('t'))
  39. 字符串替换相关
  40. 替换oldnewstr.replace('old','new')
  41. 替换指定次数的oldnewstr.replace('old','new',maxReplaceTimes)
  42. print '%s replace t to *=%s' % (str,str.replace('t', '*'))
  43. print '%s replace t to *=%s' % (str,str.replace('t', '*',1))
  44. 字符串去空格及去指定字符
  45. 去两边空格:str.strip()
  46. 去左空格:str.lstrip()
  47. 去右空格:str.rstrip()
  48. 去两边字符串:str.strip('d'),相应的也有lstriprstrip
  49. str=' python String function '
  50. print '%s strip=%s' % (str,str.strip())
  51. str='python String function'
  52. print '%s strip=%s' % (str,str.strip('d'))
  53. 按指定字符分割字符串为数组:str.split(' ')
  54. 默认按空格分隔
  55. str='a b c de'
  56. print '%s strip=%s' % (str,str.split())
  57. str='a-b-c-de'
  58. print '%s strip=%s' % (str,str.split('-'))
  59. 字符串判断相关
  60. 是否以start开头:str.startswith('start')
  61. 是否以end结尾:str.endswith('end')
  62. 是否全为字母或数字:str.isalnum()
  63. 是否全字母:str.isalpha()
  64. 是否全数字:str.isdigit()
  65. 是否全小写:str.islower()
  66. 是否全大写:str.isupper()
  67. str='python String function'
  68. print '%s startwith t=%s' % (str,str.startswith('t'))
  69. print '%s endwith d=%s' % (str,str.endswith('d'))
  70. print '%s isalnum=%s' % (str,str.isalnum())
  71. str='pythonStringfunction'
  72. print '%s isalnum=%s' % (str,str.isalnum())
  73. print '%s isalpha=%s' % (str,str.isalpha())
  74. print '%s isupper=%s' % (str,str.isupper())
  75. print '%s islower=%s' % (str,str.islower())
  76. print '%s isdigit=%s' % (str,str.isdigit())
  77. str='3423'
  78. print '%s isdigit=%s' % (str,str.isdigit())
作业:




附件列表

     

    posted @ 2016-10-21 15:31  Anonymous-develop  阅读(334)  评论(0编辑  收藏  举报

    python自动化开发&研究 - 创建于 2016年1月10日

    这是一位运维自动化开发工程师的个人站,内容主要是网站开发方面的技术文章,大部分来自学习或工作,部分来源于网络,希望对大家有所帮助。

    致力于自动化应用开发&研究工作,专注运维与前端开发,关注互联网前沿技术与趋势。


    廖雪峰的博客 | 徐亮的博客 | 刘耀的博客 | python基础课程 | 我的svn | 我的个人导航首页