python内置方法、模块讲解(十五)

  • 2016-03-14 22:35:53
  • 1125
  • 0

四十一、int类

1、定义:

class int(object)
 |  int(x=0) -> int or long
 |  int(x, base=10) -> int or long
 |
 |  Convert a number or string to an integer, or return 0 if no arguments
 |  are given.  If x is floating point, the conversion truncates towards zero.
 |  If x is outside the integer range, the function returns a long instead.
 |
 |  If x is not a number or if base is given, then x must be a string or
 |  Unicode object representing an integer literal in the given base.  The
 |  literal can be preceded by '+' or '-' and be surrounded by whitespace.
 |  The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
 |  interpret the base from the string as an integer literal.
 |  >>> int('0b100', base=0)
 |  4

2、解释:将一个数字或者字符串转换成整型,若未传入参数时,返回0。此类用法比较多,具体见举例。

3、举例:

>>> int('10')
10
>>> int(10.98)
10
>>> int()
0
>>> int('0b100', base=0)
4
>>> int('0x100', base=0)
256

四十二、intern方法

(以下省略)

阅读更多

python内置方法、模块讲解(十四)

  • 2016-03-13 11:49:44
  • 1137
  • 0

三十九、input方法

1、定义:

input(...)
    input([prompt]) -> value

    Equivalent to eval(raw_input(prompt)).

2、解释:等价于:eval(raw_input(prompt)),因此,下一个介绍raw_input

3、举例:

>>> aa = input('请输入内容或python表达式:')
请输入内容或python表达式:range(5)
>>> aa
[0, 1, 2, 3, 4]
>>> aa = input('请输入内容或python表达式:')
请输入内容或python表达式:abc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
>>> aa = input('请输入内容或python表达式:')
请输入内容或python表达式:'anc'
>>> aa
'anc'

四十、raw_input方法

(以下省略)

阅读更多

python内置方法、模块讲解(十三)

  • 2016-03-13 11:38:57
  • 1097
  • 0

三十七、hex方法

1、定义:

hex(...)
    hex(number) -> string

    Return the hexadecimal representation of an integer or long integer.

2、解释:以字符串的方式返回一个number的16进制形式

3、举例:

>>> hex(16)
'0x10'
>>> hex(10)
'0xa'
>>> hex(2)
'0x2'

三十八、id方法

(以下省略)

阅读更多