运算-list
1)三元运算
a,b,c = 2,3,4
d = a if a>b else c
2)进制
- 二进制,01
- 八进制,01234567
- 十进制,0123456789
- 十六进制,0123456789ABCDEF 二进制到16进制转换 http://jingyan.baidu.com/album/47a29f24292608c0142399cb.html?picindex=1
3)二进制转字符串decode,网络传输只能二进制encode字符串
'&&&20'.encode(encoding = "utf-8")
b'\x82xe2.decode(encoding="utf-8")
4)列表运算 --在系统中就是内存指针
names=["Zhang","Li","wang","Yin"]
print(names)
print(names[1])
len(names) #count records
print(names[0:3]) # 取头不取尾
print(names[-1]) #取右边第一个
print(names[-3:-1]) #和print(names[-3:])相同
names.append("Zhu") #追加列表
names.insert(1,"insert after Zhang!")
names.pop(1) #delete
print(names.index("Yin")) #Query record position
print(names.count("Yin")) #Count
names.clear() #clear the list "names"
names.reverse() #反转
names.sort() #排序
names2 = ["1","2"]
names.extend(names2) #merge names list to names
names3 = names.copy() #复制
names3[2] ="change second Record!"
names4=["Zhang",["Li","wang"],"Yin"]
print(names4) #注意[]的用法
names[2] = "xyz"
names2[3][0] ="测试"
print(names)
print(names2)
names5 =names #完成一样,不是单纯指针指向的问题,开了一个新的内存空间
print(names)
print(names5)
import copy
# 浅copy的三种方式
1)names5 = copy.copy(names)
2)names5 = names[:]
3)names5 = list(names)
# 可以通过浅copy创建联合账号 ---???浅copy修改记录之后没有同步,what is Wrong?
_name = ["Zhang", ["Alex",100]] #测试下这个list套list是否可以???
p1 = _name[:]
p2 = _name[:]
p1[0] ='A'
p2[0] ='B'
p1[1][1] = 50 #浅copy修改记录之后可以同步
# 深copy
names5 = copy.deepcopy(name) #深copy
### 利用循环来copy
for i in names:
print(i)
print(name[0:-1:2]) #步长打印 等同print(name[::2]
嵌套list
#shopping cart program class24
product_list = [
('iphone',5800),
('iwatch', 1500),
]
# if salary.isdigit():
#product_list.index(item) / for index, item in enumerate(product_list): print(index,item)
# user_choice < len(product_list) and user_choice >=0
# print("Added %s into shopping cart, your current balance is \033[31;1m%s\033[0m" %(pitem,salary))
exit()