Python基本数据类型(数字)
⼀、概述
Python中的数字类型主要包括五种,分别是整型、长整型、浮点型、复数型和布尔型。
⼆、整型
整型(Integer)是最基本的数字类型,⽀持加减乘除运算。除了加减乘除,还可以⽤“%”进⾏取余操作,⽤“**”进⾏指数操作,⽤“//”进⾏整数除法操作
v = 2 + 2
print(v)#结果为:4
三、长整型
当整型超出最⼤最⼩值的范围时,Python会⾃动将超出范围的整数转化为长整型(Long Integer)
四、浮点型
为了让除法12/5返回结果是2.4⽽不是2,我们可以使⽤浮点型进⾏计算:
v = 12.0/5.0
print(v)      #结果为:2.4
浮点型与整型的运算结果还是浮点型。浮点数还⽀持整数除法。在Python中,整数除法是⼀种特殊的除法,⽤“//”表⽰,返回的是⽐实际结果⼩的最⼤整数值:v = 12.3 // -4
print(v)        #结果为:-4.0
五、复数型
复数型是表⽰复数的类型,定义时,Python使⽤字母j来表⽰复数的虚部:
v = 1+2j
a = v.real
b = v.imag
print(a,b)      #结果为:1.0  2.0
六、布尔型
布尔型可以看成是⼀种取值为Ture和False的⼆值变量,分别对应逻辑上的真和假。
布尔型变量可以使⽤⽐较表达式得到:
v = 1>2
print(v)    #结果为:False
常⽤的⽐较符号包括⼩于“<”、⼤于“>”、⼩于或等于“<=” 、⼤于或等于“>=”、不等于“!=”等。
七、运算优先级
Python中各种运算也有⼀定的优先顺序,优先级从⾼到低排序如下:
(),括号
**,乘幂运算
*、/、//、%,乘、除、整数除法、取余
+、-,加、减
⼋、原地运算
Python⽀持原地运算的操作,其形式如下:
v = 2.5
v += 1
print(v)#结果为:3.5
九、数字函数
Python提供了⼀些简单的数学函数对数字进⾏处理。例如,求绝对值(abs)、四舍五⼊取整(round)、最⼤值(max)、最⼩值(min)等。
⼗、类型转换
不同类型的数字间可以进⾏类型转换。
int()函数可以将浮点型转化为整型,但只保留整数部分。
整型转浮点型的函数为float()
整型、浮点型转长整型的函数为long()
整型、浮点型转复数型的函数为complex()
⼗⼀、整型的其他表⽰
通常整型的表⽰是以⼗进制为基础的。在计算机科学中,还存在其他进制的表⽰⽅法,如⼆进制、⼋进制和⼗六进制。
Python中的⼆进制数字以0b开头、⼋进制数字以0或者0o开头、⼗六进制数字以0x开头
⼗⼆、整型的⽅法
最常⽤的⽅法为int(),可将字符串转换为数字:
a = "123"
b = int(a)
print(b)    #结果为:123
整型所有⽅法归纳:
1class int(object):
2"""
3    int(x=0) -> int or long
4    int(x, base=10) -> int or long
5
6    Convert a number or string to an integer, or return 0 if no arguments
7    are given.  If x is floating point, the conversion truncates towards zero.
8    If x is outside the integer range, the function returns a long instead.
9
10    If x is not a number or if base is given, then x must be a string or
11    Unicode object representing an integer literal in the given base.  The
12    literal can be preceded by '+' or '-' and be surrounded by whitespace.
13    The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
14    interpret the base from the string as an integer literal.
15    >>> int('0b100', base=0)
16"""
17def bit_length(self):
18""" 返回表⽰该数字的时占⽤的最少位数 """
19"""
20        int.bit_length() -> int
21
22        Number of bits necessary to represent self in binary.
23        >>> bin(37)
24        '0b100101'
25        >>> (37).bit_length()
26"""
27return 0
28
29def conjugate(self, *args, **kwargs): # real signature unknown
30""" 返回该复数的共轭复数 """
31""" Returns self, the complex conjugate of any int. """
32pass
33
34def__abs__(self):
35""" 返回绝对值 """
36""" x.__abs__() <==> abs(x) """
37pass
38
39def__add__(self, y):
40""" x.__add__(y) <==> x+y """
41pass
42
43def__and__(self, y):
44""" x.__and__(y) <==> x&y """
45pass
46
47def__cmp__(self, y):
48""" ⽐较两个数⼤⼩ """
49""" x.__cmp__(y) <==> cmp(x,y) """
50pass
51
52def__coerce__(self, y):
53""" 强制⽣成⼀个元组 """
54""" x.__coerce__(y) <==> coerce(x, y) """
55pass
56
57def__divmod__(self, y):
58""" 相除,得到商和余数组成的元组 """
59""" x.__divmod__(y) <==> divmod(x, y) """
60pass
61
62def__div__(self, y):
63""" x.__div__(y) <==> x/y """
64pass
65
66def__float__(self):
67""" 转换为浮点类型 """
68""" x.__float__() <==> float(x) """
69pass
70
71def__floordiv__(self, y):
72""" x.__floordiv__(y) <==> x//y """
73pass
74
75def__format__(self, *args, **kwargs): # real signature unknown
76pass
77
78def__getattribute__(self, name):
float()函数79""" x.__getattribute__('name') <==> x.name """
80pass
81
82def__getnewargs__(self, *args, **kwargs): # real signature unknown  83""" 内部调⽤ __new__⽅法或创建对象时传⼊参数使⽤ """
84pass
85
86def__hash__(self):
87"""如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查中,哈希值⽤于快速⽐较字典的键。两个数值如果相等,则哈希值也相等。""" 88""" x.__hash__() <==> hash(x) """
89pass
90
91def__hex__(self):
92""" 返回当前数的⼗六进制表⽰ """
93""" x.__hex__() <==> hex(x) """
94pass
95
96def__index__(self):
97""" ⽤于切⽚,数字⽆意义 """
98""" x[y:z] <==> x[y.__index__():z.__index__()] """
99pass
100
101def__init__(self, x, base=10): # known special case of int.__init__
102""" 构造⽅法,执⾏ x = 123 或 x = int(10) 时,⾃动调⽤,暂时忽略 """
103"""
104        int(x=0) -> int or long
105        int(x, base=10) -> int or long
106
107        Convert a number or string to an integer, or return 0 if no arguments
108        are given.  If x is floating point, the conversion truncates towards zero.
109        If x is outside the integer range, the function returns a long instead.
110
111        If x is not a number or if base is given, then x must be a string or
112        Unicode object representing an integer literal in the given base.  The
113        literal can be preceded by '+' or '-' and be surrounded by whitespace.
114        The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
115        interpret the base from the string as an integer literal.
116        >>> int('0b100', base=0)
117        # (copied from class doc)
118"""
119pass
120
121def__int__(self):
122""" 转换为整数 """
123""" x.__int__() <==> int(x) """
124pass
125
126def__invert__(self):
127""" x.__invert__() <==> ~x """
128pass
129
130def__long__(self):
131""" 转换为长整数 """
132""" x.__long__() <==> long(x) """
133pass
134
135def__lshift__(self, y):
136""" x.__lshift__(y) <==> x<<y """
137pass
138
139def__mod__(self, y):
140""" x.__mod__(y) <==> x%y """
141pass
142
143def__mul__(self, y):
144""" x.__mul__(y) <==> x*y """
145pass
146
147def__neg__(self):
148""" x.__neg__() <==> -x """
149pass
150
151    @staticmethod # known case of __new__
152def__new__(S, *more):
153""" T.__new__(S, ...) -> a new object with type S, a subtype of T """
154pass
155
156def__nonzero__(self):
157""" x.__nonzero__() <==> x != 0 """
158pass
159
160def__oct__(self):
161""" 返回改值的⼋进制表⽰ """
162""" x.__oct__() <==> oct(x) """
163pass
164
165def__or__(self, y):
166""" x.__or__(y) <==> x|y """
167pass
168
169def__pos__(self):
170""" x.__pos__() <==> +x """
171pass
172
173def__pow__(self, y, z=None):
174""" 幂,次⽅ """
175""" x.__pow__(y[, z]) <==> pow(x, y[, z]) """
176pass
177
178def__radd__(self, y):
179""" x.__radd__(y) <==> y+x """
180pass
181
182def__rand__(self, y):
183""" x.__rand__(y) <==> y&x """
184pass
185
186def__rdivmod__(self, y):
187""" x.__rdivmod__(y) <==> divmod(y, x) """
188pass
189
190def__rdiv__(self, y):
191""" x.__rdiv__(y) <==> y/x """
192pass
193
194def__repr__(self):
195"""转化为解释器可读取的形式 """
196""" x.__repr__() <==> repr(x) """
197pass
198
199def__str__(self):
200"""转换为⼈阅读的形式,如果没有适于⼈阅读的解释形式的话,则返回解释器课阅读的形式""" 201""" x.__str__() <==> str(x) """
202pass
203
204def__rfloordiv__(self, y):
205""" x.__rfloordiv__(y) <==> y//x """
206pass
207
208def__rlshift__(self, y):
209""" x.__rlshift__(y) <==> y<<x """
210pass
211
212def__rmod__(self, y):
213""" x.__rmod__(y) <==> y%x """
214pass
215
216def__rmul__(self, y):
217""" x.__rmul__(y) <==> y*x """
218pass
219
220def__ror__(self, y):
221""" x.__ror__(y) <==> y|x """
222pass
223
224def__rpow__(self, x, z=None):
225""" y.__rpow__(x[, z]) <==> pow(x, y[, z]) """
226pass
227
228def__rrshift__(self, y):
229""" x.__rrshift__(y) <==> y>>x """
230pass
231
232def__rshift__(self, y):
233""" x.__rshift__(y) <==> x>>y """
234pass
235
236def__rsub__(self, y):
237""" x.__rsub__(y) <==> y-x """
238pass
239
240def__rtruediv__(self, y):
241""" x.__rtruediv__(y) <==> y/x """
242pass
243
244def__rxor__(self, y):
245""" x.__rxor__(y) <==> y^x """
246pass
247
248def__sub__(self, y):
249""" x.__sub__(y) <==> x-y """
250pass
251
252def__truediv__(self, y):
253""" x.__truediv__(y) <==> x/y """
254pass
255
256def__trunc__(self, *args, **kwargs):
257""" 返回数值被截取为整形的值,在整形中⽆意义 """
258pass
259
260def__xor__(self, y):
261""" x.__xor__(y) <==> x^y """
262pass
263
264    denominator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default 265""" 分母 = 1 """
266"""the denominator of a rational number in lowest terms"""
267
268    imag = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default 269""" 虚数,⽆意义 """
270"""the imaginary part of a complex number"""
271
272    numerator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default 273""" 分⼦ = 数字⼤⼩ """
274"""the numerator of a rational number in lowest terms"""
275
276    real = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default 277""" 实属,⽆意义 """
278"""the real part of a complex number"""
279
280 int
int