数据运算
数据类型
不可变类型:整型,字符串,元组
可变类型:列表,字典
- 数字–>整数 int(integer)
整型/长整型:
在 python3中 已经不区分整型与长整型,统一都叫整型
在C中区分,分别是: int age 22 , long age
布尔:
只有2种状态,分别是:真 True; 假 False。
字符串:
salary.isdigit()
计算机中, 一切皆为对象。
世界万物,皆为对象,一切对象皆可分类
列表,元组
列表创建:
a=[1,2,3]#第一种
a=list(((1,2,3),))#第二种,也可以写成a=list([[1,2,3],])
元组创建:
tup1=()#空元组
tup2=(20,)#创建一个元组,第一个元素是20(还有多个元素可在‘逗号’后面追加)
a=tuple([1,2,3])
元组切片查询
a=(1,2,3,4,5)
print(a[1:2])
#打印结果
(2,)
- 查
索引(下标) ,都是从0开始
#切片
.count 查某个元素的出现次数
.index 根据内容找其对应的位置
"haidilao ge" in a
- 增加
a.append() 追加
a.insert(index, "内容")
a.extend 扩展
- 修改
a[index] = "新的值"
a[start:end] = [a,b,c]
- 删除
remove("内容")
pop(index)
del a, del a[index]
a.clear() 清空
- 排序
sort ()
reverse()
- 身份判断
>>> type(a) is list
True
>>>
循环loop
for
while
break , continue
for i in range(int(len(data) * rate)):
out_file_1.write(data[i][1])
有限循环 ,次数限制
无限循环=死循环
continue 结束本次循环,继续下一次循环
break 跳出整个当前的循环
字符格式化输出
占位符
- %s s = string
- %d d = digit 整数
- %f f = float 浮点数,约等于小数
#举个栗子
print ("the classifier came back with: %s,the real answer is: %s" %(classifierResult, datingLabels[i]))
print ("the total error rate is: %f" %(errorCount/float(numTestVecs)))
# % 格式字符串
print('alex is a good teacher')
print('%s is a good teacher'%'alex')
#关键字 in
print(123 in [23,45,123])
print('e2l' in