-----元组-----
元组查询
 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

第一部分day03-元组、字典、字符串的更多相关文章

  1. 2.9高级变量类型操作(列表 * 元组 * 字典 * 字符串)_内置函数_切片_运算符_for循环

    高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...

  2. python基础(4)-元组&字典&字符串&队列

    元组(tuple) #元组相对列表来说不同之处是只读不可写 读操作和列表一致 letter_tuple = ('a','b','c','d'); print(letter_tuple[0]);#res ...

  3. 跟着ALEX 学python day2 基础2 模块 数据类型 运算符 列表 元组 字典 字符串的常用操作

    声明 : 文档内容学习于 http://www.cnblogs.com/xiaozhiqi/  模块初始: Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相 ...

  4. Python列表,元组,字典,字符串方法笔记

    01. 列表 1.1 列表的定义 List(列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组 专门用于存储 一串 信息 列表用 [] 定义,数据 之间使用 , 分隔 列 ...

  5. Python学习---列表/元组/字典/字符串/set集合/深浅拷贝1207【all】

    1.列表 2.元组 3.字典 4.字符串 5.set集合 6.深浅拷贝

  6. python之列表/元组/字典/字符串

    一.列表 格式:list = ['xxx','xxx','xxx'] 性质:可以修改列表内容 copy用法: import copy names = ['] names01 = names #直接引用 ...

  7. python3速查参考- python基础 4 -> 元组 + 字典 + 字符串 的学习

    元组 元组:特点就是内容不可变,算只读的列表,可以被查询,不能被修改 a = 2, print(a) print(type(a)) b = ('a','b','c') print(b[1]) 运行结果 ...

  8. python高级变量类型(元组,列表,字典, 字符串和重要方法)

    高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...

  9. 第二天----列表、元组、字符串、算数运算、字典、while

    列表 列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 基本操作: 索引切片追加删除长度切片循环包含 创建.查看列表: 列表中的数字不要加引号,列表的索引从0开始: lis ...

  10. Python第三天 序列 数据类型 数值 字符串 列表 元组 字典

    Python第三天 序列  数据类型  数值  字符串  列表  元组  字典 数据类型数值字符串列表元组字典 序列序列:字符串.列表.元组序列的两个主要特点是索引操作符和切片操作符- 索引操作符让我 ...

随机推荐

  1. C++中int与string的相互转换

    一.int转string 1.c++11标准增加了全局函数std::to_string: string to_string (int val); string to_string (long val) ...

  2. 使用教育邮箱免费申请JetBrains套装(IntelliJ, PhpStorm, WebStorm...)

    想下个PhpStorm来写php,发现可以使用教育账号白嫖. 申请步骤 打开 申请页面 ,点击 “APPLY NOW” 开始申请. 填写姓名,以及学校提供给你的邮箱(edu后缀邮箱,或.end.cn) ...

  3. zookeeper图形化的客户端工具(ZooInspector)

    1.ZooInspector下载地址 https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 2.解压压缩 ...

  4. UVA10559 方块消除 Blocks 题解

    设g[i][j][k]为消去区间[i,j]中的方块,只留下k个与a[i]颜色相同的方块的最大价值,f[i][j]为将[i,j]中所有方块消去的价值,转移自己yy一下即可. 为什么这样是对的?因为对于一 ...

  5. 新电脑安装操作系统一定要注意硬盘是否被bitlocker加密!

    新电脑安装操作系统一定要注意硬盘是否被bitlocker加密! 前段时间帮一MM的戴尔灵越14燃5488装机,购买不久的电脑,硬盘是被bitlocker加密的,鬼知道戴尔为什么这么过分.按照常规思路, ...

  6. MySQL实战45讲学习笔记:第十三讲

    一.引子 经常会有同学来问我,我的数据库占用空间太大,我把一个最大的表删掉了一半的数据,怎么表文件的大小还是没变? 那么今天,我就和你聊聊数据库表的空间回收,看看如何解决这个问题. 这里,我们还是针对 ...

  7. [LeetCode] 35. Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. multiply two numbers using + opertor

    public class Solution { public static void main(String[] args) { , y = ; ; ; i <= y; i++) res = i ...

  9. HTML+css基础 标签的起名 style标签 选择器的使用规则

    标签的起名: 1. 官方提供的标签名 2. 类名: 用class属性起的名字 3. Id名: 用id属性起的名字 唯一的 我们把这种起名叫选择器 class选择器 id选择器  标签选择器 style ...

  10. CF1200D 【White Lines】

    退役快一年了之后又打了场紧张刺激的$CF$(斜眼笑) 然后发现$D$题和题解里的大众做法不太一样 (思路清奇) 题意不再赘述,我们可以看到这个题~~好做~~在只有一次擦除机会,尝试以此为突破口解决问题 ...