不一样编码之间的关系 python
python2中文件的默认编码为ASCII,在文件中含有中文的时候就会报错,这时,咱们须要是设置一下文件的默认编码,以下:git
#!/usr/bin/env python # -*- coding: UTF-8 -*- # 指定python文件编码方式
在python3中,文件的默认编码为UTF-8,已经不存在这个问题。app
循环优化
for i in range(1, 10, 2): # 1-10 2是步长 i是变量 #[1, 3, 5, 7, 9] break #退出循环 continus #跳过当次循环 exit() #退出程序
# __author: Administrator # date: 2016/8/22 name = 'weijie' age = 32 job = 'developer ' salary = 5000.00if salary.isdigit(): #长的像不像数字,好比200d , '200' salary = int(salary) # else: # #print() # exit("must input digit") #退出程序 msg = ''' --------- info of %s -------- Name: %s Age : %d Job : %s Salary: %f You will be retired in %s years -------- end ---------- ''' % (name, name, age, job, salary, 65-age) print(msg)
列表编码
查
索引(下标) ,都是从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 () #按照ASAII码排序
reverse() #把现行列表倒叙
身份判断
>>> type(a) is list
True
>>>
spa