字符类型转换
msg = "我爱你"
print(msg.encode()) #字符串类型转成二进制类型(bytes)
print(msg.encode(encoding="utf-8").decode(encoding="utf-8"))
程序运行结果:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
列表:
import copy
names = ["Chenliang","Wenyu","Xiedehua","Xiaohu"]
print(names)
# print(names)
# print(names[1],names[2])
# print(names[:2]) #切片
# print(names[-2:])
# names.insert(3,"Fengdashan") #插入数据
# names.remove("Xiaohu")
# del names[0] #删除数据
# names[1]="Xiaohei" #替换列表数据
# names.append("Weiyingming") #增加元素
# names.sort() #排序,依照ASCII码
# print(names.index("Xiedehua")) #打印元素在列表中的位置
# print(names[names.index("Xiedehua")])
# names.reverse() #将列表颠倒
# print(names.count("Chenliang")) #打印列表中相同元素的个数
# print(names)
=====================================================
names2=["Guanyu",["Zhaoyun","caocao"],"Zhangfei"]
# names.extend(names2)
# print(names,names2)
print(names2)
name = copy.deepcopy(names2) #深度copy,复制names2后,若names2改变任意地址元素,name不会变
print(name)
names2[1][1]="huangzhong"
names2[2]="machao"
print(names2)
print(name)
结果:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
字典:
# 字典是无序的
# key-value
av_log = {
"a01":"xiaozemaliya",
"a02":"canginkong",
"a03":"longzeluola"
} #定义字典
print(av_log["a02"])
print(av_log.get("a01")) #查
print(av_log.get("a04")) #用get方法获取value,如果健值不存在,则返回空
av_log["a02"] = "boduoyejieyi" #改
print(av_log)
av_log["a04"] = "jessechan" #健值不存在时增加,存在时则修改原来的
# del av_log["a03"]
av_log.pop("a03") #删 .pop如不指定健值,则随机删一个
print(av_log)
print("a01" in av_log) #判断key是否在字典中
print("a05" in av_log)
==================================================================================================
字典多级嵌套:
diration = {
"a":{
"01":["zhangfei","guanyu","zhaoyun"],"02":["huangzhong","machao","liubei"]},
"b":{
"01":["caocao","xunyu"],"02":["xuchu","zhangliao"]},
"c":{
"01":["sunquan","zhouyu"],"02":["luxun","lumeng"]}
} #字典套字典 ,列表
while True:
for i in diration:
print(i)
choice = input("请输入")
if choice in diration:
while True:
for j in diration[choice]:
print("\t",j)
choice_1 = input("请选择")
if choice_1 in diration[choice]:
for k in diration[choice][choice_1]:
print(k)
choice_2=input("到底了,按b返回")
if choice_2 == "b":
break #三级菜单
# print(diration["c"]["02"])
# for i in diration["a"]["02"]:
# print(i)
# a = diration["c"]["02"] #修改字典里的列表元素,首先取出列表赋值给变量a,让后在修改
# a[0]="caohong"
#
#
# print(diration)
# print(diration["c"]["02"])
------------------------------------------------------------------------------------------------------------------------
字符串操作:
a="i love cat"
# print("jesse chan".capitalize()) #首字母大写
# print("jesse chan".center(50,"*")) #打印五十个字符,不够用*补
# print("jesse chan".endswith("an")) #判断是否以...结尾
# print(a[a.find("love"):]) #截取字符串部分值
# print("jesse chan".replace("c","C")) #替换
f=open("f:\\iphone.txt",encoding="utf-8")
car = f.read()
print(car)
print(car.split()) #转成列表 默认以空格为分割符
# print("1+2+3+4+5".split("+")) #以+号为分割符,将字符串转成列表
# print("jesse chan".title()) #转成标题格式
# print("+".join(["1","2","3"])) #列表转字符串,以+号为分割符
#
# print("\n jessechan".strip()) #去掉空格和换行
------------------------------------------------------------------------------------------------
购物车小程序:
user = "admin"
passwd = "123456"
title = "淘宝网"
title_1 = "请登录"
print(title.center(50,"#"))
print(title_1.center(50,"*"))
username = input("请输入用户名:")
password = input("请输入密码:")
if user==username and passwd==password:
print("登入成功!")
goods = [["香蕉", 52], ["苹果", 25], ["葡萄", 29], ["榴莲", 128], ["西瓜", 244]]
car_list = []
money = int(input("请充值(请输入正整数):"))
while True:
for index, item in enumerate(goods): #循环打印列表,enumerate用于展示出下标
print(index+1, item)
user_choice = input("选择你要买的商品(按p打印购物车列表,按q结算退出):")
if user_choice.isdigit(): # 检测是否只由数字构成
user_choice = int(user_choice)
if user_choice <= len(goods) and user_choice > 0:
p_item = goods[user_choice-1]
if money >= p_item[1]:
car_list.append(p_item)
money -= p_item[1]
print("你已经添加%s到购物车,可用余额为%s元" % (p_item[0], money))
else:
print("余额不足!")
else:
print("你输入的商品不存在!")
elif user_choice == "q":
print("你已购买:")
for q in car_list:
print(q)
print("余额:{}".format(money))
print("欢迎下次光临!!!")
exit()
elif user_choice == "p":
print("你已购买:")
for p in car_list:
print(p)
print("===========================")
elif user_choice == "d":
for i,j in enumerate(car_list):
print(i+1,j)
print("-------------------------------")
del_choice=input("选择你要删除的商品:")
if del_choice.isdigit():
del_choice = int(del_choice)
if del_choice <= len(car_list) and del_choice > 0:
del car_list[del_choice-1]
print("你已将%s移出购物车" % (car_list[0]))
else:
print("你输入的商品不存在")
elif del_choice== "b":
continue
elif car_list == None:
print("你的购物车空了。。。即将返回")
continue
else:
print("你的输入有误")
else:
print("你的输入有误")
else:
print("用户名或密码错误!")