博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python进阶之路---1.4python数据类型-数字
阅读量:4319 次
发布时间:2019-06-06

本文共 11245 字,大约阅读时间需要 37 分钟。

python入门基础
声明:以后python代码未注明情况下,默认使用python3.x版本
1.python代码基础:print
    print('hello,python')
  1.1pythong内部执行原理:
    
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))

数值运算:

  

>>> 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)
>>> b = int(a)>>> type(b)
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 = -12print(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 = 12print(age)b = age.__add__(10)print(b)output:1222
def __and__(self, *args, **kwargs): # real signature unknown        """ Return self&value. """        pass用于进行两个数值的与运算age = 12print(age)b = age.__and__(4)print(b)output124用二进制与运算0000110000000100结果00000100换算成10进制为4
def __bool__(self, *args, **kwargs): # real signature unknown        """ self != 0 """        pass布尔型判断age = Trueprint(age)b = age.__bool__()print(b)output:TrueTrue
def __ceil__(self, *args, **kwargs): # real signature unknown        """ Ceiling of an Integral returns itself. """        pass返回数字的上入整数,如果数值是小数,则返回的数值是整数加一improt mathmath.ceil(4.1)output:5
def __divmod__(self, *args, **kwargs): # real signature unknown        """ Return divmod(self, value). """        pass数字相除,将商和余数返回一个数组,相当于 a//b ,返回(商,余数)age = 12print(age)b = age.__divmod__(5)print(b)print(type(b))outuput:12(2, 2)
def __eq__(self, *args, **kwargs): # real signature unknown        """ Return self==value. """        pass用于判断数值是否相等,等价于 a == bage = 12print(age)b = age.__eq__(5)print(b)output:12False
def __float__(self, *args, **kwargs): # real signature unknown        """ float(self) """        pass用于判断数值是否是,浮点型,即小数型age = 4.2print(age)b = age.__float__()print(type(b))output:4.2
def __floordiv__(self, *args, **kwargs): # real signature unknown        """ Return self//value. """        pass用于数字相除取其商,例如, 4//3 返回 1age = 4print(age)b = age.__floordiv__(3)print(b)output:41
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 > valueage = 4print(age)b = age.__ge__(2)print(b)print(type(b))output:4True
def __gt__(self, *args, **kwargs): # real signature unknown        """ Return self>value. """        pass大于等于,self  >= valueage = 4print(age)b = age.__gt__(2)print(b)print(type(b))output:4True
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<
def __lt__(self, *args, **kwargs): # real signature unknown        """ Return self
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(-10) 返回 10
返回数字的上入整数,如math.ceil(4.1) 返回 5
如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1
返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045
返回数字的绝对值,如math.fabs(-10) 返回10.0
返回数字的下舍整数,如math.floor(4.9)返回 4
如math.log(math.e)返回1.0,math.log(100,10)返回2.0
返回以10为基数的x的对数,如math.log10(100)返回 2.0
返回给定参数的最大值,参数可以为序列。
返回给定参数的最小值,参数可以为序列。
返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。
x**y 运算后的值。
返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。
返回数字x的平方根,数字可以为负数,返回类型为实数,如math.sqrt(4)返回 2+0j

python随机函数:

随机数可以用于数学,游戏,安全等领域中,还经常被嵌入到算法中,用以提高算法效率,并提高程序的安全性。

Python包含以下常用随机数函数:

函数 描述
从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。
从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1
随机生成下一个实数,它在[0,1)范围内。
改变随机数生成器的种子seed。如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed。
将序列的所有元素随机排序
随机生成下一个实数,它在[x,y]范围内。

Python三角函数:

函数 描述
返回x的反余弦弧度值。
返回x的反正弦弧度值。  
返回x的反正切弧度值。
返回给定的 X 及 Y 坐标值的反正切值。
返回x的弧度的余弦值。
返回欧几里德范数 sqrt(x*x + y*y)。
返回的x弧度的正弦值。
返回x弧度的正切值。
将弧度转换为角度,如degrees(math.pi/2) , 返回90.0
将角度转换为弧度

Python数学常量:

常量 描述
pi 数学常量 pi(圆周率,一般以π来表示)
e 数学常量 e,e即自然常数(自然常数)。

注意:

  • 1、Python可以同时为多个变量赋值,如a, b = 1, 2。
  • 2、一个变量可以通过赋值指向不同类型的对象。
  • 3、数值的除法(/)总是返回一个浮点数,要获取整数使用//操作符。
  • 4、在混合计算时,Python会把整型转换成为浮点数。
 
 
 
 
 
 
 
 
 
 
 
 
 
 

转载于:https://www.cnblogs.com/lixingli/p/9fcc9e0ad365d9dbf28f728e0c38a39d.html

你可能感兴趣的文章
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
学习进度
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>