首页 > temp > 简明python教程 >
-
运算符以及数据类型解析
运算符
一.算数运算:

二.比较运算:

三.赋值运算

四.逻辑运算

五.成员运算

基本数据类型
一.Number(数字)
Python3中支持int、float、bool、complex。
使用内置的type()函数查询变量类型。

int(整型)
在python2中整数类型有两种一个是int,表示整型,一种是long,表示长整型。而在python3中整数类型只有int,没有限制大小,但是由于内存有限得原因,所以我们使用的整型不可能是无限大的。
在python2中整型大小的限制
在32为系统上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
在64为系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
python2中,当没有超过这个范围的时候是int类型,当超过这个范围的时候就是long类型。
整型的表现形式有四种,分别是二进制(0b开头),八进制(0o开头),十进制,十六进制(0x开头),进制之间的转换可以使用python中的内置函数(bin、oct、int、hex)。

二.布尔值
布尔值很简单,就是真和假,1为真0为假(进行判断的时候除了数字0为假,其它数字都为真)
三.字符串(str)
1.字符串的创建
我们的第一串代码hello world就是字符串类型的。

Python中的字符串通常使用单引号(‘’)或者双引号(“”)括起来,在字符串中可以使用反斜杠(\)进行转义,可以使用+好将两个字符串连接起来。

2.字符串的常用功能
- 分割
- 长度
- 索引
- 切片









3.字符串特有功能
- 去除两边空白
- 是否以什么开头
- 查找子序列
- 将字符串的某个子序列替换为指定的值
- 变大写
- 判断字符串是否和某个字符串一样

四.列表(List)
Python最常用的数据类型就是列表,在列表中的的数据类型可以不同,而且可以多层嵌套数据。
列表的格式,列表是写在[]中,使用逗号(,)进行元素间的分割。
1.创建列表

2、转换
字符串、字典、元组都可以转换为列表

3.列表常用功能
- 索引
- 切片
- 长度
- ......

4.列表特有功能
- 排序
- 追加
- 插入
- 移除
- 个数
- 扩展
- 反转

5.嵌套

五、元组(tuple)
元组合列表类似,但是元组中的元素一旦定义了就不能就行修改。
元祖的格式,元祖使用一个括号括起来,元素之间的分割是用逗号(,),元组中也可以存放不同类型的数据。
1.创建元组

2.转换
字符串、列表以及字典都可以转换为元组

3.常用功能
- 索引
- 长度
- 切片
- ...

4.元组特有功能
- 个数
- 索引位置

5.嵌套

6.元组的特性
元组的特性是不可修改,元素不可修改,但是元素中包含的元素可以修改

六、字典
Python中一个比较特别的数据类型,字典中的数据是映射的关系,一个键(key)映射一个或多个值(value),使用{}来进行标识,字典是一个无序的键值对的集合,而且一个字典中的间(key)是唯一的,不能重复。
1.创建字典

用列表创建字典时不能直接加到字典里需要通过enumerate。

2.字典常用功能
字典中没有切片,因为字典中的键值对的键(key)就是相应值的索引,相当于自定义了每个指的索引。
- 索引
- 新增
- 删除
- 键、值、键值对
- 长度

3.另一种方式创建字典
示例一

示例二
