Python进阶之路---1.4python数据类型-数字
python入门基础
声明:以后python代码未注明情况下,默认使用python3.x版本
1.python代码基础:print
print('hello,python')
1.1pythong内部执行原理:
![](https://images2015.cnblogs.com/blog/866986/201601/866986-20160111162030772-638658312.jpg)
2.解释器
python 中.py的文件需要使用python的解释器执行并生成.pyc的字节码文件,在第二次执行.py文件时会直接执行已经存在的.pyc文件,如果.py文件改变,解释器会重新执行.py文件从而更新.pyc文件。
在linux中如果执行python 的文件需要使用 chmod +x hello.py 才可以执行 python hello.py文件,如果想像shell一样执行hello.py文件(./hello.py)就要在hello.py文件中声明解释器,可以在文件开头添加 #!/usr/bin/python 或者 #!/usr/bin/env python,第一种是直接指定python解释器的位置,第二种是根据系统变量去寻找python解释器的位置,建议使用第二种写法。
3.python工作原理
python.py ==> python.pyc(python解释器执行生成) ==》0101001(机器码,cpu可以直接执行)
4.变量
python中的变量不需要声明,变量的赋值操作就是变量声明和定义的过程。
每个变量都会在内存中创建,都包括变量的表示,名称和数据这些信息。
每个变量都会被赋值变量赋值后才会被创建。
等号 (=)用来给变量赋值。左边是变量名称,右边是变量的赋值,例如:
>>> ty =100 >>>print(ty) 100
5.数据类型-数字
在python中,变量就是变量,他没有类型,我们所说的“类型”是变量所指的内存中对象的类型。
python中有六个标准的数据类型:
Numbers (数字) String(字符串) List(列表) Tuple(元祖) sets(集合) Disctionaries(字典)
>>> a, b, c, d = 20, 5.5, True, 4+3j >>> print(type(a), type(b), type(c), type(d)) <class 'int'> <class 'float'> <class 'bool'> <class 'complex'>
数值运算:
>>> 5 + 4 # 加法 9 >>> 4.3 - 2 # 减法 2.3 >>> 3 * 7 # 乘法 21 >>> 2 / 4 # 除法,得到一个浮点数 0.5 >>> 2 // 4 # 除法,得到一个整数,取商 0 >>> 17 % 3 # 取余 2 >>> 2 ** 5 # 乘方 32
数字常用的函数方法:
class int(object):
"""
int(x=0) -> integer
int(x, base=10) -> integer
将对象转换成字符类型
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance 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
"""
>>> a = "2" >>> type(a) <class 'str'> >>> b = int(a) >>> type(b) <class 'int'>
def bit_length(self): # real signature unknown; restored from __doc__ """ int.bit_length() -> int Number of bits necessary to represent self in binary. >>> bin(37) '0b100101' >>> (37).bit_length() 6 """ return 0
def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ int.from_bytes(bytes, byteorder, *, signed=False) -> int Return the integer represented by the given array of bytes. The bytes argument must be a bytes-like object (e.g. bytes or bytearray). The byteorder argument determines the byte order used to represent the integer. If byteorder is 'big', the most significant byte is at the beginning of the byte array. If byteorder is 'little', the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder' as the byte order value. The signed keyword-only argument indicates whether two's complement is used to represent the integer. """ pass def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ int.to_bytes(length, byteorder, *, signed=False) -> bytes Return an array of bytes representing an integer. The integer is represented using length bytes. An OverflowError is raised if the integer is not representable with the given number of bytes. The byteorder argument determines the byte order used to represent the integer. If byteorder is 'big', the most significant byte is at the beginning of the byte array. If byteorder is 'little', the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder' as the byte order value. The signed keyword-only argument determines whether two's complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised. """ pass
def __abs__(self, *args, **kwargs): # real signature unknown """ abs(self) """ pass
用于获取数字的绝对值 age = -12 print(age) b = age.__abs__() print(b)
output:
-12
12
def conjugate(self, *args, **kwargs): # real signature unknown """ Returns self, the complex conjugate of any int. """ pass @classmethod # known case
def __add__(self, *args, **kwargs): # real signature unknown """ Return self+value. """ pass 用户两个数字求和: age = 12 print(age) b = age.__add__(10) print(b) output: 12 22
def __and__(self, *args, **kwargs): # real signature unknown """ Return self&value. """ pass 用于进行两个数值的与运算 age = 12 print(age) b = age.__and__(4) print(b) output 12 4 用二进制与运算 00001100 00000100 结果 00000100 换算成10进制为 4
def __bool__(self, *args, **kwargs): # real signature unknown """ self != 0 """ pass 布尔型判断 age = True print(age) b = age.__bool__() print(b) output: True True
def __ceil__(self, *args, **kwargs): # real signature unknown """ Ceiling of an Integral returns itself. """ pass 返回数字的上入整数,如果数值是小数,则返回的数值是整数加一 improt math math.ceil(4.1) output: 5
def __divmod__(self, *args, **kwargs): # real signature unknown """ Return divmod(self, value). """ pass 数字相除,将商和余数返回一个数组,相当于 a//b ,返回(商,余数) age = 12 print(age) b = age.__divmod__(5) print(b) print(type(b)) outuput: 12 (2, 2) <class 'tuple'>
def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass 用于判断数值是否相等,等价于 a == b age = 12 print(age) b = age.__eq__(5) print(b) output: 12 False
def __float__(self, *args, **kwargs): # real signature unknown """ float(self) """ pass 用于判断数值是否是,浮点型,即小数型 age = 4.2 print(age) b = age.__float__() print(type(b)) output: 4.2 <class 'float'>
def __floordiv__(self, *args, **kwargs): # real signature unknown """ Return self//value. """ pass 用于数字相除取其商,例如, 4//3 返回 1 age = 4 print(age) b = age.__floordiv__(3) print(b) output: 4 1
def __floor__(self, *args, **kwargs): # real signature unknown """ Flooring an Integral returns itself. """ pass
def __format__(self, *args, **kwargs): # real signature unknown pass
def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass
def __getnewargs__(self, *args, **kwargs): # real signature unknown pass
def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass 数字大小判断, self > value age = 4 print(age) b = age.__ge__(2) print(b) print(type(b)) output: 4 True <class 'bool'>
def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass 大于等于,self >= value age = 4 print(age) b = age.__gt__(2) print(b) print(type(b)) output: 4 True <class 'bool'>
def __index__(self, *args, **kwargs): # real signature unknown """ Return self converted to an integer, if self is suitable for use as an index into a list. """ pass
用于切片,数字无意义
def __init__(self, x, base=10): # known special case of int.__init__ """ 构造方法,执行 x = 123 或 x = int(10) 时,自动调用,暂时忽略 """ """ 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 # (copied from class doc) """ pass
def __int__(self): """ 转换为整数 """ """ x.__int__() <==> int(x) """ pass
def __invert__(self): """ x.__invert__() <==> ~x """ pass
def __le__(self, *args, **kwargs): # real signature unknown """ Return self<=value. """ pass 小于等于
def __lshift__(self, *args, **kwargs): # real signature unknown """ Return self<<value. """ pass
def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ pass 小于
def __mod__(self, *args, **kwargs): # real signature unknown """ Return self%value. """ pass 取余数
def __mul__(self, *args, **kwargs): # real signature unknown """ Return self*value. """ pass 乘积
def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ pass 不等于
def __or__(self, *args, **kwargs): # real signature unknown """ Return self|value. """ pass 或
def __pos__(self, *args, **kwargs): # real signature unknown """ +self """ pass
def __pow__(self, *args, **kwargs): # real signature unknown """ Return pow(self, value, mod). """ pass 幂
def __radd__(self, *args, **kwargs): # real signature unknown """ Return value+self. """ pass +
def __repr__(self): """转化为解释器可读取的形式 """ """ x.__repr__() <==> repr(x) """ pass def __str__(self): """转换为人阅读的形式,如果没有适于人阅读的解释形式的话,则返回解释器课阅读的形式""" """ x.__str__() <==> str(x) """ pass
def __trunc__(self, *args, **kwargs): """ 返回数值被截取为整形的值,在整形中无意义 """ pass
数值类型:
整型(int)-通常被称为是整型或整数,是正或负整数,不带小数点。
长整型(long integers)-无限大小的整数,整数最后是一个大写或者小写的L
浮点型(floadting point real values)-浮点型由整数部分与小数部分组成,也可以使用科学计数法表示
复数(complex numbers)-复数的虚部以字母J或j结尾。如2+3i
int | long | float | complex |
---|---|---|---|
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
- 长整型也可以使用小写"L",但是还是建议您使用大写"L",避免与数字"1"混淆。Python使用"L"来显示长整型。
- Python还支持复数,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型
Python数学函数:
函数 | 返回值 ( 描述 ) |
---|---|
abs(x) | 返回数字的绝对值,如abs(-10) 返回 10 |
ceil(x) | 返回数字的上入整数,如math.ceil(4.1) 返回 5 |
cmp(x, y) | 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1 |
exp(x) | 返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045 |
fabs(x) | 返回数字的绝对值,如math.fabs(-10) 返回10.0 |
floor(x) | 返回数字的下舍整数,如math.floor(4.9)返回 4 |
log(x) | 如math.log(math.e)返回1.0,math.log(100,10)返回2.0 |
log10(x) | 返回以10为基数的x的对数,如math.log10(100)返回 2.0 |
max(x1, x2,...) | 返回给定参数的最大值,参数可以为序列。 |
min(x1, x2,...) | 返回给定参数的最小值,参数可以为序列。 |
modf(x) | 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。 |
pow(x, y) | x**y 运算后的值。 |
round(x [,n]) | 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。 |
sqrt(x) | 返回数字x的平方根,数字可以为负数,返回类型为实数,如math.sqrt(4)返回 2+0j |
python随机函数:
随机数可以用于数学,游戏,安全等领域中,还经常被嵌入到算法中,用以提高算法效率,并提高程序的安全性。
Python包含以下常用随机数函数:
函数 | 描述 |
---|---|
choice(seq) | 从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。 |
randrange ([start,] stop [,step]) | 从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1 |
random() | 随机生成下一个实数,它在[0,1)范围内。 |
seed([x]) | 改变随机数生成器的种子seed。如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed。 |
shuffle(lst) | 将序列的所有元素随机排序 |
uniform(x, y) | 随机生成下一个实数,它在[x,y]范围内。 |
Python三角函数:
函数 | 描述 | |
---|---|---|
acos(x) | 返回x的反余弦弧度值。 | |
asin(x) | 返回x的反正弦弧度值。 | |
atan(x) | 返回x的反正切弧度值。 | |
atan2(y, x) | 返回给定的 X 及 Y 坐标值的反正切值。 | |
cos(x) | 返回x的弧度的余弦值。 | |
hypot(x, y) | 返回欧几里德范数 sqrt(x*x + y*y)。 | |
sin(x) | 返回的x弧度的正弦值。 | |
tan(x) | 返回x弧度的正切值。 | |
degrees(x) | 将弧度转换为角度,如degrees(math.pi/2) , 返回90.0 | |
radians(x) | 将角度转换为弧度 |
Python数学常量:
常量 | 描述 |
---|---|
pi | 数学常量 pi(圆周率,一般以π来表示) |
e | 数学常量 e,e即自然常数(自然常数)。 |
注意:
- 1、Python可以同时为多个变量赋值,如a, b = 1, 2。
- 2、一个变量可以通过赋值指向不同类型的对象。
- 3、数值的除法(/)总是返回一个浮点数,要获取整数使用//操作符。
- 4、在混合计算时,Python会把整型转换成为浮点数。
python, equal to anything!