Python基础二
目录
一、数据类型
二、三元运算
三、进制
四、string与bytes类型的转换
五、列表&元组
一、数据类型
1、数字
2 是一个整数的例子。
长整数 不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子,其中-5,4为实数,j为虚数,数学中表示复数是什么?。
int(整型)
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
float(浮点型)
浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。
complex(复数)
复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
"hello world"
=
值
1
if
条件
else
值
2
如果条件为真:result = 值1
如果条件为假:result = 值2
三、进制
- 二进制,01
- 八进制,01234567
- 十进制,0123456789
- 十六进制,0123456789ABCDEF 二进制到16进制转换http://jingyan.baidu.com/album/47a29f24292608c0142399cb.html?picindex=1
四、string与bytes类型的转换
meg = "你好" print(meg.encode(encoding=("utf-8"))) mag = b'\xe4\xbd\xa0\xe5\xa5\xbd' print(mag.decode(encoding=("utf-8")))
执行结果:
b'\xe4\xbd\xa0\xe5\xa5\xbd'
你好
五、列表&元组
打印列表各个位置的值,切片
# Author Cathy list_name = ["aa","bb","cc","dd","ee"] print(list_name) #打印列表所有内容 print(list_name[0])# 打印列表第一个位置的内容 print(list_name[0],list_name[2]) print(list_name[0:3]) print(list_name[-1]) print(list_name[-3:]) print(list_name[-3:-1])
执行结果:
['aa', 'bb', 'cc', 'dd', 'ee']
aa
aa cc
['aa', 'bb', 'cc']
ee
['cc', 'dd', 'ee']
['cc', 'dd']
列表中的几个方法
索引
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list_name = ["aa","bb","cc","dd","ee","ff"] print(list_name.index("cc"))
执行结果:
2
cc就是在2的位置(注:起始位置是0开始的)
追加
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list_name = ["aa","bb","cc","dd","ee","ff"] list_name.append("gg") print(list_name)
执行结果:
['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg']
在列表最后追加“gg”
插入
list_name = ["aa","bb","cc","dd","ee","ff"] list_name.insert(3,"gg") print(list_name)
执行结果:
['aa', 'bb', 'cc', 'gg', 'dd', 'ee', 'ff']
替换
list_name = ["aa","bb","cc","dd","ee","ff"] list_name[2]="hh" print(list_name)
执行结果:
['aa', 'bb', 'hh', 'dd', 'ee', 'ff']
删除
list_name = ["aa","bb","cc","dd","ee","ff"] list_name.remove("bb")#删除bb del list_name[0]#删除第一个 list_name[0]=list_name.pop(0) list_name.pop()#删除最后一个 print(list_name)
执行结果:
['cc', 'dd', 'ee']
统计
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# Author Cathy list_name = ["aa","bb","cc","dd","ee","ff","ee","ee"] print(list_name.count("ee"))
执行结果:
3
直接返回结果3,说明列表中有3个“ee”
clear清空列表
list_name = ["aa","bb","cc","dd","ee","ff","ee","ee"] list_name.clear() print(list_name)
排序反转
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# Author Cathy list_name = ["àằầèềìòờùừỳ","年后","cc","dd","ee","ff","ee","Chụp"] list_name.reverse()#反转 list_name.sort()#排序 list_name2 = ["11","22"] list_name.extend(list_name2)#两个列表拼接到一起 print(list_name) #del list_name2 删除列表 print(list_name2)
执行结果:
['Chụp', 'cc', 'dd', 'ee', 'ee', 'ff', 'àằầèềìòờùừỳ', '年后', '11', '22']
['11', '22']
浅copy
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# Author Cathy import copy list_name = ["àằầèềìòờùừỳ","年后",["clair","bell"],"cc","dd","ee","ff","ee","Chụp"] list_name2 = list_name.copy()#浅copy #list_name2 = copy.deepcopy(list_name)#深copy list_name[1] = "nianhou" list_name[2][0] = "女王"#改的是同一块内存地址 print(list_name) print(list_name2)
执行结果:
['àằầèềìòờùừỳ', 'nianhou', ['女王', 'bell'], 'cc', 'dd', 'ee', 'ff', 'ee', 'Chụp']
['àằầèềìòờùừỳ', '年后', ['女王', 'bell'], 'cc', 'dd', 'ee', 'ff', 'ee', 'Chụp']
深copy
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# Author Cathy import copy list_name = ["àằầèềìòờùừỳ","年后",["clair","bell"],"cc","dd","ee","ff","ee","Chụp"] #list_name2 = list_name.copy()#浅copy list_name2 = copy.deepcopy(list_name)#深copy list_name[1] = "nianhou" list_name[2][0] = "女王"#改的是同一块内存地址 print(list_name) print(list_name2)
执行结果:
['àằầèềìòờùừỳ', 'nianhou', ['女王', 'bell'], 'cc', 'dd', 'ee', 'ff', 'ee', 'Chụp']
['àằầèềìòờùừỳ', '年后', ['clair', 'bell'], 'cc', 'dd', 'ee', 'ff', 'ee', 'Chụp']
步长切片和循环
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# Author Cathy list_name = ["àằầèềìòờùừỳ","年后",["clair","bell"],"cc","dd","ee","ff","ee","Chụp"] print(list_name[0:-1:2])#步长的切片 for i in list_name: print(i)#打印列表
执行结果:
['àằầèềìòờùừỳ', ['clair', 'bell'], 'dd', 'ff']
àằầèềìòờùừỳ
年后
['clair', 'bell']
cc
dd
ee
ff
ee
Chụp
list_name[开始的位置:结束的位置:步长],0或-1可以省略掉
元组
元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
语法
tuple_name = ("aa","bb","cc")
它只有2个方法,一个是count,一个是index,完毕。