码迷,mamicode.com
首页 > 编程语言 > 详细

python之路-基础篇-day2

时间:2016-01-14 00:52:04      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:

基本数据类型

在Python中,一切事物都是对象,对象基于类创建

技术分享

注:查看对象相关成员的函数有:var,type,dir

基本数据类型主要有:

  • 整数
  • 长整型
  • 浮点型
  • 字符串
  • 列表
  • 元组
  • 字典
  • set集合

接下来将对以上数据类型进行剖析:
1、整数 int
如:18、73、-100

每一个整数都具备如下功能:

  1. class int(object):
  2. """
  3. int(x=0) -> integer
  4. int(x, base=10) -> integer
  5. Convert a number or string to an integer, or return 0 if no arguments
  6. are given. If x is a number, return x.__int__(). For floating point
  7. numbers, this truncates towards zero.
  8. If x is not a number or if base is given, then x must be a string,
  9. bytes, or bytearray instance representing an integer literal in the
  10. given base. The literal can be preceded by ‘+‘ or ‘-‘ and be surrounded
  11. by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
  12. Base 0 means to interpret the base from the string as an integer literal.
  13. >>> int(‘0b100‘, base=0)
  14. 4
  15. """
  16. def bit_length(self): # real signature unknown; restored from __doc__
  17. """
  18. int.bit_length() -> int
  19. Number of bits necessary to represent self in binary.
  20. """
  21. """
  22. 表示该数字返回时占用的最少位数
  23. 例如:整数37用二进制方法表示为:0b100101,调用.bit_length方法时他只占了6位
  24. >>> bin(37)
  25. ‘0b100101‘
  26. >>> (37).bit_length()
  27. 6
  28. 整数10用二进制方法表示为:0b1010,调用.bit_length方法时他只占了4位
  29. >>> bin(10)
  30. ‘0b1010‘
  31. >>> (10).bit_length()
  32. 4
  33. """
  34. return 0
  35. def conjugate(self, *args, **kwargs): # real signature unknown
  36. """ Returns self, the complex conjugate of any int."""
  37. """返回该复数的共轭复数""" # <--- 银角大王说没啥卵用
  38. pass
  39. @classmethod # known case
  40. def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
  41. """
  42. int.from_bytes(bytes, byteorder, *, signed=False) -> int
  43. Return the integer represented by the given array of bytes.
  44. The bytes argument must be a bytes-like object (e.g. bytes or bytearray).
  45. The byteorder argument determines the byte order used to represent the
  46. integer. If byteorder is ‘big‘, the most significant byte is at the
  47. beginning of the byte array. If byteorder is ‘little‘, the most
  48. significant byte is at the end of the byte array. To request the native
  49. byte order of the host system, use `sys.byteorder‘ as the byte order value.
  50. The signed keyword-only argument indicates whether two‘s complement is
  51. used to represent the integer.
  52. """
  53. """
  54. python官方给出了下面几个例子,我是没看懂,谁看懂了告诉我下:
  55. 这个方法是在Python3.2的时候加入的
  56. >>> int.from_bytes(b‘\x00\x10‘, byteorder=‘big‘)
  57. 16
  58. >>> int.from_bytes(b‘\x00\x10‘, byteorder=‘little‘)
  59. 4096
  60. >>> int.from_bytes(b‘\xfc\x00‘, byteorder=‘big‘, signed=True)
  61. -1024
  62. >>> int.from_bytes(b‘\xfc\x00‘, byteorder=‘big‘, signed=False)
  63. 64512
  64. >>> int.from_bytes([255, 0, 0], byteorder=‘big‘)
  65. 16711680
  66. """
  67. pass
  68. def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
  69. """
  70. int.to_bytes(length, byteorder, *, signed=False) -> bytes
  71. Return an array of bytes representing an integer.
  72. The integer is represented using length bytes. An OverflowError is
  73. raised if the integer is not representable with the given number of
  74. bytes.
  75. The byteorder argument determines the byte order used to represent the
  76. integer. If byteorder is ‘big‘, the most significant byte is at the
  77. beginning of the byte array. If byteorder is ‘little‘, the most
  78. significant byte is at the end of the byte array. To request the native
  79. byte order of the host system, use `sys.byteorder‘ as the byte order value.
  80. The signed keyword-only argument determines whether two‘s complement is
  81. used to represent the integer. If signed is False and a negative integer
  82. is given, an OverflowError is raised.
  83. """
  84. """
  85. # 应该是跟上面的from_bytes是相反的,感觉没啥卵用
  86. 官方例子:
  87. >>> (1024).to_bytes(2, byteorder=‘big‘)
  88. b‘\x04\x00‘
  89. >>> (1024).to_bytes(10, byteorder=‘big‘)
  90. b‘\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00‘
  91. >>> (-1024).to_bytes(10, byteorder=‘big‘, signed=True)
  92. b‘\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00‘
  93. >>> x = 1000
  94. >>> x.to_bytes((x.bit_length() // 8) + 1, byteorder=‘little‘)
  95. b‘\xe8\x03‘
  96. """
  97. pass
  98. def __abs__(self, *args, **kwargs): # real signature unknown
  99. """ abs(self)"""
  100. """
  101. 返回一个绝对值
  102. 例子:
  103. >>> (10).__abs__()
  104. 10
  105. >>> (-10).__abs__()
  106. 10
  107. """
  108. pass
  109. # 这里有两个__add__() 应该是用来区分数字和字符串的
  110. def __add__(self, *args, **kwargs): # real signature unknown
  111. """ Return self+value."""
  112. """
  113. 例子:
  114. >>> (3).__add__(4)
  115. 7
  116. # 在python3.x中,数字不能和字符串相加
  117. >>> (3).__add__(‘a‘)
  118. NotImplemented
  119. """
  120. pass
  121. def __and__(self, *args, **kwargs): # real signature unknown
  122. """ Return self&value."""
  123. """
  124. 例子:
  125. >>> a = 3
  126. >>> a.__add__(4)
  127. 7
  128. """
  129. pass
  130. def __bool__(self, *args, **kwargs): # real signature unknown
  131. """ self != 0 """
  132. """
  133. 判断一个整数对象是否为0,如果为0,则返回False,如果不为0,则返回True
  134. 例子:
  135. >>> (-1).__bool__()
  136. True
  137. >>> (0).__bool__()
  138. False
  139. """
  140. pass
  141. def __ceil__(self, *args, **kwargs): # real signature unknown
  142. """ Ceiling of an Integral returns itself. """
  143. """
  144. 好像是返回自己本身,没啥卵用
  145. 例子:
  146. >>> (35).__ceil__()
  147. 35
  148. >>> (35343).__ceil__()
  149. 35343
  150. >>> (-35343).__ceil__()
  151. -35343
  152. """
  153. pass
  154. def __divmod__(self, *args, **kwargs): # real signature unknown
  155. """ Return divmod(self, value). """
  156. """
  157. 返回一个元组,第一个元素为商,第二个元素为余数
  158. 例子:
  159. >>> (3).__divmod__(2)
  160. (1, 1)
  161. >>> (3).__divmod__(3)
  162. (1, 0)
  163. >>> (3).__divmod__(4)
  164. (0, 3)
  165. """
  166. pass
  167. def __eq__(self, *args, **kwargs): # real signature unknown
  168. """ Return self==value. """
  169. """
  170. 判断两个值是否相等
  171. 例子:
  172. >>> (3).__eq__(4)
  173. False
  174. >>> (3).__eq__(3)
  175. True
  176. """
  177. pass
  178. def __float__(self, *args, **kwargs): # real signature unknown
  179. """ float(self) """
  180. """
  181. 将一个整数转换成浮点型
  182. 例子:
  183. >>> a = 1
  184. >>> a.__float__()
  185. 1.0
  186. """
  187. pass
  188. def __floordiv__(self, *args, **kwargs): # real signature unknown
  189. """ Return self//value. """
  190. """
  191. 整除,保留结果的整数部分
  192. 例子:
  193. >>> a = 10
  194. >>> b = 3
  195. >>> a.__floordiv__(b)
  196. 3
  197. """
  198. pass
  199. def __floor__(self, *args, **kwargs): # real signature unknown
  200. """ Flooring an Integral returns itself. """
  201. """
  202. 返回本身,没啥卵用
  203. 例子:
  204. a = 10
  205. >>> a.__floor__()
  206. 10
  207. """
  208. pass
  209. def __format__(self, *args, **kwargs): # real signature unknown
  210. """ """
  211. """
  212. 转换对象的类型
  213. 例子:
  214. >>> a = 10
  215. >>> a.__format__(‘f‘)
  216. ‘10.000000‘
  217. >>> a.__format__(‘b‘)
  218. ‘1010‘
  219. """
  220. pass
  221. def __getattribute__(self, *args, **kwargs): # real signature unknown
  222. """ Return getattr(self, name). """
  223. pass
  224. def __getnewargs__(self, *args, **kwargs): # real signature unknown
  225. pass
  226. def __ge__(self, *args, **kwargs): # real signature unknown
  227. """ Return self>=value. """
  228. pass
  229. def __gt__(self, *args, **kwargs): # real signature unknown
  230. """ Return self>value. """
  231. pass
  232. def __hash__(self, *args, **kwargs): # real signature unknown
  233. """ Return hash(self). """
  234. pass
  235. def __index__(self, *args, **kwargs): # real signature unknown
  236. """ Return self converted to an integer, if self is suitable for use as an index into a list. """
  237. pass
  238. def __init__(self, x, base=10): # known special case of int.__init__
  239. """
  240. int(x=0) -> integer
  241. int(x, base=10) -> integer
  242. Convert a number or string to an integer, or return 0 if no arguments
  243. are given. If x is a number, return x.__int__(). For floating point
  244. numbers, this truncates towards zero.
  245. If x is not a number or if base is given, then x must be a string,
  246. bytes, or bytearray instance representing an integer literal in the
  247. given base. The literal can be preceded by ‘+‘ or ‘-‘ and be surrounded
  248. by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
  249. Base 0 means to interpret the base from the string as an integer literal.
  250. >>> int(‘0b100‘, base=0)
  251. 4
  252. # (copied from class doc)
  253. """
  254. pass
  255. def __int__(self, *args, **kwargs): # real signature unknown
  256. """ int(self) """
  257. pass
  258. def __invert__(self, *args, **kwargs): # real signature unknown
  259. """ ~self """
  260. pass
  261. def __le__(self, *args, **kwargs): # real signature unknown
  262. """ Return self<=value. """
  263. pass
  264. def __lshift__(self, *args, **kwargs): # real signature unknown
  265. """ Return self<<value. """
  266. pass
  267. def __lt__(self, *args, **kwargs): # real signature unknown
  268. """ Return self<value. """
  269. pass
  270. def __mod__(self, *args, **kwargs): # real signature unknown
  271. """ Return self%value. """
  272. pass
  273. def __mul__(self, *args, **kwargs): # real signature unknown
  274. """ Return self*value. """
  275. pass
  276. def __neg__(self, *args, **kwargs): # real signature unknown
  277. """ -self """
  278. pass
  279. @staticmethod # known case of __new__
  280. def __new__(*args, **kwargs): # real signature unknown
  281. """ Create and return a new object. See help(type) for accurate signature. """
  282. pass
  283. def __ne__(self, *args, **kwargs): # real signature unknown
  284. """ Return self!=value. """
  285. pass
  286. def __or__(self, *args, **kwargs): # real signature unknown
  287. """ Return self|value. """
  288. pass
  289. def __pos__(self, *args, **kwargs): # real signature unknown
  290. """ +self """
  291. pass
  292. def __pow__(self, *args, **kwargs): # real signature unknown
  293. """ Return pow(self, value, mod). """
  294. pass
  295. def __radd__(self, *args, **kwargs): # real signature unknown
  296. """ Return value+self. """
  297. pass
  298. def __rand__(self, *args, **kwargs): # real signature unknown
  299. """ Return value&self. """
  300. pass
  301. def __rdivmod__(self, *args, **kwargs): # real signature unknown
  302. """ Return divmod(value, self). """
  303. pass
  304. def __repr__(self, *args, **kwargs): # real signature unknown
  305. """ Return repr(self). """
  306. pass
  307. def __rfloordiv__(self, *args, **kwargs): # real signature unknown
  308. """ Return value//self. """
  309. pass
  310. def __rlshift__(self, *args, **kwargs): # real signature unknown
  311. """ Return value<<self. """
  312. pass
  313. def __rmod__(self, *args, **kwargs): # real signature unknown
  314. """ Return value%self. """
  315. pass
  316. def __rmul__(self, *args, **kwargs): # real signature unknown
  317. """ Return value*self. """
  318. pass
  319. def __ror__(self, *args, **kwargs): # real signature unknown
  320. """ Return value|self. """
  321. pass
  322. def __round__(self, *args, **kwargs): # real signature unknown
  323. """
  324. Rounding an Integral returns itself.
  325. Rounding with an ndigits argument also returns an integer.
  326. """
  327. pass
  328. def __rpow__(self, *args, **kwargs): # real signature unknown
  329. """ Return pow(value, self, mod). """
  330. pass
  331. def __rrshift__(self, *args, **kwargs): # real signature unknown
  332. """ Return value>>self. """
  333. pass
  334. def __rshift__(self, *args, **kwargs): # real signature unknown
  335. """ Return self>>value. """
  336. pass
  337. def __rsub__(self, *args, **kwargs): # real signature unknown
  338. """ Return value-self. """
  339. pass
  340. def __rtruediv__(self, *args, **kwargs): # real signature unknown
  341. """ Return value/self. """
  342. pass
  343. def __rxor__(self, *args, **kwargs): # real signature unknown
  344. """ Return value^self. """
  345. pass
  346. def __sizeof__(self, *args, **kwargs): # real signature unknown
  347. """ Returns size in memory, in bytes """
  348. pass
  349. def __str__(self, *args, **kwargs): # real signature unknown
  350. """ Return str(self). """
  351. pass
  352. def __sub__(self, *args, **kwargs): # real signature unknown
  353. """ Return self-value. """
  354. pass
  355. def __truediv__(self, *args, **kwargs): # real signature unknown
  356. """ Return self/value. """
  357. pass
  358. def __trunc__(self, *args, **kwargs): # real signature unknown
  359. """ Truncating an Integral returns itself. """
  360. pass
  361. def __xor__(self, *args, **kwargs): # real signature unknown
  362. """ Return self^value. """
  363. pass
  364. denominator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  365. """the denominator of a rational number in lowest terms"""
  366. imag = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  367. """the imaginary part of a complex number"""
  368. numerator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  369. """the numerator of a rational number in lowest terms"""
  370. real = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  371. """the real part of a complex number"""





python之路-基础篇-day2

标签:

原文地址:http://www.cnblogs.com/CongZhang/p/5128864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!