python 基础知识(二)
1、二进制转十六进制方法
二进制到16进制转换:http://jingyan.baidu.com/album/47a29f24292608c0142399cb.html?picindex=1
2、encode 和 decode 例子
msg = "我爱北京天安门"
print(msg.encode(encoding="utf-8")) #编码为二进制
print(msg.encode(encoding="utf-8").decode(encoding="utf-8")) #解码
3、数组中切片取值
names = ["xiefei","daishaolong","piaoyongjin","heyande"]
print(names)
names.append("yanting") #追加字符串
names.insert(1, "wudawei") #指定位置插入
names[2] = "caojian" #替换指定位置的参数
names.remove("xiefei") #删除值
del names[0] #删除指定位置的值
names.pop() #删除最后一个值 默认删除最后一个 输入下标删除指定位置
print(names.count("heyande")) #统计个数
print(names)
#print(names[0], names[2])
#print(names[1:3])
#print(names[3])
#print(names[-2])
#print(names[-3:-1])
#print(names[0:3])
#print(names[:3]) #和上边相等
4、浅copy
import copy
person=['name',['saving',100]]
p1 = copy.copy(person) #第一种方式
p2 = person[:] #第二种方式
p3 = list(person) #第三种方式
print(p1)
print(p2)
print(p3)
#p1和p2两个人为夫妻,p1取款50,p2发现卡里少50,浅copy用于,两个人有一个公共账号,同步卡里金额
p1 = person[:]
p2 = person[:]
p1[0] = 'xiefei'
p2[0] = 'zhangxue'
p1[1][1] = 50
print(p1)
print(p2)
5、深copy
name2 = copy.deepcopy(names) #深copy
6、购物车联系
#author:xie fei
product_list = [
('Iphone', 5800),
('Mac Pro', 9800),
('Bike', 800),
('Watch', 10600),
('Coffee', 31),
('Book', 120),
]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
#for item in product_list:
#print(product_list.index(item),item) #下标
for index, item in enumerate(product_list):
print(index, item)
user_choice = input("choice buy?>>>:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_item = product_list[user_choice]
if p_item[1] <= salary: #buy yes
shopping_list.append(p_item)
salary -= p_item[1]
print("Added %s into shopping cart, your current balance is \033[31;1m%s\033[0m" %(p_item, salary))
else:
print("\033[41;1m你的余额不足,剩余[%s],还买个毛线啊\033[0m" % salary)
else:
print("produce code [%s] is not exist!" % user_choice)
elif user_choice == 'q':
print("-------------------------shopping list-----------------------")
for p in shopping_list:
print(p)
print("Your current balance:", salary)
exit()
else:
print("invalid option")
else:
print("salary is must number, can't is [%s]" % salary)
7、for循环
info = dict.fromkeys([1,2,3],[4,5,6])
#高效for循环
for i in info:
print(i, info[i])
#低效for循环
for k, v in info.items():
print(k, v)
8、练习题
用户入口:
1、商品信息存在文件里
2、已购商品,余额记录。
商家入口:
1、可以添加商品, 修改商品价格

浙公网安备 33010602011771号