python基础知识(二)—— 数据运算&循环loop&字符格式化输出&字典&String 操作&文件操作

数据运算

数据类型

不可变类型:整型,字符串,元组
可变类型:列表,字典

  • 数字–>整数 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值