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中的变量不需要声明,变量的赋值操作就是变量声明和定义的过程。
每个变量都会在内存中创建,都包括变量的表示,名称和数据这些信息。
每个变量都会被赋值变量赋值后才会被创建。
等号 (=)用来给变量赋值。左边是变量名称,右边是变量的赋值,例如:
  1. >>> ty =100
  2. >>>print(ty)
  3. 100

5.数据类型-数字

在python中,变量就是变量,他没有类型,我们所说的“类型”是变量所指的内存中对象的类型。

python中有六个标准的数据类型:

Numbers (数字) String(字符串) List(列表) Tuple(元祖) sets(集合) Disctionaries(字典)

  1. >>> a, b, c, d = 20, 5.5, True, 4+3j
  2. >>> print(type(a), type(b), type(c), type(d))
  3. <class 'int'> <class 'float'> <class 'bool'> <class 'complex'>

数值运算:

  

  1. >>> 5 + 4 # 加法
  2. 9
  3. >>> 4.3 - 2 # 减法
  4. 2.3
  5. >>> 3 * 7 # 乘法
  6. 21
  7. >>> 2 / 4 # 除法,得到一个浮点数
  8. 0.5
  9. >>> 2 // 4 # 除法,得到一个整数,取商
  10. 0
  11. >>> 17 % 3 # 取余
  12. 2
  13. >>> 2 ** 5 # 乘方
  14. 32

数字常用的函数方法:

  1. 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.
  2.  
  3. 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
    """
  1. >>> a = ""
  2. >>> type(a)
  3. <class 'str'>
  4. >>> b = int(a)
  5. >>> type(b)
  6. <class 'int'>
  1. def bit_length(self): # real signature unknown; restored from __doc__
  2. """
  3. int.bit_length() -> int
  4.  
  5. Number of bits necessary to represent self in binary.
  6. >>> bin(37)
  7. '0b100101'
  8. >>> (37).bit_length()
  9. 6
  10. """
  11. return 0
  1. def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
  2. """
  3. int.from_bytes(bytes, byteorder, *, signed=False) -> int
  4.  
  5. Return the integer represented by the given array of bytes.
  6.  
  7. The bytes argument must be a bytes-like object (e.g. bytes or bytearray).
  8.  
  9. The byteorder argument determines the byte order used to represent the
  10. integer. If byteorder is 'big', the most significant byte is at the
  11. beginning of the byte array. If byteorder is 'little', the most
  12. significant byte is at the end of the byte array. To request the native
  13. byte order of the host system, use `sys.byteorder' as the byte order value.
  14.  
  15. The signed keyword-only argument indicates whether two's complement is
  16. used to represent the integer.
  17. """
  18. pass
  19.  
  20. def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
  21. """
  22. int.to_bytes(length, byteorder, *, signed=False) -> bytes
  23.  
  24. Return an array of bytes representing an integer.
  25.  
  26. The integer is represented using length bytes. An OverflowError is
  27. raised if the integer is not representable with the given number of
  28. bytes.
  29.  
  30. The byteorder argument determines the byte order used to represent the
  31. integer. If byteorder is 'big', the most significant byte is at the
  32. beginning of the byte array. If byteorder is 'little', the most
  33. significant byte is at the end of the byte array. To request the native
  34. byte order of the host system, use `sys.byteorder' as the byte order value.
  35.  
  36. The signed keyword-only argument determines whether two's complement is
  37. used to represent the integer. If signed is False and a negative integer
  38. is given, an OverflowError is raised.
  39. """
  40. pass
  1. def __abs__(self, *args, **kwargs): # real signature unknown
  2. """ abs(self) """
  3. pass
    用于获取数字的绝对值
  4.  
  5. age = -12
  6. print(age)
  7. b = age.__abs__()
  8. print(b)
  9.  
  10. output

  -12
  12

  1. def conjugate(self, *args, **kwargs): # real signature unknown
  2. """ Returns self, the complex conjugate of any int. """
  3. pass
  4.  
  5. @classmethod # known case
  1. def __add__(self, *args, **kwargs): # real signature unknown
  2. """ Return self+value. """
  3. pass
  4.  
  5. 用户两个数字求和:
  6. age = 12
  7. print(age)
  8. b = age.__add__(10)
  9. print(b)
  10.  
  11. output
  12. 12
  13. 22
  1. def __and__(self, *args, **kwargs): # real signature unknown
  2. """ Return self&value. """
  3. pass
  4. 用于进行两个数值的与运算
  5. age = 12
  6. print(age)
  7. b = age.__and__(4)
  8. print(b)
  9.  
  10. output
  11. 12
  12. 4
  13. 用二进制与运算
  14. 00001100
  15. 00000100
  16. 结果
  17. 00000100
  18. 换算成10进制为
  19. 4
  1. def __bool__(self, *args, **kwargs): # real signature unknown
  2. """ self != 0 """
  3. pass
  4. 布尔型判断
  5. age = True
  6. print(age)
  7. b = age.__bool__()
  8. print(b)
  9.  
  10. output:
  11. True
  12. True
  1. def __ceil__(self, *args, **kwargs): # real signature unknown
  2. """ Ceiling of an Integral returns itself. """
  3. pass
  4. 返回数字的上入整数,如果数值是小数,则返回的数值是整数加一
  5. improt math
  6. math.ceil(4.1)
  7.  
  8. output:
  9. 5
  1. def __divmod__(self, *args, **kwargs): # real signature unknown
  2. """ Return divmod(self, value). """
  3. pass
  4. 数字相除,将商和余数返回一个数组,相当于 a//b ,返回(商,余数)
  5. age = 12
  6. print(age)
  7. b = age.__divmod__(5)
  8. print(b)
  9. print(type(b))
  10.  
  11. outuput
  12. 12
  13. (2, 2)
  14. <class 'tuple'>
  1. def __eq__(self, *args, **kwargs): # real signature unknown
  2. """ Return self==value. """
  3. pass
  4. 用于判断数值是否相等,等价于 a == b
  5. age = 12
  6. print(age)
  7. b = age.__eq__(5)
  8. print(b)
  9.  
  10. output:
  11. 12
  12. False
  1. def __float__(self, *args, **kwargs): # real signature unknown
  2. """ float(self) """
  3. pass
  4. 用于判断数值是否是,浮点型,即小数型
  5. age = 4.2
  6. print(age)
  7. b = age.__float__()
  8. print(type(b))
  9.  
  10. output:
  11. 4.2
  12. <class 'float'>
  1. def __floordiv__(self, *args, **kwargs): # real signature unknown
  2. """ Return self//value. """
  3. pass
  4. 用于数字相除取其商,例如, 4//3 返回 1
  5. age = 4
  6. print(age)
  7. b = age.__floordiv__(3)
  8. print(b)
  9.  
  10. output:
  11. 4
  12. 1
  1. def __floor__(self, *args, **kwargs): # real signature unknown
  2. """ Flooring an Integral returns itself. """
  3. pass
  1. def __format__(self, *args, **kwargs): # real signature unknown
  2. pass
  1. def __getattribute__(self, *args, **kwargs): # real signature unknown
  2. """ Return getattr(self, name). """
  3. pass
  1. def __getnewargs__(self, *args, **kwargs): # real signature unknown
  2. pass
  1. def __ge__(self, *args, **kwargs): # real signature unknown
  2. """ Return self>=value. """
  3. pass
  4. 数字大小判断, self > value
  5. age = 4
  6. print(age)
  7. b = age.__ge__(2)
  8. print(b)
  9. print(type(b))
  10.  
  11. output:
  12. 4
  13. True
  14. <class 'bool'>
  1. def __gt__(self, *args, **kwargs): # real signature unknown
  2. """ Return self>value. """
  3. pass
  4. 大于等于,self >= value
  5. age = 4
  6. print(age)
  7. b = age.__gt__(2)
  8. print(b)
  9. print(type(b))
  10.  
  11. output:
  12. 4
  13. True
  14. <class 'bool'>
  1. def __index__(self, *args, **kwargs): # real signature unknown
  2. """ Return self converted to an integer, if self is suitable for use as an index into a list. """
  3. pass
    用于切片,数字无意义
  1. def __init__(self, x, base=10): # known special case of int.__init__
  2. """ 构造方法,执行 x = 123 或 x = int(10) 时,自动调用,暂时忽略 """
  3. """
  4. int(x=0) -> int or long
  5. int(x, base=10) -> int or long
  6.  
  7. Convert a number or string to an integer, or return 0 if no arguments
  8. are given. If x is floating point, the conversion truncates towards zero.
  9. If x is outside the integer range, the function returns a long instead.
  10.  
  11. If x is not a number or if base is given, then x must be a string or
  12. Unicode object representing an integer literal in the given base. The
  13. literal can be preceded by '+' or '-' and be surrounded by whitespace.
  14. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to
  15. interpret the base from the string as an integer literal.
  16. >>> int('0b100', base=0)
  17. 4
  18. # (copied from class doc)
  19. """
  20. pass
  1. def __int__(self):
  2. """ 转换为整数 """
  3. """ x.__int__() <==> int(x) """
  4. pass
  1. def __invert__(self):
  2. """ x.__invert__() <==> ~x """
  3. pass
  1. def __le__(self, *args, **kwargs): # real signature unknown
  2. """ Return self<=value. """
  3. pass
  4. 小于等于
  1. def __lshift__(self, *args, **kwargs): # real signature unknown
  2. """ Return self<<value. """
  3. pass
  1. def __lt__(self, *args, **kwargs): # real signature unknown
  2. """ Return self<value. """
  3. pass
  4.  
  5. 小于
  1. def __mod__(self, *args, **kwargs): # real signature unknown
  2. """ Return self%value. """
  3. pass
  4. 取余数
  1. def __mul__(self, *args, **kwargs): # real signature unknown
  2. """ Return self*value. """
  3. pass
  4. 乘积
  1. def __ne__(self, *args, **kwargs): # real signature unknown
  2. """ Return self!=value. """
  3. pass
  4. 不等于
  1. def __or__(self, *args, **kwargs): # real signature unknown
  2. """ Return self|value. """
  3. pass
  1. def __pos__(self, *args, **kwargs): # real signature unknown
  2. """ +self """
  3. pass
  1. def __pow__(self, *args, **kwargs): # real signature unknown
  2. """ Return pow(self, value, mod). """
  3. pass
  4.  
  1. def __radd__(self, *args, **kwargs): # real signature unknown
  2. """ Return value+self. """
  3. pass
  4. +
  1. def __repr__(self):
  2. """转化为解释器可读取的形式 """
  3. """ x.__repr__() <==> repr(x) """
  4. pass
  5.  
  6. def __str__(self):
  7. """转换为人阅读的形式,如果没有适于人阅读的解释形式的话,则返回解释器课阅读的形式"""
  8. """ x.__str__() <==> str(x) """
  9. pass
  1. def __trunc__(self, *args, **kwargs):
  2. """ 返回数值被截取为整形的值,在整形中无意义 """
  3. 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进阶之路---1.4python数据类型-数字的更多相关文章

  1. Python进阶之路---1.5python数据类型-字符串

    字符串 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; ...

  2. python精进之路1---基础数据类型

    python精进之路1---基本数据类型 python的基本数据类型如上图,重点需要掌握字符串.列表和字典. 一.int.float类型 int主要是用于整数类型计算,float主要用于小数. int ...

  3. Python进阶之路---1.2python版本差异

    Python2.*与python3.*版本差异 作为一个初学者,我们应该如何选择python的版本进行学习呢,这两个版本有什么区别呢,接下来让我们简单了解一下,以便我们后续的学习. Python版本差 ...

  4. python进阶之路之文件处理

    Python之文件处理 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...

  5. python进阶之路4.2---装饰器

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. Python进阶之路---1.3python环境搭建

      python环境安装 windows python环境安装 下载安装包     https://www.python.org/downloads/ 安装并指定安装目录     C:\python2 ...

  7. Python进阶之路---1.1python简介

                            Python简介 Python简介 Python (发音:[ 'paiθ(ə)n; (US) 'paiθɔn ]n.蟒蛇,巨蛇 ),是一种面向对象的解释 ...

  8. python全栈开发笔记---基本数据类型--数字型魔法

    数字  int a1 =123 a2=456 int 讲字符串转换为数字 a = " #字符串 b = int(a) #将字符串转换成整形 b = b + 1000 #只有整形的时候才可以进 ...

  9. python进阶之路4.1---生成器与迭代器

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

随机推荐

  1. HTML 转文本及HTML内容提取(C#)

    //1.HTML直接转文本 //使用方法 HtmlToText convert = new HtmlToText(); textBox2.Text = convert.Convert(textBox1 ...

  2. C#实现测量程序运行时间及cpu使用时间

    private void ShowRunTime() { TimeSpan ts1 = Process.GetCurrentProcess().TotalProcessorTime; Stopwatc ...

  3. Mysql 储存过程以及 python callproc调用

    一.存储过程(stored procedure) 存储过程将存入的一系列SQL语句进行预编译,执行并存放在数据库中,之后如果需要使用sql语句对这一组sql进行访问时可以直接提取(很好理解 存储过程就 ...

  4. C# XmlSerializer序列化浅析

    C# 中使用 XmlSerializer 实现类和xml文件的序列化和反序列化,使用起来非常简单. C# XmlSerializer实现序列化: XmlSerializer xml = new Xml ...

  5. Javascript获取某个月的天数-简单方法 .(转别人的)

    Javascript里面的new  Date("xxxx/xx/xx")这个日期的构造方法有一个妙处,当你传入的是"xxxx/xx/0"(0号)的话,得到的日期 ...

  6. ECSTORE关于MONGODB安装

    1.安装mongodb wget http://www.phpwindow.com/linux/mongodb-linux-x86_64-2.2.1.tgz tar zxvf mongodb-linu ...

  7. sublime text3 安装package control

    20141104日更新的安装代码为 import urllib.request,os,hashlib; h = '7183a2d3e96f11eeadd761d777e62404' + 'e330c6 ...

  8. Python新手学习基础之运算符——算术运算符

    算术运算符 之前文章在介绍变量类型的时候,其实已经用过了很多算术符,比如+.-.*././/.** 等,除此之外,还有一个符号是之前内容没提到的,就是 % ,用来返回除法余数的运算符号. 假设有变量x ...

  9. SelectedNode与e.node的区别

    SelectedNode与e.node的区别 待补.......

  10. 二维指针*(void **)的研究(uC/OS-II案例) 《转载》

    uC/OS-II内存管理函数内最难理解的部分就是二维指针,本文以图文并茂的方式对二维指针进行了详细分析与讲解.看完本文,相信对C里面指针的概念又会有进一步的认识. 一.OSMemCreate( ) 函 ...