列表是咱们最之后最经常使用的数据类型之一,经过列表能够对数据实现最方便的存储、修改等操做html
定义列表python
students = ["ronghui","jyj","a","b"]linux
students[0]git
'ronghui'shell
切片:取多个元素windows
students = ["ronghui","jyj","a","b"]api
students[1:4]#包括1可是不包括4app
['jyj','a','b']iphone
students[1:-1]#下标1到倒数第二个ide
['jyj','a']
students[0:2] = students[:2]#开头到第一个,不包括下标2
['ronghui','jyj']
students[0::2] = students[::2]#每隔一个元素取一个
['ronghui','a']
追加
students = ["ronghui","jyj","a","b"]
students.append("jiangyijing")
students = ["ronghui","jyj","a","b","jiangyijing"]
插入
students = ["ronghui","jyj","a","b"]
students.insert(2,"new")#在第二个位置插入new
students = ["ronghui","jyj","new","a","b"]
修改
students = ["ronghui","jyj","a","b"]
students[0]="new"
students = ["new","jyj","a","b"]
删除
students = ["ronghui","jyj","a","b"]
students.remove("ronghui")
del students[0]
students.pop(obj)#默认删除最后一个
students.pop(0)
拷贝
students = ['a','b','c',['d','e']]
students2 = students.copy()
print(students) #['a', 'b', 'c', ['d', 'e']]
print(students2) #['a', 'b', 'c', ['d', 'e']]
students[0] = 'A'
print(students) ['A', 'b', 'c', ['d', 'e']]
print(students2) ['a', 'b', 'c', ['d', 'e']]
students[3][0] = 'D'
print(students) ['A', 'b', 'c', ['D', 'e']]
print(students2) ['a', 'b', 'c', ['D', 'e']]
仅仅拷贝第一层 由于第一层['d','e']指向内存中的一个地址
['a', 'b', 'c', ['d', 'e']]
['a', 'b', 'c', ['d', 'e']]
['A', 'b', 'c', ['d', 'e']]
['a', 'b', 'c', ['d', 'e']]
['A', 'b', 'c', ['D', 'e']]
['a', 'b', 'c', ['D', 'e']]
排序&翻转
sort&reverse
students = ["ronghui","jyj","a","b"]
students.sort()#排序
students.reverse()#倒序
获取下标
students = ["ronghui","jyj","a","b"]
students.index("ronghui")
元组其实跟列表差很少,也是存一组数,只不是它一旦建立,便不能再修改,因此又叫只读列表
语法
students = ('a','b','c')
它只有2个方法,一个是count,一个是index,完毕。
请闭眼写出如下程序。
程序:购物车程序
需求:
shopping_car = []
shopping_list = [['iphone',5000],['book',100],['bike',800]]
for line in shopping_list:
print(line)
flag = True
while flag:
salary = input("your salary:")
if salary.isdigit():
salary = int(salary)
flag = False
print("输入你要购买商品的编号:")
while True:
choice = input("your number:")
if choice.isdigit():
choice = int(choice)
product = shopping_list[choice]
price = shopping_list[choice][1]
print(price)
if salary >= price:
shopping_car.append(product)
salary -= price
print("your balance %s"%(salary))
elif choice == 'q':
print(shopping_car)
break
else:
print("invalid input ")
else:
print("not number,exit")
name = 'jiangyijing'
name2 = name.capitalize()
name2 = name.swapcase()
name2 = name.center(50,'*')
name2 = len(name)
name2 = name.count('j')
name2 = name.encode()
name2 = name.endswith('g')
name2 = name.expandtabs(10)
name2 = name.find('j')
name2 = name.index('j')
name2 = name.isdigit()
name2 = name.isalpha()
name2 = name.isalnum()
name2 = name.isidentifier()
name2 = name.isupper()
name2 = name.ljust(40,'*')
name2 = name.rjust(40,'*')
name2 = name.strip()
name2 = name.lstrip()
name2 = name.rstrip()
print(name)
print(name2)
students = {
'stu1':'ronghui',
'stu2':'jyj',
'stu3':'a'
}
字典的特性:
students['stu4'] = 'huihui'
students = {
'stu1':'ronghui',
'stu2':'jyj',
'stu3':'a',
'stu4':'huihui'
}
修改
students['stu4'] = 'HHH'
students = {
'stu1':'ronghui',
'stu2':'jyj',
'stu3':'a',
'stu4':'HHH'
}
删除
students.pop()
del students['stu1']
students.popitem()
查找
"stu1" in students
True
多级菜单
menu = {
"shanghai":{
"baoshan":{
"sitang":{"ronghui":"hardworking"},
"huma":{"zhouwenzhe":"shabi"},
"tonghe":{"zhouwei":"dashabi"}
},
"hongkou":{
"hkxincun":{},
"hkzuqiuchang":{}
},
"pudong":{
"nanhui":{},
"huinan":{}
}
},
"beijing":{
"haidian":{},
"changping":{},
"chaoyang":{}
},
}
其余
#values
students.value
#keys
循环dict
for key in students:
print(key,students[key])
程序: 三级菜单
要求:
集合是一个无序的,不重复的数据组合,它的主要做用以下:
经常使用操做
s = set([3,5,9,10]) #建立一个数值集合 t = set("Hello") #建立一个惟一字符的集合 a = t | s # t 和 s的并集 b = t & s # t 和 s的交集 c = t – s # 求差集(项在t中,但不在s中) d = t ^ s # 对称差集(项在t或s中,但不会同时出如今两者中) 基本操做: t.add('x') # 添加一项 s.update([10,37,42]) # 在s中添加多项 使用remove()能够删除一项: t.remove('H') len(s) set 的长度 x in s 测试 x 是不是 s 的成员 x not in s 测试 x 是否不是 s 的成员 s.issubset(t) s <= t 测试是否 s 中的每个元素都在 t 中 s.issuperset(t) s >= t 测试是否 t 中的每个元素都在 s 中 s.union(t) s | t 返回一个新的 set 包含 s 和 t 中的每个元素 s.intersection(t) s & t 返回一个新的 set 包含 s 和 t 中的公共元素 s.difference(t) s - t 返回一个新的 set 包含 s 中有可是 t 中没有的元素 s.symmetric_difference(t) s ^ t 返回一个新的 set 包含 s 和 t 中不重复的元素 s.copy() 返回 set “s”的一个浅复制
5. 文件操做
f
=
open
(
'lyrics'
)
#打开文件
first_line
=
f.readline()
print
(
'first line:'
,first_line)
#读一行
print
(
'我是分隔线'
.center(
50
,
'-'
))
data
=
f.read()
# 读取剩下的全部内容,文件大时不要用
print
(data)
#打印文件
f.close()
#关闭文件
打开文件的模式有:
"+" 表示能够同时读写某个文件
"U"表示在读取时,能够将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)
"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)
with语句
为了不打开文件后忘记关闭,能够经过管理上下文,即:with
open
(
'log'
,
'r'
) as f:
with
open
(
'log1'
) as obj1,
open
(
'log2'
) as obj2:
pass
程序练习
程序1: 实现简单的shell sed替换功能
程序2:修改haproxy配置文件
详细文章:
http://www.cnblogs.com/yuanchenqi/articles/5956943.html
http://www.diveintopython3.net/strings.html
需知:
1.在python2默认编码是ASCII, python3里默认是unicode
2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节), so utf-16就是如今最经常使用的unicode版本, 不过在文件里存的仍是utf-8,由于utf8省空间
3.在py3中encode,在转码的同时还会把string 变成bytes类型,decode在解码的同时还会把bytes变回string