首页 技术 正文
技术 2022年11月19日
0 收藏 907 点赞 2,188 浏览 4485 个字
-----元组-----
元组查询
 a = (1,2,3,4)
print(a[1:2]) #(2,)
购物车练习(列表方法练习)
 product_list=[
['Mac',9000],
['kindle',800],
['tesla',900000],
['python book',105],
['bike',2000],
]
pubs_list = []
save = input("please input money:")
if save.isdigit():
save = int(save)
while True: print("shopping info".center(50,"-"))
#、打印商品内容
for i,v in enumerate(product_list,1):
print(i,v)
choice = input("please input nums:")
#验证输入是否合法
if choice.isdigit():
choice = int(choice)
if choice > 0 and choice <= len(product_list):
#将用户选择商品用p_iters取出
p_iters = product_list[choice-1]
# print(p_iters)
#如果剩余钱足够,可以继续购买
if p_iters[1] < save:
pubs_list.append(p_iters)
print(p_iters)
save -= p_iters[1]
else:
print("余额不足 %s" % save)
elif choice == 'quit':
for j in pubs_list:
# print(pubs_list)
print("您购买的商品 :%s" % j)
print("购买商品剩余金额 :%s" % save)
break else:
print("Invalid input")
-----字典-----字典:是Python中唯一的映射类型,采用键值对的形式存储数据。
特点:1、字典是无序的,且键(key)可哈希 2、键唯一不可变类型:整型,字符串,元祖
可变类型:列表,字典字典的创建
 a=list()  #列表创建
print(a) #[] dic={'name':'dream'}
print(dic) #{'name': 'dream'} dic1={}
print(dic1) #{} dic2=dict((('name','dream'),))
print(dic2) #{'name': 'dream'} dic3=dict([['name','dream'],])
print(dic3) #{'name': 'dream'}
id方法使用
 a = 100
print(id(a)) #
b = a
print(id(b)) #
b = 20
print(id(b)) #
字典增加
 dic1 = {'name':'dream'}
print(dic1) #{'name': 'dream'}
#setdefault,键存在,返回想用的键相应的值;,键不存在,在字典中添加新的键值对
ret = dic1.setdefault('age',20)
print(dic1) #{'name': 'dream', 'age': 20}
print(ret) #
字典的查询
 dic2 = {'age': 20, 'name': 'dream'}
print(dic2['name']) #dream
显示列表中所有的键
 print(dic2.keys()) #dict_keys(['age', 'name'])
print(list(dic2.keys())) #['name', 'age']
#显示列表中说有的值
print(list(dic2.values())) #[20, 'dream']
#显示列表中说有的键、值
print(list(dic2.items())) #[('name', 'dream'), ('age', 20)]
字典修改
 dic3 = {'age': 20, 'name': 'dream'}
dic3['name'] = 'rise'
print(dic3) #{'name': 'rise', 'age': 20}
#update
dic4 = {'age':18,'sex':'man'}
dic3.update(dic4)
print(dic3) #{'age': 18, 'sex': 'man', 'name': 'rise'}
字典删除
 dic5 = {'age': 18, 'sex': 'man', 'name': 'rise'} #del 删除键值对
del dic5['age']
print(dic5) #{'sex': 'man', 'name': 'rise'}
#clear 清空字典
dic5.clear()
print(dic5) #{} #pop 删除字典中指定键值对,并返回该键值对的值
ret = dic5.pop('name')
print(ret) #rise
print(dic5) #{'sex': 'man', 'age': 18}
#popitem 随机删除某组键值对,病以元祖方式返回值
ret = dic5.popitem()
print(ret) #('sex', 'man')
print(dic5) #{'name': 'rise', 'age': 18}
#删除整个字典
del dic5
print(dic5)
字典初始化
 dic6 = dict.fromkeys(['age', 'sex','name','rise'],'test')
print(dic6) #{'rise': 'test', 'sex': 'test', 'age': 'test', 'name': 'test'}
字典嵌套
 school = {
"teachers":{
'xiaowang':["高个子","长的帅"],
'xiaohu':["技术好","玩的好"]
},
"students":{
"zhangsan":["成绩好","爱讲笑话"]
}
}
字典嵌套查询
 print(school['teachers']['xiaohu'][0]) #技术好
print(school["students"]["zhangsan"][1]) #爱讲笑话
字典嵌套修改
 school["students"]["zhangsan"][0] = "眼睛很好看"
print(school["students"]["zhangsan"][0]) #眼睛很好看
字典排序
 dic = {6:'',2:'',5:''}
print(sorted(dic)) #[2, 5, 6]
print(sorted(dic.values())) #['222', '555', '666']
print(sorted(dic.items())) #[(2, '222'), (5, '555'), (6, '666')]
循环遍历
 dic7 = {'name': 'rise', 'age': 18}
for i in dic7:
print(("%s:%s") % (i,dic7[i])) #name:rise age:18

-----字符串-----
 a = "this is my progect"
#重复输出字符串
print(a*2) #重复2次输出 this is my progectthis is my progect
#通过索引获取字符串
print(a[3:]) #s is my progect
#in 方法判度
print('is' in a) #True
#格式化输出字符串
print('%s mode1' % a) #this is my progect mode1 #字符串拼接
a = "this is my progect"
b = "test"
print("".join([a,b])) #this is my progecttest d = "this is my progect"
e = "test"
f = ""
print(f.join([d,e])) #this is my progecttest #字符串常用内置方法
a = "this is my progect"
#居中显示
print(a.center(50,'*')) #****************this is my progect****************
#统计 元素在字符串中重复次数
print(a.count("is")) #
#首字母大写
print(a.capitalize()) #This is my progect
#以某个内容结尾字
print(a.endswith("ct")) #True
#以某个内容开头字
print(a.startswith("th")) #True
#调整空格数
a = "this\t is my progect"
print(a.expandtabs(tabsize=10)) #this is my progect
#查找一个元素,返回元素索引值
a = "this is my progect"
print(a.find('is')) #
a = "this is my progect{name},{age}"
print(a.format(name='dream',age=18)) #this is my progectdream,18
print(a.format_map({'name':'rise','age':20})) #this is my progectrise,20
print(a.index('s')) #
#判度字符串时候包含数字
print("abc1234".isalnum()) #True
#检查是否数字
print(''.isdigit())#True
#检查字符串是否合法
print('123abc'.isidentifier()) #False
print(a.islower()) #True 判断是否全小写
print(a.isupper())
print('f d'.isspace()) #是否包含空格
print("My Project".istitle()) #首字母大写 True
print('my project'.upper()) #MY PROJECT
print('my project'.lower()) #my project
print('My project'.swapcase()) #mY PROJECT
print('my project'.ljust(50,"-")) #my project----------------------------------------
print('my project'.rjust(50,'-')) #----------------------------------------my project
#去掉字符串空格与换行符
print(" my project\n".strip()) #my project
print('test')
#替换
print("my project project".replace('pro','test',1)) #my testject project
#从右向左查找
print("my project project".rfind('t')) #
#以右为准分开
print("my project project".rsplit('j',1)) #['my project pro', 'ect']
print("my project project".title()) #My Project Project
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,022
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,773
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,851