人生苦短,我用Python

Python标识符




python3使用do while python中do while循环_python 字典值转义


Python有五个标准的数据类型

  • Numbers(数字)
  • String(字符串)
  • List(列表)
  • Tuple(元组)
  • Dictionary(字典)

万丈高楼平地起,这五个标准的数据类型将贯穿于整个Python

Python支持四种不同的数字类型:

  • int(有符号整型)
  • long(长整型[也可以代表八进制和十六进制])
  • float(浮点型)
  • complex(复数

python的字串列表有2种取值顺序:

  • 从左到右索引默认0开始的,最大范围是字符串长度少1
  • 从右到左索引默认-1开始的,最大范围是字符串开头

List(列表) 是 Python 中使用最频繁的数据类型。

  • 列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(即嵌套)。
  • 列表用 [ ] 标识,是 python 最通用的复合数据类型。
  • 列表中值的切割也可以用到变量 [头下标:尾下标] ,就可以截取相应的列表,从左到右索引默认 0 开始,从右到左索引默认 -1 开始,下标可以为空表示取到头或尾。
  • 加号 + 是列表连接运算符,星号 * 是重复操作。

元组是另一个数据类型,类似于List(列表)。

  • 元组用"()"标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。

字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。

  • 列表是有序的对象结合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
  • 字典用"{ }"标识。字典由索引(key)和它对应的值value组成。


python3使用do while python中do while循环_python 字典值转义_02


Python数据类型转换


python3使用do while python中do while循环_Python写入字典_03


Python 运算符


python3使用do while python中do while循环_Python写入字典_04


python3使用do while python中do while循环_python3使用do while_05


python比较运算符

以下假设变量a为10,变量b为20:


python3使用do while python中do while循环_python while循环引用_06


Python赋值运算符

以下假设变量a为10,变量b为20:


python3使用do while python中do while循环_python 字典值转义_07


Python为运算符

下表中变量 a 为 60,b 为 13,二进制格式如下:


python3使用do while python中do while循环_python3使用do while_08


python3使用do while python中do while循环_python 字典值转义_09


Python逻辑运算符


python3使用do while python中do while循环_python 字典值转义_10


Python成员运算符

除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。


python3使用do while python中do while循环_python3使用do while_11


Python身份运算符

身份运算符用于比较两个对象的存储单元


python3使用do while python中do while循环_python 字典值转义_12


注:is 与 == 区别:

is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。

Python 循环语句

Python提供了for循环和while循环(在Python中没有do..while循环):


python3使用do while python中do while循环_Python写入字典_13


python3使用do while python中do while循环_python3使用do while_14


Python Number(数字)

Python 支持四种不同的数值类型:


python3使用do while python中do while循环_python 字典值转义_15


Python数学函数


python3使用do while python中do while循环_python while循环引用_16


随机函数

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

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


python3使用do while python中do while循环_python3使用do while_17


Python三角函数

Python包括以下三角函数:


python3使用do while python中do while循环_Python写入字典_18


Python数字常量


python3使用do while python中do while循环_python3使用do while_19


Python字符串

Python转义字符:

在需要在字符中使用特殊字符时,python用反斜杠()转义字符。如下表:


python3使用do while python中do while循环_python while循环引用_20


Python字符串运算符:

下表实例变量 a 值为字符串 "Hello",b 变量值为 "Python":


python3使用do while python中do while循环_Python写入字典_21


Python字符串格式化


python3使用do while python中do while循环_python3使用do while_22


Python列表

Python包含以下函数:


python3使用do while python中do while循环_python3使用do while_23


Python包含以下方法:


python3使用do while python中do while循环_Python写入字典_24


Python元组

Python的元组(tuple)与列表类似,不同之处在于元组的元素不能修改。

元组使用小括号,列表使用方括号。

元组内置函数:

Python元组包含了以下内置函数


python3使用do while python中do while循环_python 字典值转义_25


Python字典

字典是另一种可变容器模型,且可存储任意类型对象。

字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中

字典内置函数及方法:

Python字典包含了以下内置函数:


python3使用do while python中do while循环_Python写入字典_26


Python字典包含了以下内置方法:


python3使用do while python中do while循环_python while循环引用_27


匿名函数lambda

python 使用 lambda 来创建匿名函数。


python3使用do while python中do while循环_python while循环引用_28


如:

sum = lambda arg1, arg2: arg1 + arg2;print "相加后的值为 : ", sum( 10, 20 ) //输出30

python import语句


python3使用do while python中do while循环_python 字典值转义_29


Python文件操作


python3使用do while python中do while循环_python3使用do while_30


不同模式打开文件的完全列表:


python3使用do while python中do while循环_python3使用do while_31


  • File对象的属性

一个文件被打开后,你有一个file对象,你可以得到有关该文件的各种信息。

以下是和file对象相关的所有属性的列表:


python3使用do while python中do while循环_python3使用do while_32


python3使用do while python中do while循环_python while循环引用_33


python3使用do while python中do while循环_Python写入字典_34


python3使用do while python中do while循环_python while循环引用_35


Python File(文件)方法

file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数:

序号方法及描述1

file.close()

关闭文件。关闭后文件不能再进行读写操作。

2

file.flush()

刷新文件内部缓冲,直接把内部缓冲区的数据立刻写入文件, 而不是被动的等待输出缓冲区写入。

3

file.fileno()

返回一个整型的文件描述符(file descriptor FD 整型), 可以用在如os模块的read方法等一些底层操作上。

4

file.isatty()

如果文件连接到一个终端设备返回 True,否则返回 False。

5

file.next()

返回文件下一行。

6

file.read([size])

从文件读取指定的字节数,如果未给定或为负则读取所有。

7

file.readline([size])

读取整行,包括 " " 字符。

8

file.readlines([sizehint])

读取所有行并返回列表,若给定sizeint>0,返回总和大约为sizeint字节的行, 实际读取值可能比sizhint较大, 因为需要填充缓冲区。

9

file.seek(offset[, whence])

设置文件当前位置

10

file.tell()

返回文件当前位置。

11

file.truncate([size])

截取文件,截取的字节通过size指定,默认为当前文件位置。

12

file.write(str)

将字符串写入文件,没有返回值。

13

file.writelines(sequence)

向文件写入一个序列字符串列表,如果需要换行则要自己加入每行的换行符。

Python内置函数


python3使用do while python中do while循环_Python写入字典_36